online posts 16
ansible 101 for networking

task 1.2 · chapter 1 of 8: hello, ansible · 15 min

Write the inventory

objective

List the three switches in groups Ansible understands, and give the fabric the settings to reach them.

you edit, in the labinventory/hosts.yml

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
Spinesspine1
Leavesleaf1, leaf2
Parent groupfabric (holds spines and leaves)
Connectionansible.netcommon.network_cli (SSH), network OS cisco.ios.ios, login admin / capeos-lab
1 explain

What is an Ansible inventory?

Before Ansible can do anything, it needs a list of the devices it manages. That list is the inventory.

The inventory does two jobs:

  1. Names the devices. Ansible calls each one a host: spine1, leaf1, leaf2.
  2. Puts them in groups. A group is a name for a set of hosts, so you can say “all the leaves” instead of listing them every time. Groups can hold other groups, too.

Everything later in the lab hangs off these names. A playbook runs on hosts: fabric; a file of settings for the leaves is called leaves.yml. So the groups you choose here matter.

For our fabric, the natural groups are the spines, the leaves, and a parent group, fabric, that holds both.

2 show

What are the parts of an inventory file?

Inventories are YAML: key: value pairs, nested by indentation (always 2 spaces per level, never tabs). Here’s the shape, with every part named:

all:                      # the top: every inventory starts here
  children:               # "the groups inside all are..."
    fabric:               # a group
      children:           # "the groups inside fabric are..."
        spines:           # a group inside fabric
          hosts:          # "the hosts in spines are..."
            spine1:       # a host. The trailing colon matters: it's a key with nothing under it (yet).
      vars:               # settings every host in fabric gets
        ansible_user: admin

Three keywords do all the work:

KeywordMeansGoes under
children:the groups inside this groupall, or any group
hosts:the hosts in this groupany group
vars:settings for every host in this groupany group, next to children:

The settings that start with ansible_ are special: they tell Ansible how to connect. For a server, Ansible logs in over SSH and runs Python. A switch has no Python, so Ansible logs in over SSH and types commands at its CLI, the way you would. That connection is called network_cli, and ansible_network_os tells it which style of CLI to expect, so it knows what the prompts and error messages look like. These switches speak the classic IOS-style CLI, so it’s cisco.ios.ios.

3 follow along

The fabric, the spines, and how to connect

  1. Open the file:

    editinventory/hosts.ymlCtrl+P → hosts.yml
    all:
      children:
        fabric:
    

    The comments at the top list the TODOs. fabric is there, empty.

  2. Give fabric a child group, spines, with one host, spine1. Line up children: 2 spaces in from fabric:, and each level after that 2 more:

    editinventory/hosts.ymlCtrl+P → hosts.yml
    all:
      children:
        fabric:
          children:
            spines:
              hosts:
                spine1:
    
  3. Save (Ctrl+S) and ask Ansible what it sees:

    run in the terminal
    ansible-inventory --graph
    
    @all:
      |--@ungrouped:
      |--@fabric:
      |  |--@spines:
      |  |  |--spine1
    

    That tree is your file, read back by Ansible. If it doesn’t look like this, the indentation is off.

  4. Now the connection settings. Add vars: inside fabric:, at the same level as its children:, with the four settings:

    editinventory/hosts.ymlCtrl+P → hosts.yml
    all:
      children:
        fabric:
          children:
            spines:
              hosts:
                spine1:
          vars:
            ansible_connection: ansible.netcommon.network_cli
            ansible_network_os: cisco.ios.ios
            ansible_user: admin
            ansible_password: capeos-lab
    

    One line at a time: log in over SSH and drive the CLI, expect the IOS-style CLI, and log in as admin. Yes, the password is in plain text. Chapter 7 fixes that.

  5. Prove it works. This asks spine1 to run one command:

    run in the terminal
    ansible spine1 -m cisco.ios.ios_command -a "commands='show running-config | include hostname'"
    
    spine1 | SUCCESS =>
        changed: false
        stdout:
        - hostname spine1
        stdout_lines: <omitted>
    

    Ansible found the host, logged in, ran the command and brought the answer back.

4 challenge

Add the leaves

The spines are done. On your own, add the second group: leaves, with hosts leaf1 and leaf2, also inside fabric. Then check it the same two ways: ansible-inventory --graph shows both leaves under @leaves, and the hostname command works with fabric instead of spine1.

hint

leaves is a sibling of spines: same indentation, both under fabric’s children:.

hint

You don’t need to repeat the vars:. They’re on fabric, and every host in every group inside it gets them. That’s the point of groups.

show the solutioninventory/hosts.yml

or pull it into your lab: make solve T=1.2, then git diff --staged shows what changed

all:
  children:
    fabric:
      children:
        spines:
          hosts:
            spine1:
        leaves:
          hosts:
            leaf1:
            leaf2:
      vars:
        ansible_connection: ansible.netcommon.network_cli
        ansible_network_os: cisco.ios.ios
        ansible_user: admin
        ansible_password: capeos-lab

5 check: run it, then prove it

$ ansible-inventory --graph
$ make check T=1.2

when it's right, you'll see

╭─ task 1.2 · write the inventory
│  ✓ inventory/hosts.yml parses (ansible-inventory --list)
│  ✓ group spines is [spine1] (got ['spine1'])
│  ✓ group leaves is [leaf1, leaf2] (got ['leaf1', 'leaf2'])
│  ✓ group fabric holds leaves and spines (got ['leaves', 'spines'])
│  ✓ Ansible reaches all three switches over SSH (3/3)
╰─ 5/5 checks passed  verified