Use Ansible To Deploy ARM Templates From Local File System

Posted by Richard Waal on Tuesday, May 28, 2019

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.


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
    state: present
    resource_group_name: myResourceGroup
    name: myDeployment
    location: West Europe
    template: "{{ lookup('file', 'azuredeploy.json') }}"
        value: myWebApp
        value: myHostingPlan
        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.

comments powered by Disqus