Using the gathered parameter

  • Learn how to use the gathered parameter with a resource module

    state gathered picture
  • Example of state gathered

        - name: use SNMP resource module
          cisco.ios.snmp_server:
            state: gathered
          register: snmp_config

    We are reading in the configuration with the state: gathered (versus applying it to the network device), and we are using the register to save the output from the module into a variable named snmp_config

Lets get started

That is the end of of your lab briefing!

Step 1 - Create the gathered playbook

  • Create a new playbook named gathered.yml in the code-server tab.

    (You may need to drag the instruction pane to be larger)

    ---
    - name: retrieve SNMP config
      hosts: cisco
      gather_facts: false
      tasks:
    
        - name: use SNMP resource module
          cisco.ios.snmp_server:
            state: gathered
          register: snmp_config
    
        - name: copy snmp_config to file
          copy:
            content: "{{ snmp_config | to_nice_yaml }}"
            dest: "{{ playbook_dir }}/{{ inventory_hostname }}_snmp.yml"
    • The first task is identical except the state: merged has been switched to gathered, the config is no longer needed since we are reading in the configuration (versus applying it to the network device), and we are using the register to save the output from the module into a variable named snmp_config

    • The second task is copying the snmp_config variable to a flat-file. The double curly brackets denotes that this is a variable.

    • The | to_nice_yaml is a filter, that will transform the JSON output (default) to YAML.

    • The playbook_dir and inventory_hostname are special variables also referred to as magic variables. The playbook_dir simply means the directory we executed the playbook from, and the inventory_hostname is the name of the device in our inventory. This means the file will be saved as /home/rhel/cisco_snmp.yml for the cisco device.

Step 2 - Execute the gathered playbook

  • Execute the playbook using the ansible-navigator run. Go to the terminal tab to run the ansible-navigator run command.

    ansible-navigator run gathered.yml --mode stdout
    picture of output
    Figure 1. The output will look similar to the following:

Step 3 - Examine the files

  • Open the newly created files that gathered the SNMP configuration from the Cisco network device(s).

    The files were stored under the device name: ~/cisco_snmp.yml.

Takeaways

  • Resource modules have a simple data structure that can be transformed to the network device syntax. In this case the SNMP dictionary is transformed into the Cisco IOS-XE network device syntax.

  • Resource modules are idempotent, and can be configured to check device state.

  • Resource Modules are bi-directional, meaning that they can gather facts for that specific resource, as well as, apply configuration. Even if you are not using resource modules to configure network devices, there is a lot of value for checking resource states.

  • The bi-directional behavior also allows brown-field networks (existing networks) to quickly turn their running-configuration into structured data. This allows network engineers to get automation up running more quickly and get quick automation victories.