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.
inventory/hosts.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Spines | spine1 |
|---|---|
| Leaves | leaf1, leaf2 |
| Parent group | fabric (holds spines and leaves) |
| Connection | ansible.netcommon.network_cli (SSH), network OS cisco.ios.ios, login admin / capeos-lab |
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:
- Names the devices. Ansible calls each one a host:
spine1,leaf1,leaf2. - 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.
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:
| Keyword | Means | Goes under |
|---|---|---|
children: | the groups inside this group | all, or any group |
hosts: | the hosts in this group | any group |
vars: | settings for every host in this group | any 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.
The fabric, the spines, and how to connect
Open the file:
editinventory/hosts.ymlCtrl+P → hosts.ymlall: children: fabric:The comments at the top list the TODOs.
fabricis there, empty.Give
fabrica child group,spines, with one host,spine1. Line upchildren:2 spaces in fromfabric:, and each level after that 2 more:editinventory/hosts.ymlCtrl+P → hosts.ymlall: children: fabric: children: spines: hosts: spine1:Save (Ctrl+S) and ask Ansible what it sees:
run in the terminalansible-inventory --graph@all: |--@ungrouped: |--@fabric: | |--@spines: | | |--spine1That tree is your file, read back by Ansible. If it doesn’t look like this, the indentation is off.
Now the connection settings. Add
vars:insidefabric:, at the same level as itschildren:, with the four settings:editinventory/hosts.ymlCtrl+P → hosts.ymlall: 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-labOne 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.
Prove it works. This asks spine1 to run one command:
run in the terminalansible 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.
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.2when 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
background: part 1, The inventory