Azure - Use Keyed Groups

Populate groups within a dynamic inventory of hosts deployed on Azure using keyed groups.

Keyed groups offer another way to define and populate groups within a dynamic inventory. Instead of specifying the group name, keyed groups base the group name on a host variable’s value, and group membership is determined by the existence of the host variable. Because of this, a single keyed group can replace both conditional groups we previously created.

Using the os_profile.system host variable to define a keyed group will create two groups: linux and windows. The keyed group then populates the group memberships based on all the hosts that match the same value. If a new Linux VM is deployed, the group membership would dynamically update to two hosts.

A new group would be dynamically created and populated based on the os_profile.system host variable if a new system type were deployed.

Conditional groups make the group memberships dynamic, but keyed groups make the groups and group memberships dynamic.

You will replace the conditional groups with a keyed group using the os_profile.system key. Review the lines 6-7 of the hosts_azure_rm.yml below:

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 using keyed groups

Click on the Run button and wait for the environment to set up. Once set up, run the ansible-inventory command with the graph option.

Confirm ansible inventory

If you look closely, there is an _ at the beginning of the group name. That underscore appears because, by default, keyed groups have separators added to the group names and optionally may have a prefix. Running the site.yml would fail because _linux doesn’t match linux.

You can remove the separator from the keyed group by defining it as "".

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 using Keyed Groups

Once again, Confirm the group_vars are mapping to the hosts.

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

Confirm the inventory

If the variables are mapping correctly you’ll see the variables from the group_vars files populated on the appropriate host.

Run the site.yml playbook.

Execute the playbook

In this lesson, we have introduced keyed_groups to map hosts deployed on Azure to the appropriate groups based on the os_profile.system host variable.

Azure - Create a Dynamic Inventory
Summary
Mark as Completed
Report an Issue