Search⌘ K
AI Features

Azure - Create a Dynamic Inventory

Explore how to use the azure_rm inventory plugin in Ansible to dynamically query and manage Azure virtual machines. Understand setting up host groups with conditional Jinja2 expressions for scalable infrastructure management and running playbooks with dynamic inventories.

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.

YAML
plugin: azure_rm
include_vm_resource_groups:
- ansible
auth_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 to ansible.

  • auth_source: Set to auto. 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 ...