When Ansible runs a playbook, it uses an inventory to help determine the managed hosts on which the plays should be run. Administrators explicitly specify hosts in static inventories that both Ansible and automation controller can use.
However, static inventories require manual administration to keep the inventories up-to-date. This can be inconvenient or challenging, especially when an organization wants to run playbooks against hosts that are dynamically created in a virtualization or cloud computing environment.
Dynamic inventories determine which hosts and host groups should be in the inventory based on information from some external source. Large IT environments that frequently deploy, test, and remove systems can benefit from using dynamic inventories.
By default, automation controller includes built-in dynamic inventory support for various external inventory sources (or cloud inventory sources), including:
Amazon EC2
Google Compute Engine
Microsoft Azure Resource Manager
VMware vCenter
Red Hat Satellite 6
OpenStack
Red Hat Virtualization
Red Hat Ansible Automation Platform
Red Hat Insights
In addition, you can use inventory plug-ins that are provided by installed Ansible Content Collections. The following command displays the inventory plug-ins that are available from the ee-supported-rhel8 automation execution environment.
[user@host ~]$ansible-navigator doc --list -t inventory \>--eei ee-supported-rhel8 --pp never --mode stdout[WARNING]: Error parsing collection metadata requires_ansible value from coll... [WARNING]: Error parsing collection metadata requires_ansible value from coll... [WARNING]: Collection redhat.insights does not support Ansible version 2.13.0 advanced_host_list Parses a 'host list' with ranges amazon.aws.aws_ec2 EC2 inventory source amazon.aws.aws_rds rds instance source ansible.controller.controller Ansible dynamic inventory plug-in for the Automa... auto Loads and executes an inventory plug-in specifie... constructed Uses Jinja2 to construct vars and groups based ... generator Uses Jinja2 to construct hosts and groups from ... host_list Parses a 'host list' string ini Uses an Ansible INI file as inventory source kubernetes.core.k8s Kubernetes (K8s) inventory source redhat.insights.insights insights inventory source redhat.openshift.openshift OpenShift inventory source redhat.rhv.ovirt oVirt inventory source redhat.satellite.foreman Foreman inventory source script Executes an inventory script that returns JSON servicenow.itsm.now Inventory source for Service Now table records toml Uses a specific TOML file as an inventory source yaml Uses a specific YAML file as an inventory source
Some of these, such as yaml and ini, are the plug-ins that provide static inventory support. Others, such as redhat.rhv.ovirt and amazon.aws.aws_ec2, construct the inventory dynamically from a separate source of information.
The remainder of this section examines two dynamic inventory plug-ins and how to configure them in automation controller. The first example explores the built-in support for getting information from OpenStack.
The second example briefly examines the built-in support for getting inventory information from a Red Hat Satellite 6 server.
Cloud technologies such as OpenStack bring many changes to the server lifecycle. Managed hosts come and go much more frequently and they can be created and started by external applications. Maintaining an accurate static inventory file can be challenging over any length of time, and is even more difficult when managed hosts must be added and removed frequently with little or no warning. Having an inventory that updates itself dynamically, based on information provided directly from OpenStack, solves this problem.
The process for configuring dynamic inventories, using any of the built-in cloud sources, follows the same basic steps:
Create a credential to authenticate to the cloud data source you intend to use, using a credential type matching the source.
Create a new inventory to provide dynamic inventory information.
In that new inventory, create a source with one of the built-in dynamic inventory sources. Configure the new inventory source to use the associated credential for the cloud service. You might set other options, such as enabling an automatic update of the inventory from its source whenever the inventory is used (using the Update on launch feature).
Perform an initial synchronization of the inventory from the source to populate the list of inventory hosts and host groups in automation controller.
The fields displayed in the section change based on the selected credential type. Mandatory fields are marked with a red asterisk.
| Field | Description |
|---|---|
| Username | The user who can access the required resources. |
| Password (API Key) | The password or API key for the specified user. |
| Host (Authentication URL) | The authentication URL of the OpenStack host to authenticate with, for example https://demo.lab.example.com/v2.0/. |
| Project (Tenant Name) | The name of the project (tenant) that you want to use. |
After creating an OpenStack credential, you can use the credential with a dynamic OpenStack source. You can either create a new inventory and add the OpenStack source to it, or you can add the OpenStack source to an existing inventory.
Dynamic sources provide the following update options:
Overwrite
The inventory update process deletes all child groups and hosts from the local inventory that are not found on the external source. By default, this option is not selected, which means that all child hosts and groups that are not found on the external source remain untouched.
Overwrite Variables
All inventory variables that are not found on the external source are removed. By default, this option is not selected, which means that an update of this dynamic inventory combines local variables with those found on the external source.
Update on Launch
Automation controller launches a separate job to update the inventory from the external source each time a job runs that uses the inventory. The inventory update job runs before automation controller runs the automation job initiated by the job template.
The source list for an inventory displays all sources for the inventory. The status column displays the results of the last synchronization attempt for each inventory. Hover over an individual status icon to see brief information about the synchronization status, or click the status icon to see the entire output of the inventory synchronization job.
Click the icon to manually synchronize an inventory source.
After a successful synchronization, review the child groups and hosts that have been created in automation controller using the information from the external source. A dynamic inventory might update every time you synchronize it with the external source.
To see all hosts in an inventory, navigate to → , click the link for the inventory, and then click the tab.
To see all groups in an inventory, navigate to → , click the link for the inventory, and then click the tab. To see the hosts that belong to a group, click the link for the group and then click the tab.
You can synchronize an inventory in the following ways:
Click the icon to manually synchronize an inventory source.
Schedule a job to synchronize an inventory source. The scheduled job can synchronize at a recurring interval.
Configure the inventory source to automatically update when a job template uses the inventory by selecting the checkbox.
Add an Inventory Source Sync node to a workflow job template.
You can use Red Hat Satellite 6 with automation controller to deploy and configure new bare-metal servers. By using a dynamic inventory that gets its information from a Red Hat Satellite 6 server, you can run playbooks on hosts that have registered with the Satellite server.
The following workflow could be used:
The new managed host uses some combination of PXE, DHCP, and TFTP to boot from the network, or a boot image to boot locally, to prepare for either an unattended installation from the Satellite server or an installation through the Satellite Discovery service.
The new managed host performs a Kickstart installation from materials provided by the Satellite server. As part of the process, the managed host registers itself to the Satellite server.
After it registers, the new managed host appears in the dynamic inventory generated from information stored in the Red Hat Satellite 6 server.
Automation controller can launch jobs that use that dynamic inventory to ensure that the new managed host is provisioned correctly.
Automation controller also has a provisioning callbacks feature that can trigger initial provisioning jobs for new managed hosts. More information about this feature is available at https://docs.ansible.com/automation-controller/latest/html/userguide/job_templates.html#provisioning-callbacks.
The procedure to create a dynamic inventory that uses Red Hat Satellite 6 is similar to the procedure used to create one that uses OpenStack.
Create a new credential using the credential type. In addition to the credential name, a Red Hat Satellite 6 credential requires you to complete the , , and fields.
| Field | Description |
|---|---|
Satellite 6 URL
| The URL for the Satellite server, such as https://satellite.example.com. |
Username
| The name of a Satellite user. |
Password
| The password for the Satellite user. |
Create a new inventory to provide dynamic inventory information.
In that inventory, add a new source. Select as the source and select the Red Hat Satellite 6 credential that you created previously. If desired, select one or more options that control how to update the inventory source. Select the checkbox if you plan to use provisioning callbacks.
Synchronize the Red Hat Satellite 6 inventory source. If the synchronization completes successfully, then automation controller updates the inventory with the hosts and groups provided by the external source.