Bookmark this page

Guided Exercise: Writing YAML Inventory Files

  • 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

  1. 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.

    1. Change to the /home/student/inventory-yaml/ directory.

      [student@workstation ~]$ cd ~/inventory-yaml/
    2. Copy the existing static inventory file to inventory.yml.

      [student@workstation inventory-yaml]$ cp inventory inventory.yml
  2. Edit the new inventory file, inventory.yml, to convert it to YAML format.

    1. 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.

    2. 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:
    3. 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:
  3. 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.yml
    
    PLAY [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...

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish inventory-yaml

This concludes the section.

Revision: do374-2.2-82dc0d7