In this exercise, you create a new static inventory containing hosts and groups.
Outcomes
You should be able to create default and custom static inventories.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start playbook-inventory
Procedure 2.1. Instructions
Change into the /home/student/playbook-inventory/ directory.
[student@workstation ~]$ cd playbook-inventory
[student@workstation playbook-inventory]$Create a custom static inventory file named inventory in the /home/student/playbook-inventory working directory.
Information about your four managed hosts is listed in the following table. Assign each host to multiple groups for management purposes based on the purpose of the host, the city where it is located, and the deployment environment to which it belongs.
In addition, groups for US cities (Raleigh and Mountain View) must be set up as children of the us group so that hosts in the United States can be managed as a group.
Table 2.1. Server Inventory Specifications
| Host name | Purpose | Location | Environment |
|---|---|---|---|
servera.lab.example.com
| Web server | Raleigh | Development |
serverb.lab.example.com
| Web server | Raleigh | Testing |
serverc.lab.example.com
| Web server | Mountain View | Production |
serverd.lab.example.com
| Web server | London | Production |
Create an inventory file in the /home/student/playbook-inventory working directory.
Using the Server Inventory Specifications table as a guide, edit the inventory file so that it contains the following content:
[webservers] server[a:d].lab.example.com [raleigh] servera.lab.example.com serverb.lab.example.com [mountainview] serverc.lab.example.com [london] serverd.lab.example.com [development] servera.lab.example.com [testing] serverb.lab.example.com [production] serverc.lab.example.com serverd.lab.example.com [us:children] raleigh mountainview
Use variations of the ansible-navigator inventory command to verify the managed hosts and groups in the custom /home/student/playbook-inventory/inventory inventory file.
Your ansible-navigator inventory command must include the -i option to specify the location of your inventory file, as shown in the following steps.
List all managed hosts in the inventory by using the ansible-navigator inventory -i inventory -m stdout --list command.
[student@workstation playbook-inventory]$ansible-navigator inventory \>-i inventory -m stdout --list{ "_meta": { "hostvars": {} }, "all": { "children": [ "development", "london", "production", "testing", "ungrouped", "us", "webservers" ] }, "development": { "hosts": [ "servera.lab.example.com" ] }, "london": { "hosts": [ "serverd.lab.example.com" ] }, "mountainview": { "hosts": [ "serverc.lab.example.com" ] }, "production": { "hosts": [ "serverc.lab.example.com", "serverd.lab.example.com" ] }, "raleigh": { "hosts": [ "servera.lab.example.com", "serverb.lab.example.com" ] }, "testing": { "hosts": [ "serverb.lab.example.com" ] }, "us": { "children": [ "mountainview", "raleigh" ] }, "webservers": { "hosts": [ "servera.lab.example.com", "serverb.lab.example.com", "serverc.lab.example.com", "serverd.lab.example.com" ] } }
Graph all managed hosts in the inventory file that are not part of a group by running the ansible-navigator inventory -i inventory -m stdout --graph ungrouped command.
No ungrouped managed hosts exist in this inventory file.
[student@workstation playbook-inventory]$ansible-navigator inventory \>-i inventory -m stdout --graph ungrouped@ungrouped:
Graph all managed hosts in the development group by using the ansible-navigator inventory -i inventory -m stdout --graph development command.
[student@workstation playbook-inventory]$ansible-navigator inventory \>-i inventory -m stdout --graph development@development: |--servera.lab.example.com
Graph all managed hosts in the testing group by using the ansible-navigator inventory -i inventory -m stdout --graph testing command.
[student@workstation playbook-inventory]$ansible-navigator inventory \>-i inventory -m stdout --graph testing@testing: |--serverb.lab.example.com
Graph all managed hosts in the production group by using the ansible-navigator inventory -i inventory -m stdout --graph production command.
[student@workstation playbook-inventory]$ansible-navigator inventory \>-i inventory -m stdout --graph production@production: |--serverc.lab.example.com |--serverd.lab.example.com
Graph all managed hosts in the us group by using the ansible-navigator inventory -i inventory -m stdout --graph us command.
[student@workstation playbook-inventory]$ansible-navigator inventory \>-i inventory -m stdout --graph us@us: |--@mountainview: | |--serverc.lab.example.com |--@raleigh: | |--servera.lab.example.com | |--serverb.lab.example.com
Run ansible-navigator inventory -i inventory in interactive mode.
Browse groups and managed host entries in the inventory file.
Type :0 to browse groups.
Type :1 to browse hosts.
Type :q to exit ansible-navigator.
[student@workstation playbook-inventory]$ansible-navigator inventory \>-i inventoryTitle Description 0│Browse groups Explore each inventory group and group members members 1│Browse hosts Explore the inventory with a list of all hosts
This concludes the section.