Bookmark this page

Lab: Creating an Inventory Managed as a Project

  • Create an inventory in automation controller that is stored and managed in a Git repository and then create a smart inventory that filters your existing inventories.

Outcomes

  • Create a new inventory.

  • Use an existing static inventory file stored in a Git repository as the source for the new inventory.

  • Create a smart inventory based on a custom fact.

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 creates a sysctl.fact custom fact on your managed hosts.

The command also updates automation controller, removing resources created in previous chapters and creating the following resources:

  • A custom fact on your managed hosts named sysctl.fact.

  • A project named Compreview Inventory that synchronizes content from a Git repository containing a static inventory and group variable files.

  • A job template named Deploy compreview MOTD that updates /etc/motd on your managed hosts based on group variables that it gets from the inventory.

  • A job template named Refresh Fact Cache Compreview that updates the fact cache for your managed hosts on automation controller.

[student@workstation ~]$ lab start compreview-inventory

Specifications

In this lab, you create an inventory in automation controller that gets its information from an existing static inventory file that is stored in a Git repository. You also create a smart inventory based on your existing inventories, selecting managed hosts from those inventories based on the value of a custom fact on your managed hosts.

Your automation controller is at https://controller.lab.example.com. You can log in to it using admin as the user and redhat as the password.

Configure your automation controller based on the following specification.

  • Create an inventory on your automation controller using the following information.

    FieldValue
    Name Git Site1 Inventory
    Organization Default

    Add a source for this inventory using the information in the following table. Make sure that you can successfully synchronize the source.

    FieldValue
    Name Git Inventory Lab
    Source Sourced from a Project
    Project Compreview Inventory
    Inventory file inventory
    Update options Update on launch (selected)

    Important

    You might see the following message below the Project field, even if you selected the project name from the list of projects.

    That value was not found. Please enter or select a valid value.

    The message disappears as soon as you select a different field.

    After you configure the Git Site1 Inventory inventory, inspect it to confirm that groups and hosts have been imported and that variables for the dev_webservers and dev_dbservers groups are set.

  • Modify the Deploy compreview MOTD job template so that it uses the Git Site1 Inventory. Limit the job template so that it only runs for the dev_webservers and dev_dbservers groups.

    When you run the job template, confirm that the contents of the /etc/motd file on managed hosts in the inventory reflect the group variables that apply to those hosts.

  • After you complete the preceding item, clone the https://git.lab.example.com/git/compreview-inventory.git Git repository into the /home/student/git-repos directory on the workstation machine. Add serverc.lab.example.com to the dev_webservers group and then commit and push the changes to the Git repository.

    Test the change to the Git repository by synchronizing the Compreview Inventory project and launching the Deploy compreview MOTD job template. Confirm that the contents of the /etc/motd file on serverc.lab.example.com reflects the values of the group variables for the dev_webservers group in the inventory.

  • Create a smart inventory named Site1 Servers for Patching in the Default organization. Configure the smart inventory to consist of managed hosts that have the custom fact ansible_local['sysctl']['netdev_max_backlog'] set to "1000".

    You might need to edit the Refresh Fact Cache Compreview job template so that it stores facts in your automation controller’s cache, and launch it to populate the cache. Make sure that your automation controller caches facts for a day.

    Inspect the completed smart inventory to confirm that it has managed hosts.

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

  2. Create an inventory in your automation controller called Git Site1 Inventory in the Default organization.

    FieldValue
    Name Git Site1 Inventory
    Organization Default

    Then, add a source for this inventory using the information in the following table. After you save the Git Inventory Lab source, start the synchronization process. Verify that the synchronization process succeeds.

    FieldValue
    Name Git Inventory Lab
    Source Sourced from a Project
    Project Compreview Inventory
    Inventory file inventory
    Update options Update on launch (selected)
    1. Navigate to ResourcesInventories and then click AddAdd inventory.

    2. Enter Git Site1 Inventory in the Name field and select the Default organization. When done, click Save.

    3. Click the Sources tab, and then click Add to add a source for the inventory.

    4. Enter Git Inventory Lab in the Name field.

    5. In the Source list, choose Sourced from a Project.

    6. Select the Compreview Inventory project. You might see the following message below the Project field:

      That value was not found. Please enter or select a valid value.

      The message disappears as soon as you select a different field.

    7. Select inventory as the Inventory file.

    8. Select the Update on launch option and then click Save.

    9. Click Sync and wait until the value of the Last Job Status field displays the Successful message.

  3. Review the Git Site1 Inventory to identify the hosts and the groups.

    1. Navigate to ResourcesInventories and click Git Site1 Inventory.

    2. Review the Hosts and the Groups tab and notice the hosts and groups that were added to the inventory.

    3. In the Groups tab, click the link for dev_webservers group and notice the variables are also imported.

      Figure 12.6: Group variables

      Note

      Although the variables here are displayed in JSON format, the web UI can also display variables in YAML format.

  4. Modify the Deploy compreview MOTD job template to use the Git Site1 Inventory and limit it to run only for the dev_webservers and dev_dbservers groups.

    1. Navigate to ResourcesTemplates.

    2. Click the Edit Template icon for the Deploy compreview MOTD template.

    3. Click the search icon for Inventory and select Git Site1 Inventory.

    4. In the Limit field, enter dev_webservers, dev_dbservers, and then click Save.

    5. Click Launch to launch the job template.

    6. Observe the live output of the running job.

      Figure 12.7: Successful output for the "Deploy compreview MOTD" job template
    7. Click the Details tab and verify that the Status field displays the Successful message.

  5. Verify the servers from the dev_webservers and dev_dbservers groups have the correct variables in the message of the day.

    1. Open a terminal on the workstation machine and use SSH to log in to servera.lab.example.com.

      [student@workstation ~]$ ssh servera.lab.example.com
      Warning: Permanently added 'servera.lab.example.com,172.25.250.10' (ECDSA) to the list of known hosts.
      ================================================================================
      Ansible manages this server using the webservers group and the DEV environment.
      ================================================================================
      ...output omitted...
      [student@servera ~]$
    2. Notice the content of the MOTD when accessing the server, and then log out of servera.lab.example.com.

      [student@servera ~]$ exit
      logout
      Connection to servera.lab.example.com closed.
      [student@workstation ~]$
  6. Clone the https://git.lab.example.com/git/compreview-inventory.git Git repository into the /home/student/git-repos directory on the workstation machine. Add the serverc.lab.example.com to the dev_webservers group and then commit and push the changes to the Git repository.

    1. From a terminal, create the /home/student/git-repos directory if it does not already exist, and then change into it.

      [student@workstation ~]$ mkdir -p ~/git-repos/
      [student@workstation ~]$ cd ~/git-repos/
    2. Clone the https://git.lab.example.com/git/compreview-inventory.git Git repository into the /home/student/git-repos directory.

      [student@workstation git-repos]$ git clone \
      > https://git.lab.example.com/git/compreview-inventory.git
      Cloning into 'compreview-inventory'...
      ...output omitted...
      [student@workstation git-repos]$ cd compreview-inventory
    3. Review the inventory file.

      [dev_webservers]
      servera.lab.example.com
      
      [dev_dbservers]
      serverb.lab.example.com
      
      [dev:children]
      dev_webservers
      dev_dbservers
      
      [prod_webservers]
      serverd.lab.example.com
      
      [prod_dbservers]
      servere.lab.example.com
      
      [prod:children]
      prod_webservers
      prod_dbservers
    4. Add serverc.lab.example.com in the [dev_webservers] section in the inventory file.

      [dev_webservers]
      servera.lab.example.com
      serverc.lab.example.com
      ...output omitted...
    5. Commit and push the changes.

      [student@workstation compreview-inventory]$ git add inventory
      [student@workstation compreview-inventory]$ git commit -m "Added serverc"
      [main ec9bd43] Added serverc
       1 file changed, 1 insertion(+)
      [student@workstation compreview-inventory]$ git push
      Enumerating objects: 5, done.
      Counting objects: 100% (5/5), done.
      ...output omitted...
  7. Synchronize the Compreview Inventory project.

    1. From the automation controller web UI, navigate to ResourcesProjects.

    2. Click the Sync Project icon for the Compreview Inventory project and wait until the synchronization is complete.

  8. Launch the Deploy compreview MOTD job template again and verify that the job updates the /etc/motd file on the serverc.lab.example.com host.

    1. Navigate to ResourcesTemplates.

    2. Click the Launch Template icon for the Deploy compreview MOTD template.

    3. Observe the live output of the running job.

    4. After the job is successful, notice that the tasks also executed on serverc.lab.example.com.

      Figure 12.8: Successful output for the "Deploy compreview MOTD" job template
    5. Open a terminal on the workstation machine and use SSH to log in to serverc.lab.example.com.

      [student@workstation compreview-inventory]$ ssh serverc.lab.example.com
      Warning: Permanently added 'serverc.lab.example.com,172.25.250.12' (ECDSA) to the list of known hosts.
      ================================================================================
      Ansible manages this server using the webservers group and the DEV environment.
      ================================================================================
      ...output omitted...
      [student@serverc ~]$
    6. Notice the content of the MOTD when accessing the server, and then log out of serverc.lab.example.com

      [student@serverc ~]$ exit
      logout
      Connection to serverc.lab.example.com closed.
      [student@workstation compreview-inventory]$
  9. Ensure that the automation controller job settings are configured so that the Per-Host Ansible Fact Cache Timeout setting has a value of 86400 seconds.

    1. Navigate to Settings in the left navigation bar.

    2. In the Settings window, under the Jobs section, 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.

  10. Modify the Refresh Fact Cache Compreview job template to enable it to use fact storage, then launch a test job using the Git Site1 Inventory inventory.

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

    2. In the Inventory field, select the Git Site1 Inventory.

    3. Select the Enable Fact Storage checkbox and then click Save.

    4. On the Details page, click Launch and wait until the Status field displays the Successful message.

  11. Verify that the sysctl custom fact for servera.lab.example.com is available in the automation controller cache (the custom fact should be also available for all the servers).

    1. Navigate to ResourcesInventories.

    2. Click the Git Site1 Inventory inventory, and then click the Hosts tab.

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

    4. Verify that the ansible_local['sysctl']['netdev_max_backlog'] custom fact for servera.lab.example.com is available in the cache.

      Figure 12.9: Custom fact in the automation controller cache
  12. Create a smart inventory named Site1 Servers for Patching in the Default organization. Configure the smart inventory to use the ansible_local__sysctl__netdev_max_backlog="1000" filter against cached Ansible facts.

    1. Navigate to ResourcesInventories and then click AddAdd smart inventory.

    2. Enter the initial details for the smart inventory, but do not save the smart inventory yet.

      FieldValue
      Name Site1 Servers for Patching
      Organization Default
    3. 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_local__sysctl__netdev_max_backlog="1000"
    4. Press Enter after you add the search field, and then click Select.

    5. Click Save to create the smart inventory.

    6. Click the Hosts tab and verify that all the hosts are available, including servera.lab.example.com, serverb.lab.example.com, serverc.lab.example.com, serverd.lab.example.com, and servere.lab.example.com.

      Note

      If you do not see the specified hosts, then look for a typing mistake in the smart inventory host filter.

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 previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish compreview-inventory

This concludes the section.

Revision: do467-2.2-08877c1