A module to get Ansible facts from SNMP devices - Feedback requested

Hi,

I’d like to get some feedback on an Ansible module I’ve written. It can target SNMP devices and collect information which it stores as Ansible facts.

There is some more information about the module here:
http://networklore.com/ansible-snmp-facts/

Specifically I would like to get suggestions about the structure of the data which is to be stored.

Best regards
Patrick

Hi Patrick,

SNMP can definitely be a beast.

I’d be interested if seeing if you could share an example of some of the output from the snmp module.

My other piece of advice would probably be to define constants for each of the MIB values, and see if there is an snmp library defines any of these (it might not).

I’d also move the import to the top of the file, and enclose it in a try/except ImportError block, so it can produce a nice error when something is not defined.

calls like “print json.dumps” can be replaced with the “fail_json” and “exit_json” calls you can see throughout AnsibleModule code.

You can also use the “required_together” feature of argument_spec (grep through the code) to simplify some of the argument checking.

Variables that are not constants, like SNMP_AUTH should be downcased (“snmp_auth”)

Additionally, some duplication like:

snmp_result[‘ansible_facts’]

throughout the system

Could be done just by having the final module return like so:

exit_json(ansible_facts=results)

As you can find done by ‘setup’, the most basic ansible facts module.

Hope that helps for starters

My other thought is different types of devices might not surface the same types of things, so it may be possible your module is written for certain network hardware – which might make the module not generally applicable? (i.e. is it always true that an SNMP thing has an IP address?)

Possibly something to think about.

Hi Michael,

Thank you for your comments! I’ll do some changes to the code. Great about the require_together feature! I’ll change the OID numbers to constants instead, for some of these basic values there are (or should be) some preloaded MIBs with pysnmp. However this could add another dependency and will be an issue if additional OIDs are to be polled.

I’m not quite sure what you mean about the snmp_result[‘ansible_facts’] comment, aren’t you doing the same thing in the setup module with “setup_result[‘ansible_facts’][k] = v”?

Concerning your thought about different devices you are correct SNMP support differs a lot. I’m not sure it’s that big of an issue, but I will move the facts assignments so that the code doesn’t try to assign something which isn’t there. What we will we however if you look at the output below is that some SNMP tables doesn’t exist on all devices. For example the ifXTable, which contain ifAlias (typically the description you set on a port in a switch or router), doesn’t exist on the printer I tested against. So under the interfaces for the printer the “description” key is missing. Would you see this as a problem?

For device specific information I’m thinking about adding arguments as in:
snmp_facts: host={{ inventory_hostname }} version=v2 community=public cdp=true (cdp=false would be default)

snmp_facts: host={{ inventory_hostname }} version=v2 community=public lldp=true (lldp=false would be default)

snmp_facts: host={{ inventory_hostname }} version=v2 community=public route_table=IP-FORWARD-MIB (options would be IP-FORWARD-MIB or RFC1213-MIB)

This could be split up into device specific modules too.

This is the playbook I’m currently using: