Convert an inventory file from INI format to YAML format.
Outcomes
Replace an Ansible inventory file in INI format with its YAML equivalent.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares an Ansible project in /home/student/inventory-yaml/ on the workstation machine.
[student@workstation ~]$ lab start inventory-yaml
Procedure 5.2. Instructions
The lab command provides an inventory file in INI format in the /home/student/inventory-yaml/ directory.
Change to that directory, create a copy of the inventory file and save it as inventory.yml.
Change to the /home/student/inventory-yaml/ directory.
[student@workstation ~]$ cd ~/inventory-yaml/Copy the existing static inventory file to inventory.yml.
[student@workstation inventory-yaml]$ cp inventory inventory.ymlEdit the new inventory file, inventory.yml, to convert it to YAML format.
Convert the groups to YAML format.
Remove the brackets that surround the group names and end each group name with a colon.
Beneath each group name, add the hosts: keyword on a line indented by two spaces relative to the group name.
Indent the hosts in each group by four spaces relative to the group name and add a colon at the end of each hostname.
The exact number of spaces does not matter as long as you keep a consistent indentation.
For example, the following group definition in INI format:
[active_web_servers] server[b:c].lab.example.com
becomes:
active_web_servers:
hosts:
server[b:c].lab.example.com:Repeat the operation for the all_servers and inactive_web_servers groups.
Convert the definition of the child groups of the web_servers group.
The file in INI format lists the child groups under the [web_servers:children] section.
Beneath the group name, add the children: keyword on a line indented by two spaces relative to the group name.
Add each child group on a line indented by four spaces relative to the group name and add a colon at the end of each group name.
For example, the following section:
[web_servers:children] active_web_servers inactive_web_servers
becomes:
web_servers:
children:
active_web_servers:
inactive_web_servers:Finally, convert the all_servers:children group to YAML format by putting the child groups in the children section.
[all_servers:children] web_servers
becomes:
all_servers:
hosts:
servera.lab.example.com:
children:
web_servers:The resulting inventory.yml file should consist of the following content:
active_web_servers:
hosts:
server[b:c].lab.example.com:
inactive_web_servers:
hosts:
server[d:f].lab.example.com:
web_servers:
children:
active_web_servers:
inactive_web_servers:
all_servers:
hosts:
servera.lab.example.com:
children:
web_servers:To test your inventory in YAML format, use the test-ping-all-servers.yml playbook that the exercise provides.
That playbook targets the all_servers group and uses the ping module to test the connection to all the servers.
Use the ansible-navigator command to run the test-ping-all-servers.yml playbook.
Add the --inventory (or -i) option to provide your inventory file in YAML format.
[student@workstation inventory-yaml]$ansible-navigator run -i inventory.yml \>--mode stdout test-ping-all-servers.ymlPLAY [Pinging all servers] ***************************************************** TASK [Ensure the hosts are reachable] ****************************************** ok: [serverb.lab.example.com] ok: [serverc.lab.example.com] ok: [serverd.lab.example.com] ok: [servere.lab.example.com] ok: [servera.lab.example.com] ok: [serverf.lab.example.com] ...output omitted...