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:
/
- hosts_azure_rm.yml
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.
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 ""
.
/
- hosts_azure_rm.yml
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.
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.
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.