Bookmark this page

Guided Exercise: Creating Host Inventories

How well do you know your network and the resources you manage? You probably already know these quite well. It may seem counterproductive to be required to create yet another place to store and manage information about hosts. Yes, but consider: (1) the inventory plays a foundational role in the ecosystem of Red Hat Ansible Engine tools, (2) text files structured in INI or YAML format can be extremely effective at modeling relationships simply (with host groups and groups of groups, for instance; or by providing easy ways to associate variables with hosts and groups), and (3) static, file-based inventories illustrate important principles, but dynamic inventories unleash the ability to potentially tap into any suitably crafted API, program, or data store providing host and group information.

At example.com, which hosts do we put into our inventory?

In the early, start-up years of example.com, there were just two managed hosts: cs01 and server03. That makes for a simple hosts inventory. To make matters interesting, this exercise builds an inventory that corresponds to example.com where it is today: eight managed network devices and servers. See Appendix A, Table of Lab Network Hosts and Groups for the full list.

In this exercise, you will create a static Ansible hosts inventory for the Lab Environment.

Outcomes

You should be able to:

  • Create a hosts inventory file for the Lab Environment.

  • Verify the syntax and contents of the inventory by inspecting it with the ansible-inventory tool.

Open a terminal window on the workstation machine. This exercise involves editing a file (the inventory file). Vim and Atom are available on the workstation machine. For a Vim refresher, see Appendix C, Editing Files with Vim.

  1. Create a hosts inventory file for the Lab Environment. In this guide, you will use the cat command to show the target contents of a file that needs editing. You do not need to use the cat command yourself; instead this indicates that a session with your preferred text editor is required. Using your preferred text editor, create your first version of a static inventory by creating a file named inventory containing this text:

    [student@workstation ~]$ cat inventory
    [leafs]
    leaf01
    leaf02
    
    [spines]
    spine01
    spine02
    
    [cloud-services]
    cs01
    
    [servers]
    server01
    server02
    server03
  2. Verify the syntax and contents of the inventory you created:

    [student@workstation ~]$ ansible-inventory -i inventory --graph leafs
    @leafs:
      |--leaf01
      |--leaf02
  3. Explore your inventory using the ansible command with host patterns:

    [student@workstation ~]$ ansible -i inventory --list-hosts 'cs*'
      hosts (1):
        cs01
  4. Use ranges to simplify the way group members are specified in your inventory:

    [student@workstation ~]$ cat inventory
    [leafs]
    leaf[01:02]
    
    [spines]
    spine[01:02]
    
    [cloud-services]
    cs01
    
    [servers]
    server[01:03]
  5. Create a group named network containing the groups spines, leafs, and cloud-services:

    [student@workstation ~]$ cat inventory
    [leafs]
    leaf[01:02]
    
    [spines]
    spine[01:02]
    
    [cloud-services]
    cs01
    
    [network:children]
    spines
    leafs
    cloud-services
    
    [servers]
    server[01:03]
  6. To make things easier later, create explicit groups that classify your network devices by network OS: one named ios that contains the cs01 host, and one named vyos that contains the spines and leafs groups:

    [student@workstation ~]$ cat inventory
    [leafs]
    leaf[01:02]
    
    [spines]
    spine[01:02]
    
    [cloud-services]
    cs01
    
    [ios:children]
    cloud-services
    
    [vyos:children]
    spines
    leafs
    
    [network:children]
    vyos
    ios
    
    [servers]
    server[01:03]
  7. Note that groups all and ungrouped are created automatically.

    [student@workstation ~]$ ansible-inventory -i inventory --graph all
    @all:
        |--@network:
        | |--@ios:
        | | |--@cloud-services:
        | | | |--cs01
        | |--@vyos:
        | | |--@leafs:
        | | | |--leaf01
        | | | |--leaf02
        | | |--@spines:
        | | | |--spine01
        | | | |--spine02
        |--@servers:
        | |--server01
        | |--server02
        | |--server03
        |--@ungrouped:

This concludes the guided exercise.

Revision: do457-2.5-4693601