Bookmark this page

Guided Exercise: Building an Ansible Inventory

Create a new static inventory containing managed nodes and groups.

Outcomes

  • Create a custom static inventory using Visual Studio Code.

  • Use automation content navigator to explore the Ansible Inventory.

As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available.

[student@workstation ~]$ lab start implement-inventory

Instructions

  1. From Visual Studio Code (VS Code) on the workstation machine, open the /home/student/implement-inventory directory.

    1. Open VS Code. Click ViewExplorer to open the Primary Side Bar.

    2. Click FileOpen Folder.

    3. Click Homeimplement-inventory and then click Open.

      Note

      If prompted, select Trust the authors of all files in the parent folder 'student', and then click Yes, I trust the authors.

  2. Create a custom static inventory named inventory in the /home/student/implement-inventory directory. Use the following table to organize the Ansible inventory. Assign each managed node to multiple groups for best management, based on its operating system and the data center location.

    Managed nodeOperating systemData center
    iosxe1.lab.example.com Cisco IOS XERaleigh
    iosxe2.lab.example.com Cisco IOS XEJohannesburg
    nxos.lab.example.com Cisco NX-OSRaleigh
    junos.lab.example.com Juniper JunosLondon
    arista-leaf1.lab.example.com Arista EOSJohannesburg
    arista-leaf2.lab.example.com Arista EOSRaleigh
    arista-leaf3.lab.example.com Arista EOSLondon
    1. Click FileNew File.

    2. Enter inventory as the file name, and then click Create New File.

    3. Click Create File in the Files window behind VS Code. The Files window closes after the inventory file is created.

    4. Edit the Ansible inventory file so that it contains the following content, and then click FileSave:

      [ios]
      iosxe[1:2].lab.example.com
      
      [nxos]
      nxos.lab.example.com
      
      [junos]
      junos.lab.example.com
      
      [eos]
      arista-leaf[1:3].lab.example.com
      
      [raleigh]
      iosxe1.lab.example.com
      nxos.lab.example.com
      arista-leaf2.lab.example.com
      
      [johannesburg]
      iosxe2.lab.example.com
      arista-leaf1.lab.example.com
      
      [london]
      junos.lab.example.com
      arista-leaf3.lab.example.com
  3. Explore the Ansible inventory by using the ansible-navigator command in stdout mode. Create an inventory graph of all the Ansible inventory.

    1. In VS Code, click TerminalNew Terminal.

    2. Run the ansible-navigator inventory -m stdout --graph command to obtain the graph of all the Ansible inventory.

      [student@workstation implement-inventory]$ ansible-navigator inventory \
       -m stdout --graph
      @all:
        |--@ungrouped:
        |--@ios:
        |  |--iosxe1.lab.example.com
        |  |--iosxe2.lab.example.com
        |--@nxos:
        |  |--nxos.lab.example.com
        |--@junos:
        |  |--junos.lab.example.com
        |--@eos:
        |  |--arista-leaf1.lab.example.com
        |  |--arista-leaf2.lab.example.com
        |  |--arista-leaf3.lab.example.com
        |--@raleigh:
        |  |--iosxe1.lab.example.com
        |  |--nxos.lab.example.com
        |  |--arista-leaf2.lab.example.com
        |--@johannesburg:
        |  |--iosxe2.lab.example.com
        |  |--arista-leaf1.lab.example.com
        |--@london:
        |  |--junos.lab.example.com
        |  |--arista-leaf3.lab.example.com
  4. Use the following table to define group variables in the Ansible inventory. Create the network_devices group that includes the ios, nxos, junos, and eos groups. Use VS Code to modify the Ansible inventory.

    Managed nodeNetwork platformNetwork device
    iosxe1.lab.example.com cisco.ios.ios Yes
    iosxe2.lab.example.com cisco.ios.ios Yes
    nxos.lab.example.com cisco.nxos.nxos Yes
    junos.lab.example.com junipernetworks.junos.junos Yes
    arista-leaf1.lab.example.com arista.eos.eos Yes
    arista-leaf2.lab.example.com arista.eos.eos Yes
    arista-leaf3.lab.example.com arista.eos.eos Yes
    1. Edit the Ansible inventory file to append the following content to the inventory file, and then click FileSave:

      [network_devices:children]
      ios
      nxos
      junos
      eos
      
      [network_devices:vars]
      ansible_connection=ansible.netcommon.network_cli
      
      [ios:vars]
      ansible_network_os=cisco.ios.ios
      
      [nxos:vars]
      ansible_network_os=cisco.nxos.nxos
      
      [junos:vars]
      ansible_network_os=junipernetworks.junos.junos
      
      [eos:vars]
      ansible_network_os=arista.eos.eos
    2. Select FileSave.

  5. Run automation content navigator in interactive mode in the VS Code terminal. Explore the managed nodes entries in the inventory file and obtain the detail for the arista-leaf3.lab.example.com node.

    1. Run the ansible-navigator inventory command in the VS Code terminal.

      Figure 2.6: Browsing the Ansible inventory
    2. Type 1 in the main Ansible inventory page to list the managed nodes.

      Figure 2.7: List of managed nodes
    3. Type 2 to obtain inventory information about the arista-leaf3.lab.example.com node.

      Figure 2.8:

      Inventory information for the arista-leaf3.lab.example.com managed node

    4. Type :q and then press Enter to exit the ansible-navigator command.

  6. From VS Code, click FileClose Folder to close the /home/student/implement-inventory directory.

Finish

On the workstation machine, 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 implement-inventory

Revision: do457-2.3-7cfa22a