I was looking at the best way to create an inventory from a virtual machine scale set and found this gem. The Azure dynamic-inventory plugin is the easiest way to get VMs, scale set and resources from resource groups and do stuff with ansible.

Requirements

  • Azure CLI
  • Ansible installed
  • pip install azure or
pip install -r requirements-azure.txt

https://github.com/ansible-collections/azure

Assuming you have the requirements it is very easy to get info and an inventory file from Azure.

Step 1: Install the plugin

ansible-galaxy collection install azure.azcollection

https://galaxy.ansible.com/azure/azcollection

Step 2: Login to the azure cli

az login

Step 3: Create a dynamic inventory file

The filename has to end in azure_rm.yaml, my file name is azdoscaleset_azure_rm.yaml

This simple file will get all the VMs in the resource group and inside scale sets

Step 4: Test your query

This shows the machines that are up and available

Step 5: Test Deployment with Ansible Ping

Since I created the VMs with username / password then I am using the flags

You can create different ones based on tags or conditionally for more info see: https://docs.microsoft.com/en-us/azure/developer/ansible/dynamic-inventory-configure?tabs=azure-cli