Azure - Create a Dynamic Inventory

Create a dynamic inventory of the deployed Linux and Windows hosts on Azure.

We'll cover the following

Ansible has a built-in inventory plugin for Azure called azure_rm. This plugin queries Azure Resource Manager for the VM details and constructs an Ansible inventory from that information.

Virtual machines in Azure populate host entries, and groups and group memberships are determined by host variables assigned to each host.

We have created a file named hosts_azure_rm.yml.

hosts_azure_rm.yml

Let’s break down the file.

  • plugin: Define the inventory plugin, azure_rm.

  • include_vm_resource_groups: Control the scope of the inventory. Set to ansible.

  • auth_source: Set to auto. The auto will follow the default precedence of the module parameters → environment variables → default profile in the credential file.

Because we are leveraging the environment variables to connect to Azure with a service principal, the azure_rm plugin will use those.

Click on the Run button and wait for the environment to set up.

This code requires the following environment variables to execute:
AZURE_SUBSCRIPTION_ID
Not Specified...
AZURE_CLIENT_ID
Not Specified...
AZURE_SECRET
Not Specified...
AZURE_TENANT
Not Specified...
Azure_Linux_VM_Instance_DNS_Name
<Linux>
Azure_Windows_VM_Instance_DNS_Name
<Windows>
/
hosts_azure_rm.yml
hosts_azure_rm.yml

Output the inventory as a graph with the ansible-inventory command.

Output inventory

Azure returns two hosts:

  • <LinuxHost>_2300
  • <WindowsHost>_1ec7

As well as two groups:

  • all
  • ungrouped

By default, the plugin will use a globally unique hostname. That is why you see _2300 and _1ec7 appended to the hostnames.

You can disable this feature by setting plain_host_names to yes.

This code requires the following environment variables to execute:
AZURE_SUBSCRIPTION_ID
Not Specified...
AZURE_CLIENT_ID
Not Specified...
AZURE_SECRET
Not Specified...
AZURE_TENANT
Not Specified...
Azure_Linux_VM_Instance_DNS_Name
<Linux>
Azure_Windows_VM_Instance_DNS_Name
<Windows>
/
hosts_azure_rm.yml
roles
group_vars
configure_nginx_web_server.yml
configure_iis_web_server.yml
site.yml
index.html
Azure dynamic inventory

Update the <Password> with the password created using the ansible-vault command in the group_vars/linux.yml and group_vars/windows.yml files.

Run the ansible-inventory command to view the hostnames.

Output inventory

Next, execute the following command in the terminal to run site.yml playbook:

Execute the playbook

Host Pattern
Could not match the supplied host pattern.

The playbook fails because the linux and windows group memberships are no longer defined.

Within the static hosts file are your assigned group memberships, and Ansible used those groups for targeting the playbook and for attaching variables.

To get the site.yml to run, you will have to correct the group memberships.

Conditional groups#

The azure_rm inventory plugin has a parameter called conditional_groups—conditional groups map group names to a Jinja2 expression. When the expression evaluates as true, the host is added to the named group.

The syntax for a conditional group starts with the group’s name, followed by a colon, and then a Jinja2 expression.

If the VM’s "name" variable contains "linux", place it in the linux group.

Linux group

If the VM’s "image.offer" variable contains "WindowsServer", place it in the windows group.

Windows Group

Each of the conditional statements above would work. However, creating a group based on the virtual machine’s name doesn’t guarantee it will include all Linux machines.

A better option is to use the hostvar os_profile.system. This variable is populated by Azure and provides a general category for the OS.

You can use jq or PowerShell to output the os_profile.system hostvar.

Add conditional groups using the os_profile.system hostvar to the hosts_azure_rm.yml file.

Update the <Password> with the password created using the ansible-vault command in the group_vars/linux.yml and group_vars/windows.yml files.

Click on the Run button and wait for the environment to set up.

This code requires the following environment variables to execute:
AZURE_SUBSCRIPTION_ID
Not Specified...
AZURE_CLIENT_ID
Not Specified...
AZURE_SECRET
Not Specified...
AZURE_TENANT
Not Specified...
Azure_Linux_VM_Instance_DNS_Name
<Linux>
Azure_Windows_VM_Instance_DNS_Name
<Windows>
/
hosts_azure_rm.yml
roles
group_vars
configure_nginx_web_server.yml
configure_iis_web_server.yml
site.yml
index.html
Azure dynamic inventory

Run the ansible-inventory command to verify group memberships.

Verify group memberships

Run the site.yml playbook to configure the web servers by using the following command:

Execute the playbook

In this lesson, we introduced how to create a dynamic inventory of hosts deployed on AWS using Ansible. We looked at the following commands and modules:

  • azure_rm: To query virtual machine instances from Azure. We made use of this in the hosts_azure_rm.yml file.
  • ansible_inventory: To populate the dynamic repository using the hosts_azure_rm.yml file.
  • conditional groups: An option used with the azure_rm plugin to add hosts to groups based on Jinja2 conditionals.
AWS - Use Keyed Groups
Azure - Use Keyed Groups
Mark as Completed
Report an Issue