Bookmark this page

Guided Exercise: Gathering Facts from Managed Network Devices

Gather Ansible facts from managed network nodes to create a report of the operational state of the network.

Outcomes

  • Create and run playbooks to gather, display, and store facts from managed network nodes.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise, and to ensure that all required resources are available. This command also creates a project directory with the files needed for the exercise.

[student@workstation ~]$ lab start manage-facts

Instructions

  1. Open the /home/student/manage-facts directory in VS Code and create a playbook file.

    1. Open VS Code and navigate to FileOpen Folder.

    2. Navigate to Homemanage-facts and click Open.

      Note

      If prompted, select Trust the authors of all files in the parent folder 'student', and then click Yes, I trust the authors.

    3. Click FileNew Text File.

    4. Click Select a language and then click Ansible (ansible).

  2. In VS Code, create a playbook in the ~/manage-facts directory to gather the default set of facts from managed nodes. Save the file as minfacts.yml, and then run the playbook.

    1. In VS Code, create a playbook to gather all facts from managed nodes.

      Save the file as minfacts.yml.

      The completed playbook must consist of the following content:

      ---
      - name: Gather and display facts
        hosts: ios,junos
        gather_facts: true
        tasks:
          - name: Display facts
            ansible.builtin.debug:
              var: ansible_facts
    2. Switch to the Terminal tab in VS Code, or change to the /home/student/manage-facts directory in a GNOME terminal.

      Use the ansible-navigator run command to run the minfacts.yml playbook:

      [student@workstation ~] cd ~/manage-facts
      [student@workstation manage-facts]$ ansible-navigator run minfacts.yml
      
      PLAY [Gather and display facts] ***********************************************
      
      TASK [Gathering Facts] ********************************************************
      ok: [iosxe2.lab.example.com]
      ok: [iosxe1.lab.example.com]
      ok: [junos2.lab.example.com]
      ok: [junos1.lab.example.com]
      
      TASK [Display facts] *********************************************************
      ok: [junos1.lab.example.com] => {
          "ansible_facts": {
              "net_api": "netconf",
              "net_gather_network_resources": [],
              "net_gather_subset": [
                  "default"
              ],
              "net_hostname": "junos1.lab.example.com",
              "net_model": "vmx",
              "net_python_version": "3.9.13",
              "net_serialnum": "VM646555B74D",
              "net_system": "junos",
              "net_version": "23.1R1.8",
              "network_resources": {}
          }
      }
      ok: [junos2.lab.example.com] => {
          "ansible_facts": {
              "net_api": "netconf",
      ...output omitted...
      
      PLAY RECAP ********************************************************************
      iosxe1.lab.example.com     : ok=2    changed=0    unreachable=0    failed=0 ...
      iosxe2.lab.example.com     : ok=2    changed=0    unreachable=0    failed=0 ...
      junos1.lab.example.com     : ok=2    changed=0    unreachable=0    failed=0 ...
      junos2.lab.example.com     : ok=2    changed=0    unreachable=0    failed=0 ...
  3. Notice that in the previous step, the playbook only gathered a minimal set of facts for the managed nodes. In VS Code, create a playbook to gather all facts from managed nodes.

    Save the file as allfacts.yml, run the playbook, then analyze the results.

    1. In VS Code, create a playbook to gather all facts from managed nodes.

      Save the file as allfacts.yml.

      Note

      This playbook uses the run_once keyword in the first task.

      When you set the run_once keyword to true, the task executes on the first available managed node and, if successful, does not execute on subsequent managed nodes.

      Because this task creates a directory, the task does not need to run again if the directory is already created.

      The completed playbook must consist of the following content:

      ---
      - name: Gather and display facts for managed nodes
        hosts: ios,junos
        gather_facts: false
        tasks:
          - name: Create the facts directory if it does not exist
            ansible.builtin.file:
              path: facts
              state: directory
            run_once: true
      
          - name: Gather IOS facts
            cisco.ios.ios_facts:
              gather_subset:
                - all
            when: ansible_network_os == 'cisco.ios.ios'
      
          - name: Gather Junos facts
            junipernetworks.junos.junos_facts:
              gather_subset:
                - all
            when: ansible_network_os == 'junipernetworks.junos.junos'
      
          - name: Save facts to file
            ansible.builtin.copy:
              content: "{{ ansible_facts | to_nice_yaml }}"
              dest: "facts/{{ inventory_hostname }}.txt"
    2. Use the ansible-navigator run command to run the allfacts.yml playbook:

      [student@workstation manage-facts]$ ansible-navigator run allfacts.yml
      
      PLAY [Gather and display facts for managed nodes] *****************************
      
      TASK [Create the facts directory if it does not exist] ************************
      changed: [iosxe1.lab.example.com]
      
      TASK [Gather IOS facts] *******************************************************
      skipping: [junos1.lab.example.com]
      skipping: [junos2.lab.example.com]
      ok: [iosxe1.lab.example.com]
      ok: [iosxe2.lab.example.com]
      
      TASK [Gather Junos facts] *****************************************************
      skipping: [iosxe1.lab.example.com]
      skipping: [iosxe2.lab.example.com]
      ok: [junos2.lab.example.com]
      ok: [junos1.lab.example.com]
      
      TASK [Save facts to file] *****************************************************
      changed: [junos2.lab.example.com]
      changed: [iosxe2.lab.example.com]
      changed: [iosxe1.lab.example.com]
      changed: [junos1.lab.example.com]
      
      PLAY RECAP ********************************************************************
      iosxe1.lab.example.com     : ok=3    changed=2    unreachable=0    failed=0 ...
      iosxe2.lab.example.com     : ok=2    changed=1    unreachable=0    failed=0 ...
      junos1.lab.example.com     : ok=2    changed=1    unreachable=0    failed=0 ...
      junos2.lab.example.com     : ok=2    changed=1    unreachable=0    failed=0 ...
    3. Analyze the created facts files to verify that all facts were gathered for each managed node. Display the first five lines of each file:

      [student@workstation manage-facts]$ head -n5 facts/iosxe1.lab.example.com.txt
      net_all_ipv4_addresses:
      - 172.25.250.20
      net_all_ipv6_addresses: []
      net_api: cliconf
      net_config: "Building configuration...\n\nCurrent configuration : 6063 bytes\n!\n!
      [student@workstation manage-facts]$ head -n5 facts/iosxe2.lab.example.com.txt
      net_all_ipv4_addresses:
      - 172.25.250.21
      net_all_ipv6_addresses: []
      net_api: cliconf
      net_config: "Building configuration...\n\nCurrent configuration : 6063 bytes\n!\n!
      [student@workstation manage-facts]$ head -n5 facts/junos1.lab.example.com.txt
      net_api: netconf
      net_config: "## Last changed: 2023-07-12 21:04:00 UTC\nversion 23.1R1.8;\nsystem {\n
          \   host-name junos1.lab.example.com;\n    root-authentication {\n        encrypted-password
          \"$6$5TiPJaqE$929rhpem1O3HigTCPHZTuyg4wV.IeFK2ZFWBOQG9syw.AS50/FEHTiXmzJ1sB5ZPppRKXilY8.gXQW4e8z.yP.\";\n
          \   }\n    login {\n        user student {\n            uid 2000;\n            class
      [student@workstation manage-facts]$ head -n5 facts/junos2.lab.example.com.txt
      net_api: netconf
      net_config: "## Last changed: 2023-07-12 16:21:42 UTC\nversion 23.1R1.8;\nsystem {\n
          \   host-name junos2.lab.example.com;\n    root-authentication {\n        encrypted-password
          \"$6$5TiPJaqE$929rhpem1O3HigTCPHZTuyg4wV.IeFK2ZFWBOQG9syw.AS50/FEHTiXmzJ1sB5ZPppRKXilY8.gXQW4e8z.yP.\";\n
          \   }\n    login {\n        user student {\n            uid 2000;\n            class
  4. In VS Code, create a playbook in the ~/manage-facts directory to gather facts in resource module format using the gather_network_resources parameter.

    Save the file as resourcefacts.yml, and then run the playbook.

    1. In VS Code, create a playbook to gather facts in resource module format using the gather_network_resources parameter.

      Save the file as resourcefacts.yml.

      The completed playbook must consist of the following content:

      ---
      - name: Gather and display facts for IOS managed nodes
        hosts: ios
        gather_facts: false
        tasks:
          - name: Gather IOS facts
            cisco.ios.ios_facts:
              gather_network_resources:
                - all
      
          - name: Display facts
            ansible.builtin.debug:
              var: ansible_network_resources
    2. Use the ansible-navigator run command to run the resourcefacts.yml playbook:

      [student@workstation manage-facts]$ ansible-navigator run resourcefacts.yml
      
      PLAY [Gather and display facts for IOS managed nodes] *************************
      
      TASK [Gather IOS facts] *******************************************************
      ok: [iosxe2.lab.example.com]
      ok: [iosxe1.lab.example.com]
      
      TASK [Display facts] **********************************************************
      ok: [iosxe1.lab.example.com] => {
          "ansible_network_resources": {
              "acl_interfaces": [],
              "acls": [
                  {
                      "acls": [
                          {
                              "acl_type": "extended",
                              "name": "meraki-fqdn-dns"
                          }
                      ],
                      "afi": "ipv4"
                  }
              ],
              "bgp_address_family": {},
              "bgp_global": {},
              "hostname": {
                  "hostname": "iosxe1.lab.example.com"
              },
              "interfaces": [
      ...output omitted...
  5. In VS Code, create a playbook in the ~/manage-facts directory that creates a report of models, IOS versions, hostnames, VLANs, and Layer 3 interfaces on the IOS XE managed nodes. Save the file as factsreport.yml, and then run the playbook.

    1. Create the factsreport.yml playbook with the following content:

      ---
      - name: Gather and display facts report for IOS managed nodes
        hosts: ios
        gather_facts: false
        tasks:
          - name: Gather IOS facts
            cisco.ios.ios_facts:
              gather_subset:
                - min
              gather_network_resources:
                - vlans
                - l3_interfaces
      
          - name: Save facts to file
            ansible.builtin.copy:
              content: |
                Hostname: {{ ansible_facts['net_hostname'] }}
                Model: {{ ansible_facts['net_model'] }}
                Version: {{ ansible_facts['net_version'] }}
                {{ ansible_network_resources | to_nice_yaml(indent=2) }}
              dest: "facts/{{ inventory_hostname }}.report"
    2. Use the ansible-navigator run command to run the factsreport.yml playbook, and then view the generated report for the iosxe1.lab.example.com managed node.

      [student@workstation manage-facts]$ ansible-navigator run factsreport.yml
      ...output omitted...
      [student@workstation manage-facts]$ cat facts/iosxe1.lab.example.com.report
      Hostname: iosxe1.lab.example.com
      Model: C8000V
      Version: 17.06.02
      l3_interfaces:
      - ipv4:
        - address: 172.25.250.20/24
        name: GigabitEthernet1
      - name: GigabitEthernet2
      vlans:
      ...output omitted...
  6. Click FileClose Folder in VS Code to close the /home/student/manage-facts directory.

  7. If you are using the GNOME terminal, run the cd command to return to the student home directory:

    [student@workstation manage-facts]$ cd

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish manage-facts

Revision: do457-2.3-7cfa22a