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.
Let’s break down the file.
- 
plugin: Define the inventory plugin,azure_rm. - 
include_vm_resource_groups: Control the scope of the inventory. Set toansible. - 
auth_source: Set toauto. 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.
/
- hosts_azure_rm.yml
 
Output the inventory as a graph with the ansible-inventory command.
Azure returns two hosts:
<LinuxHost>_2300<WindowsHost>_1ec7
As well as two groups:
allungrouped
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.
/
- hosts_azure_rm.yml
 
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.
Next, execute the following command in the terminal to run site.yml 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.
If the VM’s "image.offer" variable contains "WindowsServer", place it in the 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.
/
- hosts_azure_rm.yml
 
Run the ansible-inventory command to verify group memberships.
Run the site.yml playbook to configure the web servers by using the following command:
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 thehosts_azure_rm.ymlfile.ansible_inventory: To populate the dynamic repository using thehosts_azure_rm.ymlfile.conditional groups: An option used with theazure_rmplugin to add hosts to groups based onJinja2conditionals.
