Bookmark this page

Filtering Hosts with Smart Inventories

Objectives

  • Create a smart inventory that is dynamically constructed from the other inventories on your automation controller using a filter.

Defining Smart Inventories

You have already learned how to manage static and dynamic inventories in automation controller.

  • You can manually create a static inventory in the web UI.

  • You can configure automation controller to use a project to get the inventory from files stored in version control and manage that inventory in the version control system.

  • You can configure a dynamic inventory to get host information from an external source.

Automation controller can dynamically construct a new inventory from inventories that already exist in automation controller by using a smart inventory. A smart inventory generates information by applying a host filter to the union of all static and dynamic inventories configured on the automation controller.

In the most common use case, you configure the host filter to determine if an Ansible fact has a particular value for each host. Hosts that match the host filter are included in the smart inventory. By using smart inventories, you have greater flexibility in how you manage subsets of the hosts defined by the other inventories on the automation controller.

A filter for a smart inventory can select hosts by hostname, group name, or Ansible facts. To modify its filter, your user account needs to have the Organization Administrator role for the smart inventory’s organization. If it is using Ansible facts, then you need to periodically populate the fact cache with a job template that gathers facts and that uses the Enable Fact Storage option to save facts.

You can do this by running a normal play that has gather_facts: yes enabled (this setting is normally set by default), or that runs the setup module as a task. An example minimal play to do this follows:

- name: Refresh fact cache
  hosts: all
  gather_facts: yes

Important

If you observe that automation controller is not caching host facts, then you might need to adjust the setting that determines for how long it caches host facts. Navigate to Settings and click the Jobs settings link. Review the value of the Per-Host Ansible Fact Cache Timeout setting and adjust it if necessary.

Using Ansible Facts in Smart Inventory Filters

You can create smart inventory filters that select hosts for the inventory based on many different criteria. One of the most compelling might be to select hosts based on Ansible facts.

For example, you could configure an Ansible fact-based smart inventory to automatically consist of hosts in your other inventories that satisfy the following or other criteria:

  • Have a specific CPU model.

  • Have a specific processor architecture.

  • Have their eth0 interface configured for a specific subnet.

  • Are running a specific Linux distribution and version.

  • Are running a particular version of the Linux kernel.

  • Mount particular storage devices.

This flexibility and the automatic management of the members of the inventory by automation controller can make a number of system administration tasks easier to automate.

Creating a Smart Inventory Based on Ansible Facts

To create a smart inventory, navigate to ResourcesInventories and click AddAdd smart inventory. Specify a name for the smart inventory and assign it to an organization. Click the search icon to search for the smart host filter for the smart inventory. This opens the Perform a search to define a host filter page.

If you want to filter hosts based on collected Ansible facts, then change the search criteria from Name to Advanced and then select ansible_facts from the Key field.

Figure 7.6: Defining a smart inventory filter with an advanced search

From the Key list, select ansible_facts.

Figure 7.7: Creating a filter using Ansible facts

In the search field, you must enter the Ansible fact that you want to use for the filter, and its value. Use the "inject facts as variables" form of the fact name. For example, instead of using the fact named ansible_facts['eth0'], you should use the name ansible_eth0.

If you need to refer to a fact such as ansible_eth0['mtu'] (which can also be written ansible_eth0.mtu), for the smart inventory filter, then you should use a double underscore in place of the period: ansible_eth0__mtu.

The fact name must be followed by an equals sign (=) and then the exact value for that fact on the hosts that you want to be in the inventory.

Figure 7.8: Some cached fact data

Other Smart Inventory Filters

Many other ways to create a smart host filter are available. For example, you can create simple filters using the name or partial name of a host or group. You can also create advanced filters. The web UI offers the following choices:

  • Name

  • Group

  • ID

  • Inventory ID

  • Enabled

  • Instance ID

  • Last job

  • Insights system ID

  • Advanced

For example, if you use the Name key with the value example.com, then any host that has example.com in its name is selected for this smart inventory.

A number of these choices use the internal ID number of the object in automation controller, which can be obtained from its API.

If you use the Advanced host filter type, then additional keys and values are available.

A range of operators and lookup types are available to match hosts.

OperatorDescription
exact Exact match (default lookup if not specified).
startswith Field starts with value.
istartswith Case-insensitive version of startswith.
endswith Field ends with value.
iendswith Case-insensitive version of endswith.
regex Field matches the given regular expression.
iregex Case-insensitive version of regex.
gt Greater-than comparison.
gte Greater-than or equal to comparison.
lt Less-than comparison.
lte Less-than or equal to comparison.
isnull Specified field is null.
in Field’s value is present in the list provided. Items can be separated by a comma.

After creating a filter, press Enter to apply the filter. This gives you the opportunity to test your filter. If you create a filter and it does not return any results, then either you made a mistake or no hosts match the filter. Another common issue is that automation controller might not have any facts cached. To fix the latter problem, create and run a job template that gathers facts and stores them in the fact cache.

When you are satisfied with the filter, you can save the smart inventory. Like other inventories, you can see the hosts in a smart inventory by clicking its Hosts tab in the web UI.

Revision: do467-2.2-08877c1