# Cisco CDP neighbor configure descriptions

**URL:** <https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783>\
**Category:** Get Help\
**Tags:** ios, cisco, playbook\
**Created:** [May 13, 2024, 5:21pm UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783 "2024-05-13T17:21:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shicks](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shicks](https://forum.ansible.com/u/shicks)\
**Post date:** [May 13, 2024, 5:21pm UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/1 "2024-05-13T17:21:08Z")

</div>

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.

```auto
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  

```

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [May 14, 2024, 2:36am UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/2 "2024-05-14T02:36:58Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![shicks](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shicks](https://forum.ansible.com/u/shicks)\
**Post date:** [May 14, 2024, 1:11pm UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/3 "2024-05-14T13:11:38Z")

</div>

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:

\\

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.

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [May 15, 2024, 7:34am UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/4 "2024-05-15T07:34:43Z")

</div>

@shicks Thank you for the details.

How about below tasks:

```yaml
- 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 }}"

```

---

<div class="post-metadata">

**Author:** ![shicks](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shicks](https://forum.ansible.com/u/shicks)\
**Post date:** [May 15, 2024, 12:59pm UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/5 "2024-05-15T12:59:24Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [May 15, 2024, 1:35pm UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/6 "2024-05-15T13:35:28Z")

</div>

@shicks

It seems indent of `loop` is wrong.

Please ensure.

```auto
- 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 }}"

```

---

<div class="post-metadata">

**Author:** ![shicks](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@shicks](https://forum.ansible.com/u/shicks)\
**Post date:** [May 15, 2024, 5:19pm UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/7 "2024-05-15T17:19:56Z")

</div>

That was it… Thank you so much for your help

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [May 16, 2024, 12:15am UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/8 "2024-05-16T00:15:06Z")

</div>

@shicks Your welcome 😀

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 15, 2024, 12:15am UTC](https://forum.ansible.com/t/cisco-cdp-neighbor-configure-descriptions/5783/9 "2024-06-15T00:15:27Z")

</div>

Closed on June 15, 2024, 12:15am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
