online posts 16
ansible 101 for networking

task 4.1 · chapter 4 of 8: loops, conditionals, templates · 25 min

Interfaces from data

objective

List every port as data, bring them all up with one looped task, and give only the uplinks a jumbo MTU.

you edit, in the labinventory/host_vars/leaf1.ymlinventory/host_vars/leaf2.ymlplaybooks/interfaces.yml

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

given
leaf1GigabitEthernet1 to spine1 Gi1, uplink · GigabitEthernet2 host1
leaf2GigabitEthernet1 to spine1 Gi2, uplink · GigabitEthernet2 host2
Uplink MTU9100
Modulecisco.ios.ios_interfaces: name, description, enabled, mtu
1 explain

What is a loop in Ansible, and what does when do?

A switch has dozens of ports. Writing one task per port would be a very long playbook, and every new port would be a playbook change. Instead:

  • describe the ports as data: a list in each switch’s host_vars file;
  • write one task with a loop. Ansible runs the task once per item in the list, and inside the task, item is the current one.

Some ports need something extra. Uplinks between switches carry everything, so they get a bigger MTU (9100, “jumbo frames”). That’s a condition: when on a task says “only run this if…”. With a loop, it’s checked for every item, so the task runs for some ports and is skipped for the rest.

For ports, the lab uses a resource module, cisco.ios.ios_interfaces: you say what a port should look like (description, enabled, MTU), and it works out the commands. That matters for one line in particular. A port that’s up shows no no shutdown line in the running config, so ios_config would never find it there and would send it on every run. ios_interfaces knows that enabled: true means “no shutdown line”.

2 show

What does a looped task look like?

The data, in host_vars/spine1.yml (already there as an example):

interfaces:
  - { name: GigabitEthernet1, description: "to leaf1 Gi1", uplink: true }
  - { name: GigabitEthernet2, description: "to leaf2 Gi1", uplink: true }

Each - { ... } is one item: a small dictionary with a name, a description, and maybe uplink: true. The { } form is just a one-line way of writing what you’ve been writing over several lines.

The task that uses it:

- name: Description and no shutdown, one port at a time
  cisco.ios.ios_interfaces:
    config:
      - name: "{{ item.name }}"                   # item = the current port
        description: "{{ item.description }}"
        enabled: true                             # no shutdown
  loop: "{{ interfaces }}"                        # run once per entry in the list
  loop_control:
    label: "{{ item.name }}"                      # print just the name, not the whole item

loop:, loop_control: and later when: are task keywords: they sit at the same indentation as the module name, not inside it.

3 follow along

leaf1’s ports, and the loop

  1. Give leaf1 its ports, shaped like spine1’s:

    editinventory/host_vars/leaf1.ymlCtrl+P → leaf1.yml
    location: Row A, rack 2
    interfaces:
      - { name: GigabitEthernet1, description: "to spine1 Gi1", uplink: true }
      - { name: GigabitEthernet2, description: "host1" }
    

    GigabitEthernet1 goes to the spine. GigabitEthernet2 faces a host, so no uplink.

  2. Write the looped task:

    editplaybooks/interfaces.ymlCtrl+P → interfaces.yml
      tasks:
        - name: Description and no shutdown, one port at a time
          cisco.ios.ios_interfaces:
            config:
              - name: "{{ item.name }}"
                description: "{{ item.description }}"
                enabled: true
          loop: "{{ interfaces }}"
          loop_control:
            label: "{{ item.name }}"
    
  3. Run it with ansible-playbook playbooks/interfaces.yml --limit spine1,leaf1 (leaf2 has no ports listed yet). Each switch shows one line per port: changed: [leaf1] => (item=GigabitEthernet1).

  4. The ports are up and LLDP is on (chapter 2), so the switches can now see each other. Click leaf1 on the Topology tab, then in its tab show lldp neighbors: spine1, on Gi1. Your description says the same thing.

4 challenge

leaf2’s ports, and jumbo frames on uplinks only

  1. Give leaf2 its ports, from the table at the top.
  2. Add a second looped task that sets mtu: 9100, but only when the port is an uplink. Ports without uplink should show skipping.

Run the whole playbook (make run T=4.1) twice. The second run changes nothing.

hint

when takes an expression without {{ }}. Not every port has an uplink key, so give it a default: item.uplink | default(false).

hint

The second task is a copy of the first, with mtu: 9100 in place of the description and enabled, and one extra line at the bottom.

show the solutionplaybooks/interfaces.yml

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

# inventory/host_vars/leaf2.yml
location: Row A, rack 3
interfaces:
  - { name: GigabitEthernet1, description: "to spine1 Gi2", uplink: true }
  - { name: GigabitEthernet2, description: "host2" }
    - name: Jumbo MTU on uplinks only
      cisco.ios.ios_interfaces:
        config:
          - name: "{{ item.name }}"
            mtu: 9100
      loop: "{{ interfaces }}"
      loop_control:
        label: "{{ item.name }}"
      when: item.uplink | default(false)

5 check: run it, then prove it

$ make run T=4.1
$ make check T=4.1

when it's right, you'll see

╭─ task 4.1 · interfaces from data
│  ✓ playbooks/interfaces.yml runs cleanly
│  ✓ inventory/host_vars/spine1.yml lists its interfaces (2)
│  ✓ spine1 GigabitEthernet1: 'to leaf1 Gi1', no shutdown (got 'to leaf1 Gi1', up)
│  ✓ spine1 GigabitEthernet1 is an uplink, so mtu 9100 (got 9100)
│  ✓ spine1 GigabitEthernet2: 'to leaf2 Gi1', no shutdown (got 'to leaf2 Gi1', up)
│  ✓ spine1 GigabitEthernet2 is an uplink, so mtu 9100 (got 9100)
│  ✓ inventory/host_vars/leaf1.yml lists its interfaces (2)
│  ✓ leaf1 GigabitEthernet1: 'to spine1 Gi1', no shutdown (got 'to spine1 Gi1', up)
│  ✓ leaf1 GigabitEthernet1 is an uplink, so mtu 9100 (got 9100)
│  ✓ leaf1 GigabitEthernet2: 'host1', no shutdown (got 'host1', up)
│  ✓ inventory/host_vars/leaf2.yml lists its interfaces (2)
│  ✓ leaf2 GigabitEthernet1: 'to spine1 Gi2', no shutdown (got 'to spine1 Gi2', up)
│  ✓ leaf2 GigabitEthernet1 is an uplink, so mtu 9100 (got 9100)
│  ✓ leaf2 GigabitEthernet2: 'host2', no shutdown (got 'host2', up)
│  ✓ a second run of playbooks/interfaces.yml changes nothing (changed=0)
╰─ 15/15 checks passed  verified