task 3.1 · chapter 3 of 8: variables and facts · 30 min
group_vars and host_vars
objective
Move the data out of the inventory and playbooks: connection settings and NTP for the fabric, a location per switch, and one playbook that uses them.
inventory/hosts.ymlinventory/group_vars/fabric.ymlinventory/host_vars/leaf1.ymlinventory/host_vars/leaf2.ymlplaybooks/base.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| NTP servers | 162.159.200.1 and 162.159.200.123 (Cloudflare) |
|---|---|
| Locations | spine1 Row A, rack 1 (done for you) · leaf1 Row A, rack 2 · leaf2 Row A, rack 3 |
| Modules | cisco.ios.ios_ntp_global (NTP, from a list) · cisco.ios.ios_config (the location line) |
What are group_vars and host_vars?
So far the values live inside the playbooks: the contact is typed into hello.yml. That’s fine for one value on
three switches. It stops working the day the leaves need something different from the spines.
Ansible’s answer is to keep data apart from logic:
- the playbook says what to do: “set NTP from the variable
ntp_servers”; - variables say what the values are, and they live in two folders next to the inventory:
group_vars/<group>.yml: values for every host in that group.group_vars/fabric.ymlis read for all three switches.host_vars/<host>.yml: values for one host.host_vars/leaf1.ymlis read for leaf1 only.
Ansible loads these files by name: the file name has to match the group or host name exactly. When you run a
playbook, each switch gets the variables from every file that applies to it, and the playbook uses them with
{{ variable_name }}. One playbook, three switches, three different results.
How do variables reach a playbook?
inventory/
├── hosts.yml who exists: fabric → spines (spine1), leaves (leaf1, leaf2)
├── group_vars/
│ └── fabric.yml ntp_servers: [...] → spine1, leaf1 and leaf2 all get it
└── host_vars/
├── spine1.yml location: Row A, rack 1 → only spine1
├── leaf1.yml location: Row A, rack 2 → only leaf1
└── leaf2.yml location: Row A, rack 3 → only leaf2
# playbooks/base.yml: nothing in here names a switch
- name: Where the switch lives, from host_vars
cisco.ios.ios_config:
lines:
- "snmp-server location {{ location }}" # ← filled in from host_vars/<switch>.yml
The {{ }} is Jinja2, Ansible’s template language: “put the value of this variable here”. On spine1 that line
becomes snmp-server location Row A, rack 1. A value that starts with {{ must be in quotes, or YAML gets
confused; here the whole line is quoted, to be safe.
Some modules take structured data instead of config lines. cisco.ios.ios_ntp_global knows what NTP config
looks like, and takes a list of servers:
- name: NTP servers, from group_vars
cisco.ios.ios_ntp_global:
config:
servers: "{{ ntp_servers }}" # a list: [{server: ...}, {server: ...}]
Shape the variable the way the module wants it, and the playbook hands it over as it is, however many servers there are. Modules like this are called resource modules: they read the switch’s config into data, compare it with yours, and send only what’s missing.
One command shows exactly what a host ends up with, from every file:
ansible-inventory --host leaf1
The fabric’s settings, and NTP from data
Move the connection settings. Open the inventory and cut the whole
vars:block (all five lines) out offabric:editinventory/hosts.ymlCtrl+P → hosts.ymlall: children: fabric: children: spines: hosts: spine1: leaves: hosts: leaf1: leaf2:That’s all the inventory should be: who exists, in which groups.
Paste the four settings into the fabric’s group file, with no indentation: in a
group_varsfile, the variables are the top level.editinventory/group_vars/fabric.ymlCtrl+P → fabric.ymlansible_connection: ansible.netcommon.network_cli ansible_network_os: cisco.ios.ios ansible_user: admin ansible_password: capeos-labProve nothing broke:
ansible-inventory --host leaf1still shows all four, now coming fromgroup_vars.Add the NTP servers to the same file, shaped the way
ios_ntp_globalwants them: a list, each item with aserver:editinventory/group_vars/fabric.ymlCtrl+P → fabric.ymlntp_servers: - server: 162.159.200.1 - server: 162.159.200.123Use them in a playbook. Open
base.ymland add the first task undertasks::editplaybooks/base.ymlCtrl+P → base.ymltasks: - name: NTP servers, from group_vars cisco.ios.ios_ntp_global: config: servers: "{{ ntp_servers }}"Run it with
make run T=3.1. All three switches get the same two servers, and the playbook never says which switch is which. Click leaf2 on the Topology tab, then in its tabshow running-config | include ntp: twontp serverlines.make check T=3.1passes the NTP checks and fails the location ones: those are yours.
A location for every switch
Each switch should know where it lives. spine1’s location is already in inventory/host_vars/spine1.yml. On your
own:
- Give leaf1 and leaf2 their
locationin their ownhost_varsfiles: Row A, rack 2 and Row A, rack 3. - Add a second task to
playbooks/base.ymlthat sets the switch’s location from thelocationvariable. The config line issnmp-server location <where>.
Then run it twice. The second run should change nothing.
hint
ansible-inventory --host leaf2 should show "location": "Row A, rack 3" before you touch the playbook. If it
doesn’t, the variable isn’t where Ansible looks.
hint
It’s one config line, so it’s ios_config, like chapter 2, with the variable inside the line:
"snmp-server location {{ location }}", in quotes.
show the solutionplaybooks/base.yml
or pull it into your lab: make solve T=3.1, then git diff --staged shows what changed
# inventory/host_vars/leaf1.yml
location: Row A, rack 2
# inventory/host_vars/leaf2.yml
location: Row A, rack 3
- name: Where the switch lives, from host_vars
cisco.ios.ios_config:
lines:
- "snmp-server location {{ location }}"
5 check: run it, then prove it
$ make run T=3.1
$ make check T=3.1when it's right, you'll see
╭─ task 3.1 · group_vars and host_vars
│ ✓ inventory/hosts.yml only lists hosts: connection settings moved to group_vars
│ ✓ inventory/group_vars/fabric.yml holds the connection settings
│ ✓ inventory/group_vars/fabric.yml lists two ntp_servers (got ['162.159.200.1', '162.159.200.123'])
│ ✓ playbooks/base.yml runs cleanly
│ ✓ spine1 uses the NTP servers from group_vars (got ['162.159.200.1', '162.159.200.123'])
│ ✓ leaf1 uses the NTP servers from group_vars (got ['162.159.200.1', '162.159.200.123'])
│ ✓ leaf2 uses the NTP servers from group_vars (got ['162.159.200.1', '162.159.200.123'])
│ ✓ the challenge: spine1's location comes from inventory/host_vars/spine1.yml ('Row A, rack 1')
│ ✓ the challenge: leaf1's location comes from inventory/host_vars/leaf1.yml ('Row A, rack 2')
│ ✓ the challenge: leaf2's location comes from inventory/host_vars/leaf2.yml ('Row A, rack 3')
│ ✓ a second run of playbooks/base.yml changes nothing (changed=0)
╰─ 11/11 checks passed verified
background: part 3, group_vars and host_vars