task 3.2 · chapter 3 of 8: variables and facts · 25 min
Facts
objective
Ask each switch for its version and serial number with a facts module, keep the answers in variables, and write them to a file per switch.
playbooks/facts.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Module | cisco.ios.ios_facts, gather_subset: min |
|---|---|
| Version | ansible_facts.net_version |
| Serial | ansible_facts.net_serialnum |
| Output | build/facts/<switch>.yml, on this machine |
How do you use what a switch tells you?
Chapter 1 asked the switches questions and you copied the answers by hand. A playbook can keep the answers itself, and use them.
A facts module asks a switch about itself and hands the answers back as data, already picked out of the
output: version, serial number, image, hostname. cisco.ios.ios_facts runs show version for you and parses
it. Its answers land in a variable called ansible_facts, for that switch, for the rest of the play:
ansible_facts.net_version, ansible_facts.net_serialnum.
Two more pieces make it useful:
set_factcreates variables of your own, with names that make sense to you:sw_versionreads better later thanansible_facts.net_version. Variables made this way are also called facts.delegate_to: localhostruns a task on this machine instead of the switch, but with the switch’s variables. That’s how you write one file per switch, here.
(Any task’s result can be kept, too: register: something saves it in a variable. You’ll use that in 5.3.)
What does a facts module give you?
gather_subset: min asks for the basics. The ones you’ll use:
ansible_facts:
net_hostname: leaf1
net_version: 0.1.2 # the Version line of show version
net_serialnum: CAPEQ3I7EKD # the Processor board ID
net_image: flash:capeos-bolt.bin # the software image
...
To reach a value, walk down with dots: ansible_facts.net_version.
debug prints anything mid-play, which is the quickest way to see what you’ve got:
- ansible.builtin.debug:
var: ansible_facts
Ask, and pick out what you need
Open the playbook. Add the facts task under
tasks::editplaybooks/facts.ymlCtrl+P → facts.ymltasks: - name: Gather the switch's facts cisco.ios.ios_facts: gather_subset: minAdd a task that keeps the two facts under short names, and one that prints them:
editplaybooks/facts.ymlCtrl+P → facts.yml- name: Keep the two facts we care about ansible.builtin.set_fact: sw_version: "{{ ansible_facts.net_version }}" sw_serial: "{{ ansible_facts.net_serialnum }}" - name: Show them ansible.builtin.debug: msg: "{{ inventory_hostname }} runs {{ sw_version }}, serial {{ sw_serial }}"inventory_hostnameis a variable Ansible gives you for free: the current host’s name from the inventory.Run it with
make run T=3.2. Three lines likemsg: leaf1 runs 0.1.2, serial CAPEQ3I7EKD: the playbook asked, kept and used the answers.
Write it down
Write each switch’s facts to a file on this machine: build/facts/<switch>.yml, containing version: and
serial:. Two tasks: one that makes sure the build/facts folder exists, and one that writes the file.
hint
Both tasks run here, not on the switch: delegate_to: localhost. The folder only needs creating once:
run_once: true.
hint
The copy module can write a file from a string with content:. Build the content as a dictionary and turn it
into YAML with the to_nice_yaml filter: "{{ {'version': sw_version, 'serial': sw_serial} | to_nice_yaml }}".
{{ playbook_dir }}/../build/facts is the folder, wherever you run from.
show the solutionplaybooks/facts.yml
or pull it into your lab: make solve T=3.2, then git diff --staged shows what changed
- name: Make sure build/facts/ exists (once, on this machine)
ansible.builtin.file:
path: "{{ playbook_dir }}/../build/facts"
state: directory
mode: "0755"
delegate_to: localhost
run_once: true
- name: Write them to build/facts/<switch>.yml
ansible.builtin.copy:
dest: "{{ playbook_dir }}/../build/facts/{{ inventory_hostname }}.yml"
content: "{{ {'version': sw_version, 'serial': sw_serial} | to_nice_yaml }}"
mode: "0644"
delegate_to: localhost
5 check: run it, then prove it
$ make run T=3.2
$ make check T=3.2when it's right, you'll see
╭─ task 3.2 · facts
│ ✓ playbooks/facts.yml runs cleanly
│ ✓ the challenge: build/facts/spine1.yml has its real version and serial
│ ✓ the challenge: build/facts/leaf1.yml has its real version and serial
│ ✓ the challenge: build/facts/leaf2.yml has its real version and serial
╰─ 4/4 checks passed verified
background: part 3, Facts