2 min read

Tutorial: Using Azure REST API in Ansible to automate Azure resources

Ansible 2.6 was released with more capabilities to provision and manage Azure resources. It includes four new modules for Azure Kubernetes Service and Azure Resource, and many enhancements to existing Ansible modules for Azure (e.g., Virtual Machine, Networking interface).
Here I’ll introduce two new modules azure_rm_resource and azure_rm_resource_facts which allow you to directly call the Azure REST API. If you want to create or manage some Azure resource, but do not find an Ansible module for it, you could use azure_rm_resouce to call the Azure REST API to create that Azure resource and use azure_rm_resource_facts to obtain the facts of that Azure resource.
To get these new Ansible modules for Azure, you need to install:

Or

  • Azure Preview Module role by running below command line if you are still using Ansible 2.5. This playbook role integrates new modules and fixes available in the latest Ansible devel branch. If you want to know more about Ansible Galaxy and Azure Preview Module role, read my last blog.
$ ansible-galaxy install Azure.azure_preview_modules

Below is an example to use azure_rm_resource and azure_rm_resource_facts to create Event Hubs on Azure. It defines variables, such as resource group name, and then executes below four tasks:

  1. Create a resource group;
  2. Create an Event Hubs namespace;
  3. Wait for the namespace to become active asynchronously using facts module;
  4. Create an Event Hub.
- hosts: localhost
  vars:
    resource_group: myResourceGroup
    location: eastus
    namespacename: eventhubsnamesansible
    eventhubname: eventhubsansible
  tasks:
    - name: Create a resource group
      azure_rm_resourcegroup:
        name: "{{ resource_group }}"
        location: "{{ location }}"
    - name: Create a namespace for an event hubs using REST API
      azure_rm_resource:
        api_version: '2017-04-01'
        resource_group: "{{ resource_group }}"
        provider: eventhub
        resource_type: namespaces
        resource_name: "{{ namespacename }}"
        body:
          location: "{{ location }}"
          sku:
            name: Basic
            tier: Basic
            capacity: 10
          properties:
            isAutoInflateEnabled: False
    - name: Wait for namespace to be ready
      azure_rm_resource_facts:
        api_version: '2017-04-01'
        resource_group: "{{ resource_group }}"
        provider: eventhub
        resource_type: namespaces
        resource_name: "{{ namespacename }}"
      register: output
      until: output.response[0].properties.status == 'Active'
      delay: 10
    - name: Create an event hubs using REST API
      azure_rm_resource:
        api_version: '2017-04-01'
        resource_group: "{{ resource_group }}"
        provider: eventhub
        resource_type: namespaces
        resource_name: "{{ namespacename }}"
        subresource:
          - type: eventhubs
            name: "{{ eventhubname }}"
        body:
          location: "{{ location }}"
          properties:
            messageRetentionInDays: 1

Save the preceding playbook as eventhubs-create.yml, or download the sample Ansible playbook here. To run the Ansible playbook, use the ansible-playbook command as follows:

$ ansible-playbook eventhubs-create.yml


Congratulations! You’ve successfully created your event hubs by calling Azure REST API with Ansible. If you go to the portal, you will see the your event hubs there.
Now you could try to create your own Ansible playbook to provision a new Azure resource or manage existing Azure resource by calling Azure REST API. You could learn more from the documentation for azure_rm_resource / azure_rm_resource_facts and search Azure REST API  regarding details related to specific resource.
If you want to learn more about Ansible on Azure, check out the latest Azure Friday.
Questions? Let us know in the comments.