In this recipe, we will use dynamic inventory to target hosts and build an Ansible inventory using the Azure Resource Manager API. Dynamic inventory enables us to leverage its dynamic nature by not hardcoding the host's addresses in a static inventory file.
Ansible packages Azure's inventory script within an Ansible repository. We can find the inventory script at https://github.com/ansible/ansible/blob/devel/contrib/inventory/azure_rm.py. The inventory script also requires the same credentials located at ~/.azure/credentials, as required for other Azure Cloud modules.
Ansible can target the hosts present in a group, and by default, the Azure inventory creates the following groups:
- azure (all hosts): This will contain all hosts, and can be accessed using hosts: azure.
- location name: This will group hosts by their location-for example, westus...