Ansible facts are metadata information of managed node to which we connect. A fact can be hostname, OS family, OS Version,  IP address, and generally system’s software and hardware information. Ansible facts help the admin not only to store inventory information of the infrastructure but also to manage managed nodes based on their metadata information. For example, you might want to configure BGP of your gateway routers, but the configuration will be different depending on the operating system family of the device if it is IOS or IOS XR. or you want to configure EIGRP, but the router ID and network addresses differ depending on the device name. Using ansible_facts, we can collect metadata information from managed nodes and use the when condition to apply the configuration based on these information. “ansible_facts” and ansible “when” conditions is the topic this Video.

access ansible codes of this course in github

this is a video-based training therefore the output of running commands are not show in the text.

I have already prepared an ansible playbook called ansible_facts.yaml. In the header of every play we can activate the command “ansible_facts” with the true value. With this command, we are asking ansible to collect metadata information from each node it connects to. You can easily print the metadata information using debug command which I have added as first task in this playbook.

root@debian:~/ansible-project1# cat ansible_facts.yaml
---
- hosts: csr
  gather_facts: true
  connection: local

  tasks:
    - name: print ansible facts
      debug:
        msg:
#          - "{{ ansible_facts }}"
          - "{{ ansible_facts.net_iostype }}"
          - "{{ ansible_facts.net_hostname }}"
          - "{{ ansible_facts.net_all_ipv4_addresses }}"
          - "{{ ansible_facts.net_interfaces.GigabitEthernet1.ipv4 }}"

    - name:  config eigrp on csr1
      ios_config:
        lines:
          - router-id 1.1.1.1
          - network 0.0.0.0
        parents: router eigrp 1
      when: (ansible_facts.net_iostype == "IOS-XE" and ansible_facts.net_hostname == "csr1")

    - name:  config eigrp on csr2
      ios_config:
        lines:
          - router-id 2.2.2.2
          - network 0.0.0.0
        parents: router eigrp 1
      when: (ansible_facts.net_iostype == "IOS-XE" and ansible_facts.net_hostname == "csr2")

I will run the playbook to see the output of the information collected. Metadata information has a hierarchical dictionary structure with “key:value” fields. For example you can extract the IP address of the managed node using “net_all_ipv4_addresses” key, the host name of managed nodes using “net_hostname” and OS family with “net_iostype” key. the IP address of the interface “GigabitEthernet1” can be extracted with “net_interfaces.GigabitEthernet1.ipv4 ” in the output of ansible_facts structure. Let’s limit the output of ansible_facts to these four keys only.

# ansible-playbook ansible_facts.yaml

An Introduction to ansible Facts in ansible website

Ansible conditional Tasks in ansible website

Now is the time to configure managed nodes based on the output of the ansible facts. As an example I will configure EIGRP on csr1 and csr2 and the only difference in configuration is the router id which is 1.1.1.1 in csr1 and 2.2.2.2 in csr2. However, the best solution for configuring such a scenario is to use the jinja2 template, which we will cover in the next few videos. In this video I want to configure EIGRP on these two devices based on “logical and” of two conditions: OS family that must be “IOS-XE” and hostname. You can use “logical and”, “logical or” or a mixture of these when you need to check multiple conditions.

 

Back to: Ansible for Network Engineers > Ansible Progamming Features

Leave a Reply

Your email address will not be published. Required fields are marked *


Post comment