Chapter 3: Implementing an Ansible Playbook
Exercise: Building an inventory
You will build a static inventory for four web servers, grouping them three different ways (by city, by environment, and by country), then prove the groups are right with ansible-navigator inventory.
These are the four managed hosts and their properties:
| 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 |
The US cities (Raleigh and Mountain View) must also be children of a us group, so all US hosts can be managed together.
Building an Ansible inventory
- Create a custom static inventory with groups and child groups.
- Verify hosts and groups with ansible-navigator inventory.
Prerequisites: Complete control-node setup and the preceding lessons in this chapter. Run commands as student on workstation unless a step names another machine.
From the project directory, run the relevant home-lab check. For an exercise with several checkpoints, grade each state before changing or removing it:
lab grade playbook-inventoryThe grader reads project files and host state. A passing report covers the listed checks; also perform the task’s independent connection, repeat-run, and reboot checks where requested.
Add a fifth host in London for testing. Predict which city, country, environment, and function selections change.
Record the published result first. The grader checks the original requirements, so a changed name or value may intentionally fail those checks.
An operations team needs to target these four servers by function, city, environment, and country.
- Save a static
inventorymatching the host table above, using lowercase group names:webservers,raleigh,mountainview,london,development,testing, andproduction. usmust contain the Raleigh and Mountain View groups. Every host must belong to a named group.- Prove the complete host list, the production membership, and the inherited membership of
us. No server configuration should change.
Move into the project directory
[student@workstation ~]$ cd ~/playbook-inventory
[student@workstation playbook-inventory]$Write the inventory file
Create a file called inventory in the project directory. Try it yourself from the table above first, then compare with the solution.
[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
mountainviewNotice the alphabetic range server[a:d] for the webservers group.
List every host and group
Because this inventory is not the default, every command needs -i inventory.
[student@workstation playbook-inventory]$ ansible-navigator inventory \
> -i inventory -m stdout --listIn the JSON, check that all.children lists development, london, mountainview, production, raleigh, testing, ungrouped, us and webservers, and that us has mountainview and raleigh as children.
Confirm nothing is ungrouped
[student@workstation playbook-inventory]$ ansible-navigator inventory \
> -i inventory -m stdout --graph ungrouped
@ungrouped:An empty group is the right answer: every host belongs to at least one named group.
Graph the environment groups
[student@workstation playbook-inventory]$ ansible-navigator inventory \
> -i inventory -m stdout --graph development
@development:
|--servera.lab.example.com
[student@workstation playbook-inventory]$ ansible-navigator inventory \
> -i inventory -m stdout --graph testing
@testing:
|--serverb.lab.example.com
[student@workstation playbook-inventory]$ ansible-navigator inventory \
> -i inventory -m stdout --graph production
@production:
|--serverc.lab.example.com
|--serverd.lab.example.comGraph the nested us group
[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.comChild groups are shown with an @ and their hosts are indented beneath them.
Browse it interactively
Run the command without -m stdout. Type :0 to browse groups, :1 to browse hosts, and :q to quit.
[student@workstation playbook-inventory]$ ansible-navigator inventory -i inventory
Title Description
0│Browse groups Explore each inventory group and group members members
1│Browse hosts Explore the inventory with a list of all hostsFinish
Check your work with lab grade playbook-inventory, then put the exercise away with lab finish playbook-inventory.