Introduction

Get introduced to dynamic inventories.

Infrastructure is rarely static. Existing infrastructure is scaled out or up. New infrastructure is deployed to support new applications or services. And sometimes, new infrastructure shows up. You don’t always know what it’s for, but you manage it anyhow.

Each of those events requires modifications to the Ansible inventory. Adding hosts to the inventory requires you to create a new entry in the hosts file and assign it to the appropriate groups. While doable, this can be cumbersome. Ansible solves the problem of inventory management with dynamic inventories.

Destroyed Infrastructure
In case you deleted the Ansible development environment, make sure to re-deploy the infrastructure for AWS and Azure by following the links.

Dynamic inventory

A dynamic inventory uses an external source to generate the Ansible inventory. That external source can be:

  • Your cloud provider.
  • An IP address management (IPAM) and data center infrastructure management (DCIM) tool such as Netbox.
  • Even a custom script that generates JSON that Ansible can parse as an inventory.

Plugins

Ansible provides a few ways to use dynamic inventories, but the recommended approach is to use an inventory plugin. For a full list, check out Ansible’s plugin list. There are several inventory plugins available. However, we will only cover two:

  • aws_ec2 for AWS
  • azure_rm for Azure

Both plugins query the cloud provider for a list of instances or virtual machines at a high level. The results are then used to populate the hosts portion of an inventory. Group creation and membership are also dynamically created.

Parameters

The aws_ec2 plugin uses either of the following parameters:

  • groups
  • keyed_groups

While the azure_rm plugin uses:

  • conditional_groups
  • keyed_groups

Regardless of the parameter name, each of these plugins uses hostvars to determine group membership.

Options

You will create dynamic inventories for the hosts/VMs deployed on AWS and Azure:

Get hands-on with 1200+ tech skills courses.