In this exercise, you will create a new static inventory containing hosts and groups.
Outcomes
You should be able to create default and custom static inventories.
Log in to workstation as student using student as the password.
On workstation, run the lab deploy-inventory start command. This start script ensures that the managed hosts, servera, serverb, serverc, and serverd, are reachable on the network.
[student@workstation ~]$lab deploy-inventory start
Procedure 2.1. Instructions
Modify /etc/ansible/hosts to include servera.lab.example.com as a managed host.
Add servera.lab.example.com to the end of the default inventory file, /etc/ansible/hosts.
[student@workstation ~]$sudo vim /etc/ansible/hosts...output omitted... ## db-[99:101]-node.example.comservera.lab.example.com
Continue editing the /etc/ansible/hosts inventory file by adding a [webservers] group to the bottom of the file with serverb.lab.example.com server as a group member. Save and exit when complete.
...output omitted... ## db-[99:101]-node.example.com servera.lab.example.com[webservers]serverb.lab.example.com
Verify the managed hosts in the /etc/ansible/hosts inventory file.
Use the ansible all --list-hosts command to list all managed hosts in the default inventory file.
[student@workstation ~]$ansible all --list-hostshosts (2): servera.lab.example.com serverb.lab.example.com
Use the ansible ungrouped --list-hosts command to list only managed hosts that do not belong to a group.
[student@workstation ~]$ansible ungrouped --list-hostshosts (1): servera.lab.example.com
Use the ansible webservers --list-hosts command to list only managed hosts that belong to the webservers group.
[student@workstation ~]$ansible webservers --list-hostshosts (1): serverb.lab.example.com
Create a custom static inventory file named inventory in the /home/student/deploy-inventory working directory.
Information about your four managed hosts is listed in the following table. You will 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 group us 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 the /home/student/deploy-inventory working directory, and change into it.
[student@workstation ~]$mkdir ~/deploy-inventory[student@workstation ~]$cd ~/deploy-inventory[student@workstation deploy-inventory]$
Create an inventory file in the /home/student/deploy-inventory working directory. Use the Server Inventory Specifications table as a guide. Edit the inventory file and add 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 command to verify the managed hosts and groups in the custom host-or-group -i inventory --list-hosts/home/student/deploy-inventory/inventory inventory file.
Your ansible command must include the -i inventory option. This makes ansible use your inventory file in the current working directory instead of the system /etc/ansible/hosts inventory file.
Use the ansible all -i inventory --list-hosts command to list all managed hosts.
[student@workstation deploy-inventory]$ansible all -i inventory --list-hostshosts (4): servera.lab.example.com serverb.lab.example.com serverc.lab.example.com serverd.lab.example.com
Use the ansible ungrouped -i inventory --list-hosts command to list all managed hosts listed in the inventory file but are not part of a group. There are no ungrouped managed hosts in this inventory file.
[student@workstation deploy-inventory]$ansible ungrouped -i inventory \>--list-hosts[WARNING]: No hosts matched, nothing to do hosts (0):
Use the ansible development -i inventory --list-hosts command to list all managed hosts listed in the development group.
[student@workstation deploy-inventory]$ansible development -i inventory \>--list-hostshosts (1): servera.lab.example.com
Use the ansible testing -i inventory --list-hosts command to list all managed hosts listed in the testing group.
[student@workstation deploy-inventory]$ansible testing -i inventory \>--list-hostshosts (1): serverb.lab.example.com
Use the ansible production -i inventory --list-hosts command to list all managed hosts listed in the production group.
[student@workstation deploy-inventory]$ansible production -i inventory \>--list-hostshosts (2): serverc.lab.example.com serverd.lab.example.com
Use the ansible us -i inventory --list-hosts command to list all managed hosts listed in the us group.
[student@workstation deploy-inventory]$ansible us -i inventory --list-hostshosts (3): servera.lab.example.com serverb.lab.example.com serverc.lab.example.com
You are encouraged to experiment with other variations to confirm managed host entries in the custom inventory file.