# Looping through registered lines

**URL:** <https://forum.ansible.com/t/looping-through-registered-lines/26750>\
**Category:** Ansible Project\
**Created:** [February 15, 2018, 7:44pm UTC](https://forum.ansible.com/t/looping-through-registered-lines/26750 "2018-02-15T19:44:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Barrere](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@Ian\_Barrere](https://forum.ansible.com/u/Ian_Barrere)\
**Post date:** [February 15, 2018, 7:44pm UTC](https://forum.ansible.com/t/looping-through-registered-lines/26750/1 "2018-02-15T19:44:16Z")

</div>

I have a task for a Cisco device where I register zero or more lines to come back to later:

- name: gather non-standard usernames  
ios\_command:  
commands:
- show running-config | include username  
register: usernames

I would like the next task to run through each registered line individually and if the line doesn’t contain the word “root”, then it should print the line. This was my first attempt at the task:

- debug:  
msg: “{{ item }}”  
when: “‘root’ in item.stdout”  
loop: “{{ usernames.stdout\_lines }}”

If I debug the entire usernames variable I get this:

TASK [network-common : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [rtr01] =\> {  
“usernames”: {  
“changed”: false,  
“failed”: false,  
“stdout”: [  
“username root privilege 15 secret 5 \*\*\*\*\*\*\*\nusername temp privilege 15 secret 4 \*\*\*\*\*\*\*”  
],  
“stdout\_lines”: [  
[  
“username root privilege 15 secret 5 \*\*\*\*\*\*\*”,  
“username temp privilege 15 secret 4 \*\*\*\*\*\*\*”  
]  
]  
}  
}

Apparently it returns the lines inside two arrays. Since the outer array always includes the word “root”, it returns the entire contents of the outer array, which includes both lines. I tried to flatten the arrays like so:

- debug:  
msg: “{{ lookup(‘flattened’,item) }}”  
when: “‘root’ not in item”  
loop: “{{ usernames.stdout\_lines }}”

But that flattens them too much, into a single string, resulting in this:

TASK [network-common : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [rtr01] =\> (item=[u’username root privilege 15 secret 5 \*\*\*\*\*\*\*‘, u’username temp privilege 15 secret 4 \*\*\*\*\*\*\*’]) =\> {  
“item”: [  
“username root privilege 15 secret 5 \*\*\*\*\*\*\*”,  
“username temp privilege 15 secret 4 \*\*\*\*\*\*\*”  
],  
“msg”: “username root privilege 15 secret 5 \*\*\*\*\*\*\*,username temp privilege 15 secret 4 \*\*\*\*\*\*\*\*”  
}

If I use the operator “with\_items”, this works correctly, as apparently with\_items flattens the first level but nothing after that. However, the deprecation warning has suggested that we migrate to “loop” instead. Does anybody know how to emulate the flattening behavior of with\_items with the loop operator?

Thanks,  
Ian

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 15, 2018, 8:20pm UTC](https://forum.ansible.com/t/looping-through-registered-lines/26750/2 "2018-02-15T20:20:46Z")

</div>

There is no difference between them

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item }}"  
&nbsp;&nbsp;when: "'root' not in item"  
&nbsp;&nbsp;with\_items: "{{ usernames.stdout\_lines.0 }}"

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item }}"  
&nbsp;&nbsp;when: "'root' not in item"  
&nbsp;&nbsp;loop: "{{ usernames.stdout\_lines.0 }}"

---

<div class="post-metadata">

**Author:** ![Ian\_Barrere](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@Ian\_Barrere](https://forum.ansible.com/u/Ian_Barrere)\
**Post date:** [February 15, 2018, 9:00pm UTC](https://forum.ansible.com/t/looping-through-registered-lines/26750/3 "2018-02-15T21:00:42Z")

</div>

I see. Referencing the 0 explicitly on the end has resolved the problem.

Thanks!  
Ian
