Exercise 3: Ansible Facts

Read this in other languages: uk English, japan 日本語, Español Español.

Table of Contents

Objective

Demonstration use of Ansible facts on network infrastructure.

Ansible facts are information derived from speaking to the remote network elements. Ansible facts are returned in structured data (JSON) that makes it easy manipulate or modify. For example a network engineer could create an audit report very quickly using Ansible facts and templating them into a markdown or HTML file.

This exercise will cover:

Guide

Step 1 - Using documentation

Enter the ansible-navigator interactive mode on the terminal

$ ansible-navigator

screenshot of ansible-navigator: ansible-navigator interactive mode

In the above screenshot we can see a line for module or plugin documentation:

`:doc <plugin>`                 Review documentation for a module or plugin

Lets example the debug module by typing :doc debug

:doc debug

screenshot of ansible-navigator :doc debug: ansible-navigator interactive mode doc

The documentation for the debug module is now displayed in you interactive terminal session. This is a YAML representation of the same exact documentation you would see on docs.ansible.com. Examples can be cut and paste directly from the module documentation into your Ansible Playbook.

When referring to a non-built in module, there is three important fields:

namespace.collection.module

For example:

cisco.ios.facts

Explanation of terms:

Press the Esc key to return to the main menu. Try repeating the :doc command with the cisco.ios.facts module.

:doc cisco.ios.facts

We will be using the facts module in our playbook.

Step 2 - Creating the play

Ansible Playbooks are YAML files. YAML is a structured encoding format that is also extremely human readable (unlike it’s subset - the JSON format)

Create a new file in Visual Studio code: vscode new file

For simplicity please name the playbook: facts.yml: vscode save file

Enter the following play definition into facts.yml:

---
- name: Gather information from routers
  hosts: cisco
  gather_facts: no

Here is an explanation of each line:

Step 3 - Create the facts task

Next, add the first task. This task will use the cisco.ios.ios_facts module to gather facts about each device in the group cisco.

---
- name: Gather information from routers
  hosts: cisco
  gather_facts: false

  tasks:
    - name: Gather router facts
      cisco.ios.ios_facts:

Note:

A play is a list of tasks. Modules are pre-written code that perform the task.

Save the playbook.

Step 4 - Executing the playbook

Execute the Ansible Playbook by running ansible-navigator:

$ ansible-navigator run facts.yml

This will open an interactive session while the playbook interacts:

Screenshot of facts.yml: ansible-navigator run facts.yml

To zoom into the playbook output we can press 0 which will show us a host-centric view. Since there is only one host, there is just one option.

Screenshot of zooming in: ansible-navigator zoom hosts

To see the verbose output of rtr1 press 0 one more time to zoom into the module return values.

Screenshot of zooming into module data: ansible-navigator zoom module

You can scroll down to view any facts that were collected from the Cisco network device.

Step 5 - Using debug module

Write two additional tasks that display the routers’ OS version and serial number.

---
- name: Gather information from routers
  hosts: cisco
  gather_facts: false

  tasks:
    - name: Gather router facts
      cisco.ios.ios_facts:

    - name: Display version
      ansible.builtin.debug:
        msg: "The IOS version is: {{ ansible_net_version }}"

    - name: Display serial number
      ansible.builtin.debug:
        msg: "The serial number is:{{ ansible_net_serialnum }}"

Step 6 - Using stdout

Now re-run the playbook using the ansible-navigator and the --mode stdout

The full command is: ansible-navigator run facts.yml --mode stdout

Screenshot of ansible-navigator using stdout: ansible-navigator stdout screenshot

Using less than 20 lines of “code” you have just automated version and serial number collection. Imagine if you were running this against your production network! You have actionable data in hand that does not go out of date.

Takeaways

Solution

The finished Ansible Playbook is provided here for an answer key: facts.yml.

Complete

You have completed lab exercise 3


Previous Exercise | Next Exercise

Click here to return to the Ansible Network Automation Workshop