Cisco NXOS : Parsing SNMP trap hosts from command output

Hi Guys,

Can someone advise on the following?

I need to update (standardize) the SNMP trap receivers throughout the network. Given that i’m running Cisco NX OS on the 3500 platform I can’t delete the old SNMP trap receiver configuration without specifying the hosts. So I need to run a command and then parse the hosts from it. I’d like to then use the hosts in the returned output to delete the trap receivers before adding the new correct receivers.

If I understand this correctly, you want to run 2nd task (snmp update) based on the output of 1st task (get current snmp hosts).

One way to do this is to use ‘register’ keyword to store the output of 1st task and use ‘when’ conditional to run the second task.

  • name: get current snmp hosts
    register: command_output
    ios_command:
    commands: “show snmp host”
    provider: “{{ ios_provider }}”

register: result

  • name: snmp update
    ios_config:
    src: “./configs/snmp.conf”
    provider: “{{ ios_provider }}”
    match: none
    when: “‘<host’> in result.stdout[0]”

Hi Ganesh,

I think the first thing I’m trying to do is build a json object with the IP addresses of the currently configured trap receivers. So the first task is as follows:

vars:
trapdests: [] # I want to populate this list with currently configured trap receivers
ios_provider:
username: “{{ user }}”
password: “{{ password }}”
host: “{{ inventory_hostname }}”

tasks:

  • name: get current snmp hosts
    register: command_output
    ios_command:
    commands: “show snmp host”
    provider: “{{ ios_provider }}”

- debug: msg=“{{command_output.stdout_lines}}”

The next task is to extract the trap receivers from the command output (here is a sample of the command out - from the debug statement above)

ok: [lab-xxx-xx-xxxxx] => {
“msg”: [
[
“-------------------------------------------------------------------”,
"Host Port Version Level Type SecName ",
“-------------------------------------------------------------------”,
"10.50.74.38 162 v2c noauth trap mypub ",
“-------------------------------------------------------------------”,
"10.50.32.23 162 v2c noauth trap mypub ",
“Use VRF: management”,
“-------------------------------------------------------------------”,
"10.50.74.50 162 v2c noauth trap mypub ",
“Use VRF: management”,
“-------------------------------------------------------------------”,
"10.50.74.49 162 v2c noauth trap mypub ",
“Use VRF: management”,
“-------------------------------------------------------------------”,
"10.30.130.131 162 v2c noauth trap mypub ",
“Use VRF: management”,
“-------------------------------------------------------------------”,
"10.15.24.118 162 v2c noauth trap mypub ",
“Use VRF: management”,
“-------------------------------------------------------------------”,
"1.1.1.1 162 v2c noauth trap TESTING ",
“-------------------------------------------------------------------”
]
]
}

So what I’m trying to learn is how to use a regular expression to extract each IP address from this output , update the trapdests list with these addresses, and then use them to do a
no snmp-server traps version 2c mypub
no snmp-server use-vrf management

I hope that makes sense.

Just as a note - a similar approach is take here https://www.netnea.com/cms/2016/10/16/using-ansible-to-fetch-information-from-ios-devices/ , but that approach is on IOS so the command output is very different and I can’t seem to pass a regular expression to the cisco command from within Ansible to even get something similar.

Thanks

Gav

Hey Gavin,

Mine is a WIP, but I did a similar thing to replace v2 communities on IOS devices. Same problem there with needing to be specific.

Rather than doing anything with JSON, I directed stdout into a file, then used a python script to massage the results back into a jinja template. You may be able to tweak the regex used in the .py file and the playbook/modules a little to get what you need for trap receivers. I’m fairly new to Ansible, so I tried to make this as straightforward as possible. It’s probably considered a dirty approach to folks more experienced, so I hope my code/method doesn’t offend anyone. :slight_smile:

Link:
https://github.com/vPacketNinja/snmp-replace

Hopefully that is helpful for you.

Ryan