Bookmark this page

Guided Exercise: Building an Ansible Inventory

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

  1. Change into the /home/student/playbook-inventory/ directory.

    [student@workstation ~]$ cd playbook-inventory
    [student@workstation playbook-inventory]$
  2. 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 namePurposeLocationEnvironment
    servera.lab.example.com Web serverRaleighDevelopment
    serverb.lab.example.com Web serverRaleighTesting
    serverc.lab.example.com Web serverMountain ViewProduction
    serverd.lab.example.com Web serverLondonProduction

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

    Important

    Your ansible-navigator inventory command must include the -i option to specify the location of your inventory file, as shown in the following steps.

    1. 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"
              ]
          }
      }
    2. 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:
    3. 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
    4. 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
    5. 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
    6. 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
    7. 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 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 hosts

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 playbook-inventory

This concludes the section.

Revision: rh294-9.0-c95c7de