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.
| Field | Value |
|---|---|
Git Site1 Inventory
| |
Add a source for this inventory using the information in the following table. Make sure that you can successfully synchronize the source.
| Field | Value |
|---|---|
Git Inventory Lab
| |
| (selected) |
You might see the following message below the 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.
Navigate to https://controller.lab.example.com and log in as the admin user with redhat as the password.
Create an inventory in your automation controller called Git Site1 Inventory in the Default organization.
| Field | Value |
|---|---|
Git Site1 Inventory
| |
Then, add a source for this inventory using the information in the following table. After you save the source, start the synchronization process. Verify that the synchronization process succeeds.
| Field | Value |
|---|---|
Git Inventory Lab
| |
| (selected) |
Navigate to → and then click → .
Enter Git Site1 Inventory in the field and select the organization. When done, click .
Click the tab, and then click to add a source for the inventory.
Enter Git Inventory Lab in the field.
In the list, choose .
Select the project. You might see the following message below the field:
That value was not found. Please enter or select a valid value.
The message disappears as soon as you select a different field.
Select inventory as the .
Select the option and then click .
Click and wait until the value of the field displays the message.
Review the Git Site1 Inventory to identify the hosts and the groups.
Navigate to → and click .
Review the and the tab and notice the hosts and groups that were added to the inventory.
In the tab, click the link for dev_webservers group and notice the variables are also imported.
Although the variables here are displayed in JSON format, the web UI can also display variables in YAML format.
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.
Navigate to → .
Click the icon for the template.
Click the search icon for and select Git Site1 Inventory.
In the field, enter dev_webservers, dev_dbservers, and then click .
Click to launch the job template.
Observe the live output of the running job.
Click the tab and verify that the field displays the message.
Verify the servers from the dev_webservers and dev_dbservers groups have the correct variables in the message of the day.
Open a terminal on the workstation machine and use SSH to log in to servera.lab.example.com.
[student@workstation ~]$ssh servera.lab.example.comWarning: 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 ~]$
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 ~]$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.
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/
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.gitCloning into 'compreview-inventory'... ...output omitted... [student@workstation git-repos]$cd compreview-inventory
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
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...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 pushEnumerating objects: 5, done. Counting objects: 100% (5/5), done. ...output omitted...
Synchronize the Compreview Inventory project.
From the automation controller web UI, navigate to → .
Click the icon for the project and wait until the synchronization is complete.
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.
Navigate to → .
Click the icon for the template.
Observe the live output of the running job.
After the job is successful, notice that the tasks also executed on serverc.lab.example.com.
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.comWarning: 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 ~]$
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]$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.
Navigate to in the left navigation bar.
In the window, under the section, click .
Confirm that is set to 86400 seconds. If the setting is different, click , edit that field to contain the value 86400, and click .
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.
Navigate → . For the Refresh Fact Cache Compreview job template, click the icon to edit that job template.
In the field, select the .
Select the checkbox and then click .
On the page, click and wait until the field displays the message.
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).
Navigate to → .
Click the inventory, and then click the tab.
Click , and then click the tab.
Verify that the ansible_local['sysctl']['netdev_max_backlog'] custom fact for servera.lab.example.com is available in the cache.
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.
Navigate to → and then click → .
Enter the initial details for the smart inventory, but do not save the smart inventory yet.
| Field | Value |
|---|---|
Site1 Servers for Patching
| |
Click the search icon for the field. Create an advanced filter using the following information and then click .
| Field | Value |
|---|---|
| Filter type |
Advanced
|
| Key |
ansible_facts
|
| Search field |
ansible_local__sysctl__netdev_max_backlog="1000"
|
Press after you add the search field, and then click .
Click to create the smart inventory.
Click the 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.
If you do not see the specified hosts, then look for a typing mistake in the smart inventory host filter.