0

I am using ansible to gather snmp hosts configured on each of the cisco switch and also its version . i'm able to achieve to get the list but some how i'm unable to understand on how to really have the list with only items needed and remove all of those empty. Ansible code:

- name: Config check for domain name
  hosts: all
  gather_facts: false
  connection: network_cli

  tasks:
    - name: Get the show hosts command output
      cisco.ios.ios_command:
        commands:
          - show snmp host
      register: show_snmp_host_output

    - name: Regexing to get domain name
      ansible.builtin.set_fact:  
        snmp_hosts: |  
          {{ show_snmp_host_output.stdout_lines[0] |   
            map('regex_findall','(?<=Notification host: )(\S+)|(?<=security model: )(\S+)') |   
            map('join') | reject('search', '^$') | list }}

    - name: Print SNMP_HOSTS
      debug:
        var: snmp_hosts

    - name: Print snmp_data
      ansible.builtin.debug:
        var: snmp_hosts[0],snmp_hosts[1],snmp_hosts[2],snmp_hosts[3],snmp_hosts[4],snmp_hosts[5],snmp_hosts[6],snmp_hosts[7],snmp_hosts[8],snmp_hosts[9],snmp_hosts[10],snmp_hosts[11]

Result:

ok: [switch1] => {
    "snmp_hosts": [
        "('1.1.1.1', '')",
        "('', 'v3')",
        "('2.2.2.2', '')",
        "('', 'v1')",
        "('3.3.3.3', '')",
        "('', 'v1')",
        "('2.2.2.2', '')",
        "('', 'v2c')",
        "('3.3.3.3', '')",
        "('', 'v2c')",
        "('4.4.4.4', '')",
        "('', 'v1')"
    ]
}

TASK [Print snmp_data] *********************************************************************************************************************************
ok: [switch1] => {
    "snmp_hosts[0],snmp_hosts[1],snmp_hosts[2],snmp_hosts[3],snmp_hosts[4],snmp_hosts[5],snmp_hosts[6],snmp_hosts[7],snmp_hosts[8],snmp_hosts[9],snmp_hosts[10],snmp_hosts[11]": "(\"('1.1.1.1', '')\", \"('', 'v3')\", \"('2.2.2.2', '')\", \"('', 'v1')\", \"('3.3.3.3', '')\", \"('', 'v1')\", \"('2.2.2.2', '')\", \"('', 'v2c')\", \"('3.3.3.3', '')\", \"('', 'v2c')\", \"('4.4.4.4', '')\", \"('', 'v1')\")"
}

But what i'm looking is to get only the 1.1.1.1 when i use snmp_hosts[0] and v3 for snmp_hosts[1] and so on...

Right now when i print snmp_hosts[0] then i get all the other empty char

TASK [Print snmp_data] ***********************************************************************************************************************************
ok: [switch1] => {
    "snmp_hosts[0]": "('10.132.226.60', '')"
}
Yashas K.M
  • 47
  • 4

0 Answers0