# Ansible to connect to certain switchports on Cisco 2960

**URL:** <https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221>\
**Category:** Ansible Project\
**Created:** [June 20, 2020, 3:04am UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221 "2020-06-20T03:04:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason2](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@Jason2](https://forum.ansible.com/u/Jason2)\
**Post date:** [June 20, 2020, 3:04am UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/1 "2020-06-20T03:04:55Z")

</div>

I am working on a project that would require me to bump (shutdown and re-enable) switchports that are connected to an Access Point.

I have created a playbook to run a command “show cdp neighbours” on the switch, and that output would look like:

 ![sh cdp neighbours.JPG](https://us1.discourse-cdn.com/ansible/original/2X/9/97b173088e9402e734a0c2a613f828b796ef3478.jpeg)

What I want to do, is to use ansible to only run a command (ansible play) on Gig 1/0/19 in this case. For other switches, it may be more switchports and maybe in different format (eg. Gi 2/0/35).

Is there a way to filter ansible output by looking for ‘AIR-AP280’ and then extract that corresponding Local Interface (switchport number)?

Or if there is another way to get done what I want, then great.

Any suggestions/recommendations?

It is over 50 switches, so it would be great if ansible can do what I want to do.

---

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [June 20, 2020, 3:22am UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/2 "2020-06-20T03:22:03Z")

</div>

I would use the genie module and run the show cdp neighbor command. Then set fact and use that with the when command.

> **(attachments)**
>
> ![sh cdp neighbours.JPG](https://us1.discourse-cdn.com/ansible/original/2X/9/97b173088e9402e734a0c2a613f828b796ef3478.jpeg)

---

<div class="post-metadata">

**Author:** ![Jason2](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@Jason2](https://forum.ansible.com/u/Jason2)\
**Post date:** [June 20, 2020, 6:34pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/3 "2020-06-20T18:34:10Z")

</div>

Thanks. If you don’t mind me asking because I am not familiar with this, can you give me an example of how to use the set fact and when statement for my case?

---

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [June 22, 2020, 1:05pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/4 "2020-06-22T13:05:55Z")

</div>

Something like that I suppose.

#Sets a varaiable for cdp neighbor detail

- name: Run show cdp neighbors command  
ios\_command:  
commands:
- show cdp neighbors detail  
register: neighbors

# Using the Genie Filter Plugin from the Genie Parse Role

# Set pyats\_neighbors as a dictionary

- name: Set fact with Genie Filter Plugin  
set\_fact:  
pyats\_neighbors: “{{ neighbors[‘stdout’][0] | clay584.genie.parse\_genie(command=‘show cdp neighbors detail’, os=‘ios’) }}”

- name: Add interface desription  
ios\_interfaces:  
config:

- name: “{{ item.value.local\_interface }}”  
description: “{{ item.value.device\_id }} - {{ item.value.port\_id }}”  
state: merged  
loop: “{{ q(‘dict’, pyats\_neighbors.index) }}”

when: item.value.port\_id is “AIR-AP280”

---

<div class="post-metadata">

**Author:** ![Jason2](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@Jason2](https://forum.ansible.com/u/Jason2)\
**Post date:** [June 22, 2020, 7:44pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/5 "2020-06-22T19:44:21Z")

</div>

Thanks alot.

I have been getting this error for the past few days when trying to use genie:

fatal: [SW1]: FAILED! =\> {  
“msg”: “template error while templating string: no filter named ‘clay584.genie.parse\_genie’. String: {{ neighbors[‘stdout’][0] | clay584.genie.parse\_genie(command=‘show cdp neighbors’, os=‘ios’) }}”  
}

**This is my playbook (what you sent but I needed to import the role first):**

- hosts: uwioc-switches  
gather\_facts: no  
tasks:

- name: Import role  
include\_role:  
name: clay584.parse\_genie

- name: Run show cdp neighbors  
ios\_command:  
commands:

- show cdp neighbors  
register: neighbors

# Using the Genie Filter Plugin from the Genie Parse Role

# Set pyats\_neighbors as a dictionary

- name: Set fact with Genie

- name: Import role  
include\_role:  
name: clay584.parse\_genie

- name: Run show cdp neighbors  
ios\_command:  
commands:

- show cdp neighbors  
register: neighbors

# Using the Genie Filter Plugin from the Genie Parse Role

# Set pyats\_neighbors as a dictionary

- name: Set fact with Genie Filter Plugin  
set\_fact:  
pyats\_neighbors: “{{ neighbors[‘stdout’][0] | clay584.genie.parse\_genie(command=‘show cdp neighbors’, os=‘ios’) }}”

- name: Add interface description  
ios\_interfaces:  
config:

- name: “{{ item.value.local\_interface }}”  
description: “{{ item.value.device\_id }} - {{ item.value.port\_id }}”  
state: merged  
loop: “{{ q(‘dict’, pyats\_neighbors.index) }}”  
when: item.value.port\_id is “AIR-AP280”

My python version is 3.6.8 and Ansible is 2.9.9

I followed this guide to install the necessary libraries:  
[https://developer.cisco.com/codeexchange/github/repo/clay584/parse\_genie/](https://developer.cisco.com/codeexchange/github/repo/clay584/parse_genie/)

The error still persists. Also posted the issue on GitHub. Not sure if it is bug in the parsing library.

Any assistance would greatly be appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [June 22, 2020, 8:15pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/6 "2020-06-22T20:15:30Z")

</div>

I am using the collection not the role. So it looks like this at the top. Had to add the interpreter variable because Ansible kept using Python 2

---

<div class="post-metadata">

**Author:** ![Jason2](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@Jason2](https://forum.ansible.com/u/Jason2)\
**Post date:** [June 22, 2020, 8:48pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/7 "2020-06-22T20:48:48Z")

</div>

This error shows:

fatal: [SW1]: FAILED! =\> {“msg”: “template error while templating string: no filter named ‘clay584.genie.parse\_genie’. String: {{ neighbors[‘stdout’][0] | clay584.genie.parse\_genie(command=‘show cdp neighbors’, os=‘ios’) }}”}

This is the full playbook I am using:

---

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [June 23, 2020, 3:21pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/8 "2020-06-23T15:21:35Z")

</div>

Not sure it seems like it is looking for the filter. Have you used a collection before? Is the collections\_path defined in the ansible.cfg?

---

<div class="post-metadata">

**Author:** ![Jason2](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@Jason2](https://forum.ansible.com/u/Jason2)\
**Post date:** [June 23, 2020, 3:29pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/9 "2020-06-23T15:29:26Z")

</div>

No I have never used a collection before. So my ansible.cfg is currently to the default

---

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [June 23, 2020, 3:33pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/10 "2020-06-23T15:33:28Z")

</div>

You need to add a new line under [defaults]

collections\_paths = /path/to/collections

---

<div class="post-metadata">

**Author:** ![Jason2](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@Jason2](https://forum.ansible.com/u/Jason2)\
**Post date:** [June 23, 2020, 5:30pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/11 "2020-06-23T17:30:16Z")

</div>

I added it. Now this shows:

fatal: [SW1]: FAILED! =\> {  
**“msg”: “parse\_genie: Genie package is not installed. To install, run ‘pip install genie’.”**  
}

When I do pip or pip3 install genie, it says requirements already satisfied (as I already installed it).

Just to show you what my directories look like:

 ![directory genie.JPG](https://us1.discourse-cdn.com/ansible/original/2X/0/026d0ea4b93ee9d8f1ec1192e3d1ac591acb22bf.jpeg)

And under [defaults] in my ansible.cfg:

**collections\_paths = /etc/ansible/collections/ansible\_collections/**

Tried putting a less specific and then a more specifc path to the collections folder also. (= /etc/ansible/collections, = /etc/ansible/collections/ansible\_collections/clay584/genie)

Prior to this, I did a **ansible-galaxy collection install clay584.genie** to install the collection and it said this:  
Installing ‘clay584.genie:0.1.11’ to ‘/root/.ansible/collections/ansible\_collections/clay584/genie’

Did I do anything wrong?

---

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [June 23, 2020, 6:04pm UTC](https://forum.ansible.com/t/ansible-to-connect-to-certain-switchports-on-cisco-2960/33221/12 "2020-06-23T18:04:59Z")

</div>

Well at this point I would probably remove genie. Then create a virtual python environment. Run the pip install commands and see if that works for you. If you are still running into issues I would reach out to the guys who made genie - I think Clay is in the group there too.

[https://eurl.io/#r18UzrQVr](https://eurl.io/#r18UzrQVr)

> **(attachments)**
>
> ![directory genie.JPG](https://us1.discourse-cdn.com/ansible/original/2X/0/026d0ea4b93ee9d8f1ec1192e3d1ac591acb22bf.jpeg)
