使用Ansible进行Cisco IOS的信息收集 – snmp_facts

背景

使用Ansible来收集Cisco IOS的信息 – 这是ios_facts的续集。这次是记录snmp_facts。由于这是一个通用模块,而不是仅限于Cisco,所以无需考虑供应商意识,并且可以在希望使用MIB标准获取信息(但又不想使用snmpget等工具)的情况下使用。

※省略ansible.cfg和inventory的设置。
※环境与此处相同,因此省略。

播放书示例- snmp_facts

---
- hosts: ios
  gather_facts: False
  connection: local
  tasks:
    - name: snmp_facts
      snmp_facts:
        host: '{{ inventory_hostname }}'
        version: v2c
        community: public
      register: snmpfacts_out

    - debug: var=snmpfacts_out

例子执行

由於資訊量有限,我們只將調試輸出保存至文件,可以輕鬆進行網絡所有介面的調查。

[vagrant@localhost ansible]$ ansible-playbook snmp_facts.yml 

PLAY [ios] *********************************************************************

TASK [snmp_facts] **************************************************************
ok: [10.71.130.58]

TASK [debug] *******************************************************************
ok: [10.71.130.58] => {
    "snmpfacts_out": {
        "ansible_facts": {
            "ansible_all_ipv4_addresses": [
                "10.71.130.58"
            ], 
            "ansible_interfaces": {
                "1": {
                    "adminstatus": "up", 
                    "description": "", 
                    "ifindex": "1", 
                    "ipv4": [
                        {
                            "address": "10.71.130.58", 
                            "netmask": "255.255.248.0"
                        }
                    ], 
                    "mac": "005056a26a84", 
                    "mtu": "1500", 
                    "name": "GigabitEthernet1", 
                    "operstatus": "up", 
                    "speed": "1500"
                }, 
                "2": {
                    "adminstatus": "down", 
                    "description": "", 
                    "ifindex": "2", 
                    "mac": "005056a2aa72", 
                    "mtu": "1500", 
                    "name": "GigabitEthernet2", 
                    "operstatus": "down", 
                    "speed": "1500"
                }, 
                "3": {
                    "adminstatus": "down", 
                    "description": "", 
                    "ifindex": "3", 
                    "mac": "005056a25883", 
                    "mtu": "1500", 
                    "name": "GigabitEthernet3", 
                    "operstatus": "down", 
                    "speed": "1500"
                }, 
                "4": {
                    "adminstatus": "up", 
                    "description": "", 
                    "ifindex": "4", 
                    "mac": "", 
                    "mtu": "1500", 
                    "name": "VoIP-Null0", 
                    "operstatus": "up", 
                    "speed": "1500"
                }, 
                "5": {
                    "adminstatus": "up", 
                    "description": "", 
                    "ifindex": "5", 
                    "mac": "", 
                    "mtu": "1500", 
                    "name": "Null0", 
                    "operstatus": "up", 
                    "speed": "1500"
                }, 
                "6": {
                    "adminstatus": "up", 
                    "description": "", 
                    "ifindex": "6", 
                    "mac": "", 
                    "mtu": "1514", 
                    "name": "Loopback0", 
                    "operstatus": "up", 
                    "speed": "1514"
                }
            }, 
            "ansible_syscontact": "", 
            "ansible_sysdescr": "Cisco IOS Software [Everest], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.4.1, RELEASE SOFTWARE (fc2)\r\nTechnical Support: http://www.cisco.com/techsupport\r\nCopyright (c) 1986-2016 by Cisco Systems, Inc.\r\nCompiled Sun 27-Nov-16 13:02 by", 
            "ansible_syslocation": "", 
            "ansible_sysname": "CSR-2.solse.local", 
            "ansible_sysobjectid": "1.3.6.1.4.1.9.1.1537", 
            "ansible_sysuptime": "28502267"
        }, 
        "changed": false
    }
}

PLAY RECAP *********************************************************************
10.71.130.58               : ok=2    changed=0    unreachable=0    failed=0  

Playbook样本2 – snmp_facts

这个设备的sysoid是什么来着?(但是不想使用snmpwalk)。

---
- hosts: ios
  gather_facts: False
  connection: local
  tasks:
    - name: snmp_facts
      snmp_facts:
        host: '{{ inventory_hostname }}'
        version: v2c
        community: public
      register: snmpfacts_out

    - debug: var=snmpfacts_out["ansible_facts"]["ansible_sysobjectid"]

执行示例 lì)

[vagrant@localhost ansible]$ ansible-playbook snmp_facts_2.yml 

PLAY [ios] *********************************************************************

TASK [snmp_facts] **************************************************************
ok: [10.71.130.58]

TASK [debug] *******************************************************************
ok: [10.71.130.58] => {
    "snmpfacts_out[\"ansible_facts\"][\"ansible_sysobjectid\"]": "1.3.6.1.4.1.9.1.1537"
}

PLAY RECAP *********************************************************************
10.71.130.58               : ok=2    changed=0    unreachable=0    failed=0 

以上。可能做的事情可能有点少。辛苦了。

bannerAds