Use Ansible To Deploy ARM Templates From Local File System

Ansible deployments to Microsoft Azure

I have been working with Ansible a lot lately. I wanted to share a small trick that I learned in this stackoverflow thread, regarding deploying Azure Resource Manager (ARM) templates from Ansible.

azure_rm_deployment

Deploying an ARM template is done through the azure_rm_deployment Ansible module. The documentation describes two options for providing a template:

  • Inline
    You write the ARM template in YAML format inside the Ansible Playbook.
  • Template link
    You provide a link to a public accessible ARM template, such as on Github.

I learned in the stackoverflow thread it’s possible to simply deploy a template from your local file system, using the method below.

- name: Create Azure Resource Group deployment
  azure_rm_deployment:
    state: present
    resource_group_name: myResourceGroup
    name: myDeployment
    location: West Europe
    template: "{{ lookup('file', 'azuredeploy.json') }}"
    parameters:
      siteName:
        value: myWebApp
      hostingPlanName:
        value: myHostingPlan
      sku:
        value: Standard

As you can see, the trick is to perform a file lookup, which basically reads the contents of the file and loads it as a dictionary. The ‘azuredeploy.json’ is the name of my template file.

That’s it!

Now you don’t have to convert the JSON to YAML and put it inside your playbook. Which is really not a very elegant method of deploying templates. Also no need to put your template on a public location, like Github.

Leave a Reply

Your email address will not be published. Required fields are marked *