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.
roles/base/tasks/main.ymlroles/base/defaults/main.ymlroles/base/handlers/main.ymlroles/interfaces/tasks/main.ymlplaybooks/site.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Roles | base (banners, contact, domain, location, LLDP, NTP) · interfaces (the two port loops) |
|---|---|
| Defaults | prefixed with the role name: base_banner_text, base_motd_text, base_contact … |
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.
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.
The base role
Create the role’s skeleton, and look at it in the Explorer:
run in the terminalansible-galaxy role init roles/basePut the values in the role’s defaults:
editroles/base/defaults/main.ymlCtrl+P → main.ymlbase_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.combase_motd_textis new: the message of the day, shown when a CLI session starts. 6.2 overrides it.Move the tasks. Copy the tasks from
hello.ymlandbase.ymlinto 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 configThe four single config lines fit in one
ios_configtask, becauselines: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 configThen NTP, as it is in
base.yml. Every task notifies the handler.Move the handler from
base.ymlinto the role:editroles/base/handlers/main.ymlCtrl+P → main.yml- name: Save the running config cisco.ios.ios_command: commands: - write memoryRun the role from
site.yml:editplaybooks/site.ymlCtrl+P → site.yml- name: Fabric hosts: fabric gather_facts: false roles: - baseansible-playbook playbooks/site.yml: the MOTD is new, sochangedon the banners and the handler runs. A second run:changed=0. The role does exactly what the playbooks did.
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.1when 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
background: part 6, Moving tasks into a role