Use Ansible To Deploy ARM Templates From Local File System

Use Ansible To Deploy ARM Templates From Local File System

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.

 1- name: Create Azure Resource Group deployment
 2  azure_rm_deployment:
 3    state: present
 4    resource_group_name: myResourceGroup
 5    name: myDeployment
 6    location: West Europe
 7    template: "{{ lookup('file', 'azuredeploy.json') }}"
 8    parameters:
 9      siteName:
10        value: myWebApp
11      hostingPlanName:
12        value: myHostingPlan
13      sku:
14        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 really is not a very elegant method of deploying templates. Also, there's no need to put your template on a public git repository, storage account, etc.