Cisco CDP neighbor configure descriptions

I am trying to use the cdp neighbor information gathered in cisco.ios.ios_facts to configure the descriptions on the interfaces that are relevant to the neighbors. in the following code, I can iterate through the interfaces with neighbors on them just fine, but I cannot get the descriptions to parse through the “hosts” that are given in the output. I can reference each member of the list, or put every member of the list on each line, but I am having trouble getting the hosts/interfaces to line up. Much of the following code is filled with my attempts to understand what output I get with each debug statement.

code
---

 l
PLAY [Update Interface Descriptions] ********************************************************************************************************************************************************************************************************

TASK [Gather Facts] *************************************************************************************************************************************************************************************************************************
ok: [10.140.1.92]

TASK [set_fact] *****************************************************************************************************************************************************************************************************************************
ok: [10.140.1.92]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [10.140.1.92] => (item=GigabitEthernet0/1) => {
    "msg": "GigabitEthernet0/1"
}
ok: [10.140.1.92] => (item=GigabitEthernet0/14) => {
    "msg": "GigabitEthernet0/14"
}
ok: [10.140.1.92] => (item=GigabitEthernet0/9) => {
    "msg": "GigabitEthernet0/9"
}

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [10.140.1.92] => (item=[{'host': 'SEPDCEB94BC2486', 'platform': 'Cisco IP Phone 8811', 'port': 'Port 1', 'ip': '10.140.50.45'}]) => {
    "msg": [
        [
            {
                "host": "SEPDCEB94BC2486",
                "ip": "10.140.50.45",
                "platform": "Cisco IP Phone 8811",
                "port": "Port 1"
            }
        ]
    ]
}
ok: [10.140.1.92] => (item=[{'host': 'USACRSSRFSW090.newmont.net', 'platform': 'cisco WS-C4506-E', 'port': 'GigabitEthernet6/41', 'ip': '10.140.253.252'}]) => {
    "msg": [
        [
            {
                "host": "USACRSSRFSW090.newmont.net",
                "ip": "10.140.253.252",
                "platform": "cisco WS-C4506-E",
                "port": "GigabitEthernet6/41"
            }
        ]
    ]
}
ok: [10.140.1.92] => (item=[{'host': 'GQ_NET_Office', 'platform': 'cisco C9130AXI-B', 'port': 'GigabitEthernet0', 'ip': '10.140.14.87'}]) => {
    "msg": [
        [
            {
                "host": "GQ_NET_Office",
                "ip": "10.140.14.87",
                "platform": "cisco C9130AXI-B",
                "port": "GigabitEthernet0"
            }
        ]
    ]
}

TASK [Merge provided configuration with device configuration] *******************************************************************************************************************************************************************************
ok: [10.140.1.92] => (item={'msg': 'GigabitEthernet0/1', 'failed': False, 'changed': False, 'item': 'GigabitEthernet0/1', 'ansible_loop_var': 'item'})
ok: [10.140.1.92] => (item={'msg': 'GigabitEthernet0/14', 'failed': False, 'changed': False, 'item': 'GigabitEthernet0/14', 'ansible_loop_var': 'item'})
ok: [10.140.1.92] => (item={'msg': 'GigabitEthernet0/9', 'failed': False, 'changed': False, 'item': 'GigabitEthernet0/9', 'ansible_loop_var': 'item'})

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
10.140.1.92                : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

@shicks Welcome to community!

Cloud you show us the playbook you wrote?

And,

I am having trouble getting the hosts/interfaces to line up

Please tell us example output structure you desire.

Here is a cleaned up version of the mess I had created earlier. What I am trying to accomplish is the following:

I want to gather cdp neighbor information, then write that cdp neighbor info in the description of the corresponding interface.

\\


  • name: CDP Neighbor information
    hosts: trial
    tasks:

    • name: Gather Facts
      cisco.ios.ios_facts:
      gather_subset:
      - all
      gather_network_resources:
      - interfaces

    • debug:
      msg: “{{ansible_net_neighbors}}”
      register: neighbor_data

    • debug:
      msg:
      - “Interface: {{ item.key }}”
      - " Neighbor Host: {{ item.value[0].host }}"
      - " Neighbor IP: {{ item.value[0].ip }}"
      loop: “{{ neighbor_data.msg[0].items() }}”

\\

Output:

PLAY [CDP Neighbor information] ******************************************************************************************************

TASK [Gather Facts] ******************************************************************************************************************
ok: [10.140.1.92]

TASK [debug] *************************************************************************************************************************
ok: [10.140.1.92] => {
“msg”: {
“GigabitEthernet0/1”: [
{
“host”: “SEPDCEB94BC2486”,
“ip”: “10.140.50.45”,
“platform”: “Cisco IP Phone 8811”,
“port”: “Port 1”
}
],
“GigabitEthernet0/14”: [
{
“host”: “USACRSSRFSW090”,
“ip”: “10.140.253.252”,
“platform”: “cisco WS-C4506-E”,
“port”: “GigabitEthernet6/41”
}
],
“GigabitEthernet0/9”: [
{
“host”: “GQ_NET_Office”,
“ip”: “10.140.14.87”,
“platform”: “cisco C9130AXI-B”,
“port”: “GigabitEthernet0”
}
]
}
}

TASK [debug] *************************************************************************************************************************
fatal: [10.140.1.92]: FAILED! => {“msg”: “dict object has no element 0. dict object has no element 0”}

PLAY RECAP ***************************************************************************************************************************
10.140.1.92 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
\\

I cannot figure out how to iterate through each interface AND assign the descriptions accordingly. I can iterate through the interfaces, but wind up either putting ALL of the neighbors in all of the descriptions, OR putting a select neighbor on all of the descriptions.

I see it print out beautifully in the first debug, but I can’t find the way to access that and use it elsewhere in the playbook.

@shicks Thank you for the details.

How about below tasks:

- name: CDP Neighbor information
  hosts: trial

  tasks:
    - name: Gather Facts
      cisco.ios.ios_facts:
        gather_subset:
          - all
        gather_network_resources:
          - interfaces

    - name: Configure interfaces
      cisco.ios.ios_interfaces:
        config:
          - name: "{{ item.key }}"
            description: "to {{ item.value[0].host }} {{ item.value[0].ip }}"  # customizable
      loop: "{{ ansible_net_neighbors | dict2items }}"

I have tried similar formats, and I tried the above this morning.

I keep getting a “The error was: ‘item’ is undefined.” message.
I have tried several iterations of this and get the same “item is undefined error.” I have manipulated this code many times, and get close, but can’t seem to get this across the finish line.

- name: Configure interfaces
  cisco.ios.ios_interfaces:
    config:
      - name: "{{ item.key }}"
        description: "to {{ item.value[0].host }} {{ item.value[0].ip }}"  # customizable
    loop: "{{ ansible_net_neighbors | dict2items }}"

the problem seems to be here somewhere, but I don’t know how to fix it…

@shicks

It seems indent of loop is wrong.

Please ensure.

- name: Configure interfaces
  cisco.ios.ios_interfaces:
    config:
      - name: "{{ item.key }}"
        description: "to {{ item.value[0].host }} {{ item.value[0].ip }}"  # customizable
  loop: "{{ ansible_net_neighbors | dict2items }}"

That was it… Thank you so much for your help

@shicks Your welcome :grinning:

Could you mark my comment as solution for explicitly state that it has been resolved ?