online posts 16
ansible 101 for networking

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.

you edit, in the labinventory/hosts.ymlinventory/group_vars/fabric.ymlinventory/host_vars/leaf1.ymlinventory/host_vars/leaf2.ymlplaybooks/base.yml

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

given
NTP servers162.159.200.1 and 162.159.200.123 (Cloudflare)
Locationsspine1 Row A, rack 1 (done for you) · leaf1 Row A, rack 2 · leaf2 Row A, rack 3
Modulescisco.ios.ios_ntp_global (NTP, from a list) · cisco.ios.ios_config (the location line)
1 explain

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.yml is read for all three switches.
    • host_vars/<host>.yml: values for one host. host_vars/leaf1.yml is 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.

2 show

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
3 follow along

The fabric’s settings, and NTP from data

  1. Move the connection settings. Open the inventory and cut the whole vars: block (all five lines) out of fabric:

    editinventory/hosts.ymlCtrl+P → hosts.yml
    all:
      children:
        fabric:
          children:
            spines:
              hosts:
                spine1:
            leaves:
              hosts:
                leaf1:
                leaf2:
    

    That’s all the inventory should be: who exists, in which groups.

  2. Paste the four settings into the fabric’s group file, with no indentation: in a group_vars file, the variables are the top level.

    editinventory/group_vars/fabric.ymlCtrl+P → fabric.yml
    ansible_connection: ansible.netcommon.network_cli
    ansible_network_os: cisco.ios.ios
    ansible_user: admin
    ansible_password: capeos-lab
    
  3. Prove nothing broke: ansible-inventory --host leaf1 still shows all four, now coming from group_vars.

  4. Add the NTP servers to the same file, shaped the way ios_ntp_global wants them: a list, each item with a server:

    editinventory/group_vars/fabric.ymlCtrl+P → fabric.yml
    ntp_servers:
      - server: 162.159.200.1
      - server: 162.159.200.123
    
  5. Use them in a playbook. Open base.yml and add the first task under tasks::

    editplaybooks/base.ymlCtrl+P → base.yml
      tasks:
        - name: NTP servers, from group_vars
          cisco.ios.ios_ntp_global:
            config:
              servers: "{{ ntp_servers }}"
    
  6. 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 tab show running-config | include ntp: two ntp server lines. make check T=3.1 passes the NTP checks and fails the location ones: those are yours.

4 challenge

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:

  1. Give leaf1 and leaf2 their location in their own host_vars files: Row A, rack 2 and Row A, rack 3.
  2. Add a second task to playbooks/base.yml that sets the switch’s location from the location variable. The config line is snmp-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.1

when 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