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
.
plugin: azure_rminclude_vm_resource_groups:- ansibleauth_source: auto
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.
plugin: azure_rm include_vm_resource_groups: - ansible auth_source: auto
Output the inventory as a graph with the ansible-inventory
command.
ansible-inventory -i hosts_azure_rm.yml --graph
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
.
plugin: azure_rm include_vm_resource_groups: - ansible auth_source: auto plain_host_names: yes
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.
ansible-inventory -i hosts_azure_rm.yml --graph
Next, execute the following command in the terminal to run site.yml
playbook:
ansible-playbook site.yml -i hosts_azure_rm.yml --ask-vault-pass
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: "'linux' in name"
If the VM’s "image.offer"
variable contains "WindowsServer"
, place it in the windows
group.
windows: "'WindowsServer' in image.offer
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.
# Linux VM hostsansible-inventory -i hosts_azure_rm.yml --list | jq .'_meta.hostvars."<WindowsHost>".os_profile.system'# Windows VM hostsansible-inventory -i hosts_azure_rm.yml --list | jq .'_meta.hostvars."<LinuxHost>".os_profile.system'
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.
plugin: azure_rm include_vm_resource_groups: - ansible auth_source: auto plain_host_names: yes conditional_groups: linux: "'linux' in os_profile.system" windows: "'windows' in os_profile.system"
Run the ansible-inventory
command to verify group memberships.
ansible-inventory -i hosts_azure_rm.yml --graph
Run the site.yml
playbook to configure the web servers by using the following command:
ansible-playbook site.yml -i hosts_azure_rm.yml --ask-vault-pass
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.yml
file.ansible_inventory
: To populate the dynamic repository using thehosts_azure_rm.yml
file.conditional groups
: An option used with theazure_rm
plugin to add hosts to groups based onJinja2
conditionals.
Get hands-on with 1300+ tech skills courses.