online posts 16
ansible 101 for networking

task 6.1 · chapter 6 of 8: roles and collections · 35 min

Playbooks into roles

objective

Turn the base and interface playbooks into two roles with defaults and a handler, and run the whole fabric from site.yml.

you edit, in the labroles/base/tasks/main.ymlroles/base/defaults/main.ymlroles/base/handlers/main.ymlroles/interfaces/tasks/main.ymlplaybooks/site.yml

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

given
Rolesbase (banners, contact, domain, location, LLDP, NTP) · interfaces (the two port loops)
Defaultsprefixed with the role name: base_banner_text, base_motd_text, base_contact …
1 explain

What is an Ansible role?

You now have three playbooks, hello.yml, base.yml and interfaces.yml, and each one only makes sense next to the others. A role packages tasks, their handler and their default values into one folder, with a standard layout, so it can be reused, shared, and run with one line.

The big change is defaults. A role’s defaults/main.yml holds the values it uses when nobody said otherwise. They’re the lowest-priority variables there are: anything in group_vars or host_vars wins. So the role works out of the box, and you customise it without editing it. That’s chapter 6.2.

Each variable is prefixed with the role’s name (base_banner_text, not banner_text), so two roles can never fight over the same name.

2 show

What’s inside a role?

ansible-galaxy role init roles/base creates the standard layout. You’ll use three of its folders:

roles/base/
├── tasks/main.yml       the task list: just tasks, no play around them
├── handlers/main.yml    the handlers those tasks notify
├── defaults/main.yml    the values it uses unless someone overrides them
├── meta/main.yml        who wrote it, what it needs (filled in for you)
└── (vars/, files/, templates/, tests/: delete what you don't use)

And the playbook that runs it shrinks to almost nothing:

- name: Fabric
  hosts: fabric
  gather_facts: false
  roles:
    - base

Inside tasks/main.yml, tasks start at the left margin: - name: with no indentation, because there’s no play around them.

3 follow along

The base role

  1. Create the role’s skeleton, and look at it in the Explorer:

    run in the terminal
    ansible-galaxy role init roles/base
    
  2. Put the values in the role’s defaults:

    editroles/base/defaults/main.ymlCtrl+P → main.yml
    base_banner_text: "Managed by Ansible - Network Doodles lab"
    base_motd_text: "Authorized access only. Changes go through Git."
    base_contact: netops@networkdoodles.com
    base_domain_name: lab.networkdoodles.com
    

    base_motd_text is new: the message of the day, shown when a CLI session starts. 6.2 overrides it.

  3. Move the tasks. Copy the tasks from hello.yml and base.yml into the role’s task list, move them to the left margin, and replace the typed-in values with the defaults. The banners are two tasks, one per banner:

    editroles/base/tasks/main.ymlCtrl+P → main.yml
    - name: Login banner
      cisco.ios.ios_banner:
        banner: login
        text: "{{ base_banner_text }}"
        state: present
      notify: Save the running config
    
    - name: Message of the day
      cisco.ios.ios_banner:
        banner: motd
        text: "{{ base_motd_text }}"
        state: present
      notify: Save the running config
    

    The four single config lines fit in one ios_config task, because lines: is a list:

    editroles/base/tasks/main.ymlCtrl+P → main.yml
    - name: Contact, domain, location and LLDP
      cisco.ios.ios_config:
        lines:
          - "snmp-server contact {{ base_contact }}"
          - "ip domain name {{ base_domain_name }}"
          - "snmp-server location {{ location }}"
          - lldp run
      notify: Save the running config
    

    Then NTP, as it is in base.yml. Every task notifies the handler.

  4. Move the handler from base.yml into the role:

    editroles/base/handlers/main.ymlCtrl+P → main.yml
    - name: Save the running config
      cisco.ios.ios_command:
        commands:
          - write memory
    
  5. Run the role from site.yml:

    editplaybooks/site.ymlCtrl+P → site.yml
    - name: Fabric
      hosts: fabric
      gather_facts: false
      roles:
        - base
    
  6. ansible-playbook playbooks/site.yml: the MOTD is new, so changed on the banners and the handler runs. A second run: changed=0. The role does exactly what the playbooks did.

4 challenge

The interfaces role

Make a second role, interfaces, holding the two port loops from playbooks/interfaces.yml (with their tags), and add it to site.yml after base. Delete the folders it doesn’t use. make run T=6.1 twice: the second run changes nothing.

hint

Same steps as base, minus defaults and handlers: it has neither. The port data already lives in host_vars.

hint

roles: is a list; the order is the order they run in.

show the solutionroles/interfaces/tasks/main.yml

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

- 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 }}"
  tags: [descriptions]

- 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)
  tags: [mtu]
# playbooks/site.yml
  roles:
    - base
    - interfaces

5 check: run it, then prove it

$ make run T=6.1
$ make check T=6.1

when it's right, you'll see

╭─ task 6.1 · playbooks into roles
│  ✓ roles/base/tasks/main.yml exists
│  ✓ roles/interfaces/tasks/main.yml exists
│  ✓ roles/base has defaults (the banner text lives there)
│  ✓ roles/base has the save handler
│  ✓ playbooks/site.yml runs cleanly
│  ✓ a second run of playbooks/site.yml changes nothing (changed=0)
╰─ 6/6 checks passed  verified