# CLI\_Parse and loops

**URL:** <https://forum.ansible.com/t/cli-parse-and-loops/34213>\
**Category:** Ansible Project\
**Created:** [November 19, 2020, 10:20pm UTC](https://forum.ansible.com/t/cli-parse-and-loops/34213 "2020-11-19T22:20:30Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [November 19, 2020, 10:20pm UTC](https://forum.ansible.com/t/cli-parse-and-loops/34213/1 "2020-11-19T22:20:30Z")

</div>

Hello everyone. I am having some issues with using the information from cli\_parser output. ‘’’

- name: Create po\_ch variable  
ansible.netcommon.cli\_parse:  
command: “show etherchannel summary”  
parser:  
name: ansible.netcommon.pyats  
set\_fact: po\_ch  
tags: debug

- name: Create a list of interfaces that are PortChannel Memebers  
set\_fact:  
exclude\_ints: “{{ item.value.port\_channel.port\_channel\_member\_intfs }}”  
loop: “{{ ansible\_facts.po\_ch.interfaces|dict2items }}”  
tags: debug

- name: Debug new set fact  
debug:  
msg: “{{ exclude\_ints }}”  
tags: debug

```auto
**And here is the output**

TASK [Create a list of interfaces that are PortChannel Memebers] ***************
task path: /tmp/awx_1417_a6buronn/project/device_tracking.yml:58
ok: [s996sw1] => (item={'key': 'Port-channel1', 'value': {'name': 'Port-channel1', 'bundle_id': 1, 'flags': 'SU', 'oper_status': 'up', 'members': {'GigabitEthernet1/0/37': {'interface': 'GigabitEthernet1/0/37', 'flags': 'P', 'bundled': True, 'port_channel': {'port_channel_member': True, 'port_channel_int': 'Port-channel1'}}, 'GigabitEthernet1/0/38': {'interface': 'GigabitEthernet1/0/38', 'flags': 'D', 'bundled': False, 'port_channel': {'port_channel_member': True, 'port_channel_int': 'Port-channel1'}}}, 'port_channel': {'port_channel_member': True, 'port_channel_member_intfs': ['GigabitEthernet1/0/37', 'GigabitEthernet1/0/38']}}}) => {
"ansible_facts": {
"exclude_ints": [
"GigabitEthernet1/0/37",
"GigabitEthernet1/0/38"
]
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"key": "Port-channel1",
…
ok: [s996sw1] => (item={'key': 'Port-channel3', 'value': {'name': 'Port-channel3', 'bundle_id': 3, 'flags': 'SU', 'oper_status': 'up', 'members': {'GigabitEthernet1/1/2': {'interface': 'GigabitEthernet1/1/2', 'flags': 'P', 'bundled': True, 'port_channel': {'port_channel_member': True, 'port_channel_int': 'Port-channel3'}}, 'GigabitEthernet1/1/4': {'interface': 'GigabitEthernet1/1/4', 'flags': 'P', 'bundled': True, 'port_channel': {'port_channel_member': True, 'port_channel_int': 'Port-channel3'}}}, 'port_channel': {'port_channel_member': True, 'port_channel_member_intfs': ['GigabitEthernet1/1/2', 'GigabitEthernet1/1/4']}}}) => {
"ansible_facts": {
"exclude_ints": [
"GigabitEthernet1/1/2",
"GigabitEthernet1/1/4"
]
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"key": "Port-channel3",
"v…
TASK [Debug new set fact] ******************************************************
task path: /tmp/awx_1417_a6buronn/project/device_tracking.yml:64
ok: [s996sw1] => {
"msg": [
"GigabitEthernet1/1/2",
"GigabitEthernet1/1/4"
]
}
META: ran handlers
META: ran handlers

So it loops and the exclude_ints variable only has the last 2 interfaces when there are more than one portchannel on the device. How can I merge these to a list and use that in a when statement further down in the play.

Any help would be apprciated.

Thank you!
```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 20, 2020, 7:36am UTC](https://forum.ansible.com/t/cli-parse-and-loops/34213/2 "2020-11-20T07:36:06Z")

</div>

Either concatenate the lists in the loop

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exclude\_ints: "{{ exclude\_ints|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[item.value.port\_channel.port\_channel\_member\_intfs]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ po\_ch.interfaces|dict2items }}"

or use json\_query

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exclude\_ints: "{{ po\_ch.interfaces|json\_query(query) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query: '\*.port\_channel.port\_channel\_member\_intfs'

In both cases the result will be a list of lists

&nbsp;&nbsp;exclude\_ints:  
&nbsp;&nbsp;- - GigabitEthernet1/0/37  
&nbsp;&nbsp;&nbsp;&nbsp;- GigabitEthernet1/0/38  
&nbsp;&nbsp;- - GigabitEthernet1/1/2  
&nbsp;&nbsp;&nbsp;&nbsp;- GigabitEthernet1/1/4

In both cases it's possible to flatten the list if needed. For example

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exclude\_ints: "{{ po\_ch.interfaces|json\_query(query)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flatten }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query: '\*.port\_channel.port\_channel\_member\_intfs'

gives

&nbsp;&nbsp;exclude\_ints:  
&nbsp;&nbsp;- GigabitEthernet1/0/37  
&nbsp;&nbsp;- GigabitEthernet1/0/38  
&nbsp;&nbsp;- GigabitEthernet1/1/2  
&nbsp;&nbsp;- GigabitEthernet1/1/4

---

<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:** [November 20, 2020, 1:44pm UTC](https://forum.ansible.com/t/cli-parse-and-loops/34213/3 "2020-11-20T13:44:41Z")

</div>

Thanks again Vladimir. json\_query was the answer again. Need to read up on that some more to understand the syntax. These examples are extremely helpful.

Again thank you for taking the time
