Bookmark this page

Guided Exercise: Filtering Hosts with Smart Inventories

  • Create a smart inventory and explore how smart inventories work.

Outcomes

  • Create a smart inventory that dynamically includes hosts selected from all other inventories on this automation controller based on whether a particular Ansible fact gathered from each host has a particular value.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command ensures that automation controller is installed and configured with any necessary resources created in earlier exercises.

[student@workstation ~]$ lab start advinventory-smart

Procedure 7.2. Instructions

  1. Navigate to https://controller.lab.example.com and log in as the admin user with redhat as the password.

  2. Make sure that the per-host Ansible fact cache time-out for automation controller is set for exactly one day (86400 seconds). This ensures that facts are cached for hosts but also expire if those facts have not been refreshed recently.

    1. Click Settings in the left navigation bar.

    2. In the Settings window, on the Jobs tile, click Jobs settings.

    3. Confirm that Per-Host Ansible Fact Cache Timeout is set to 86400 seconds. If the setting is different, click Edit, edit that field to contain the value 86400, and click Save.

  3. Edit the Refresh Fact Cache job template to enable it to use fact storage, and then launch a test job using the Dev inventory.

    1. Click ResourcesTemplates. For the Refresh Fact Cache job template, click the Edit Template icon to edit that job template.

    2. Scroll to the bottom of the page, select the Enable Fact Storage checkbox, and then click Save.

    3. On the Details page, click Launch.

    4. Select the Dev inventory and then click Next.

    5. Click Launch to launch the job.

  4. Verify that the facts for servera and serverb in the Dev inventory are available in the automation controller cache.

    1. Navigate to ResourcesInventories.

    2. Click the Dev inventory, and then click the Hosts tab.

    3. Click servera.lab.example.com, and then click the Facts tab.

    4. Verify that the facts for servera.lab.example.com are available in the cache.

    5. Repeat the preceding substeps with serverb.lab.example.com instead of servera.lab.example.com to verify that the facts for serverb are also available in the cache.

  5. Create a smart inventory named Smart that includes the Red Hat Enterprise Linux-based systems that currently have facts cached by automation controller. This smart inventory must select hosts for which the Ansible fact ansible_distribution has the value RedHat.

    1. Navigate to ResourcesInventories.

    2. Click AddAdd smart inventory to create a new smart inventory.

    3. On the Create new smart inventory page, complete the details as follows:

      FieldValue
      Name Smart
      Description Smart Inventory
      Organization Default
    4. Click the search icon for the Smart host filter field. Create an advanced filter using the following information and then click Select.

      FieldValue
      Filter type Advanced
      Key ansible_facts
      Search field ansible_distribution="RedHat"
    5. Click Save to create the smart inventory.

    6. Click the Hosts tab and verify that both servera.lab.example.com and serverb.lab.example.com are available.

      Note

      If you previously collected facts for hosts in other inventories, then you might see additional hosts that match the smart host filter. For example, if you collected facts for the Test inventory, then the smart host filter also matches the serverc.lab.example.com and serverd.lab.example.com hosts.

      If you collect facts for additional inventories, then hosts that match the smart host filter are added to the existing hosts in the smart inventory.

  6. Log out of automation controller.

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from earlier exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish advinventory-smart

This concludes the section.

Revision: do467-2.2-08877c1