Creating the Ansible Playbook

  • Network Resource Modules

    config to code

Resource Modules

Ansible network resource modules simplify and standardize how you manage different network devices. Network devices separate configuration into sections (such as interfaces and VLANs) that apply to a network service.

Network resource modules provide a consistent experience across different network devices. This means you will get an identical experience across multiple vendors. For example the snmp_server module will work identically for the following modules:

  • arista.eos.snmp_server

  • cisco.ios.snmp_server

  • cisco.nxos.snmp_server

  • cisco.iosxr.snmp_server

  • junipernetworks.junos.snmp_server

Why use SNMP as an example?

Configuring SNMP on network devices is an extremely common task, and mis-configurations can cause headaches and monitoring issues. SNMP configurations also tend to be identical across multiple network switches resulting in a perfect use case for automation.

This exercise will cover:

  • Configuring SNMP on Cisco IOS

  • Building an Ansible Playbook using the cisco.ios.snmp_server module.

  • Understanding the state: merged

  • Understanding the state: gathered

Lets get started

That is the end of of your lab briefing!

Step 1 - 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)

  • Click on the code-server tab

  • Create a new file in Visual Studio code. Click on the File Top Nav bar then click New File

  • For simplicity please name the playbook: resource.yml

  • Enter the following play definition into resource.yml:

    ---
    - name: configure SNMP
      hosts: cisco
      gather_facts: false
      tasks:
    
        - name: use snmp resource module
          cisco.ios.snmp_server:
            state: merged
            config:
              location: 'Durham'
              packet_size: 500
              communities:
                - acl_v4: acl_uq
                  name: Durham-community
                  rw: true
                - acl_v4: acl_uq
                  name: ChapelHill-community
                  rw: true

Step 2 - Examine the Ansible Playbook

  • First lets examine the first four lines:

    ---
    - name: configure SNMP
      hosts: cisco
      gather_facts: false
    • The --- designates this is a YAML file which is what we write playbooks in.

    • name is the description of what this playbook does.

    • hosts: cisco will execute this playbook only on the Cisco network devices. cisco is a group name.

    • gather_facts: false this will disable fact gathering for this play, by default this is turned on.

  • For the second part we have one task that uses the cisco.ios.snmp_server

      tasks:
    
      - name: use snmp resource module
        cisco.ios.snmp_server:
          state: merged
          config:
            location: 'Durham'
            packet_size: 500
            communities:
              - acl_v4: acl_uq
                name: Durham-community
                rw: true
              - acl_v4: acl_uq
                name: ChapelHill-community
                rw: true
    • name: - just like the play, each task has a description for that particular task

    • state: merged - This is the default behavior of resource modules. This will simply enforce that the supplied configuration exists on the network device. There is actually seven parameters possible for resource modules:

      • merged

      • replaced

      • overridden

      • deleted

      • rendered

      • gathered

      • parsed

      Only two of these state parameters will be covered in this exercise. You can get additional details on the other state parameters with our exercise walkthrough here or read the documentation.

    • config: - this is the supplied SNMP configuration. It is a list of dictionaries. The most important takeaway is that if the module was change from cisco.ios.snmp_server to junipernetworks.junos.snmp_server it would work identically. This allows network engineers to focus on the network (e.g. SNMP configuration) versus the vendor syntax and implementation.

  • If you have not already, please save the playbook for the next exercise.

Complete

You have completed challenge 2!