online posts 16
ansible 101 for networking

task 6.2 · chapter 6 of 8: roles and collections · 15 min

Defaults and overrides

objective

Give the leaves their own message of the day, and leaf2 one of its own, without touching the role.

you edit, in the labinventory/group_vars/leaves.ymlinventory/host_vars/leaf2.yml

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

given
Variablebase_motd_text, the role’s default
LeavesLeaf switch. Hosts plug in here; uplinks go to spine1.
1 explain

Which Ansible variable wins?

The same variable can be set in several places. Ansible decides which one a host gets with a fixed order called variable precedence. The full list has more than twenty levels; the three you’ll use every day are:

Set inApplies toWins over
host_vars/<host>.ymlone hosteverything below
group_vars/<group>.ymlevery host in the grouprole defaults
roles/<role>/defaults/main.ymlanyone running the rolenothing: it’s the fallback

The more specific the place, the stronger the value. That’s what makes a role reusable: you never edit it, you override its defaults from the inventory.

2 show

How do you see which value wins?

ansible-inventory --host leaf1 | grep motd

shows what leaf1 ends up with from the inventory. Role defaults don’t appear there (they belong to the role, not the host), so no output means “the role’s default”.

On a switch, the MOTD is the first thing a CLI session prints: click leaf1 on the Topology tab. Or ask for it: show banner motd.

3 follow along

A message for the leaves

  1. Create a group file for the leaves. In the Explorer, right-click inventory/group_vars → New File, and call it exactly leaves.yml, the group’s name:

    new fileinventory/group_vars/leaves.ymlCtrl+P → leaves.yml
    ---
    # Only the leaves. Beats the role's default base_motd_text, loses to anything in host_vars/.
    base_motd_text: "Leaf switch. Hosts plug in here; uplinks go to spine1."
    
  2. Run the fabric: make run T=6.2. Only leaf1 and leaf2 change, and the save handler runs on them.

  3. Look: click leaf1 on the Topology tab, and its tab shows the leaves’ message. spine1’s tab still shows the role’s default. One line of data, no change to the role.

4 challenge

A message just for leaf2

leaf2 is the lab’s test leaf. Give leaf2 alone its own message of the day, without changing leaves.yml or the role. leaf1 should keep the leaves’ message, and spine1 the default.

hint

Which file applies to one host only, and beats group_vars?

hint

inventory/host_vars/leaf2.yml already exists, with leaf2’s location and ports. Add one line to it.

show the solutioninventory/host_vars/leaf2.yml

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

location: Row A, rack 3
# Only leaf2. Beats group_vars/leaves.yml, which beats the role's default.
base_motd_text: "leaf2: the lab's test leaf. Expect changes."
interfaces:
  - { name: GigabitEthernet1, description: "to spine1 Gi2", uplink: true }
  - { name: GigabitEthernet2, description: "host2" }

5 check: run it, then prove it

$ make run T=6.2
$ make check T=6.2

when it's right, you'll see

╭─ task 6.2 · defaults and overrides
│  ✓ inventory/group_vars/leaves.yml overrides base_motd_text
│  ✓ the challenge: inventory/host_vars/leaf2.yml gives leaf2 a message of its own
│  ✓ playbooks/site.yml runs cleanly
│  ✓ leaf1 shows the leaves' message of the day (got 'Leaf switch. Hosts plug in here; uplinks go to spine1.')
│  ✓ leaf2 shows its own: host_vars beats group_vars (got "leaf2: the lab's test leaf. Expect changes.")
│  ✓ spine1 keeps the role's default (got 'Authorized access only. Changes go through Git.')
╰─ 6/6 checks passed  verified