trouble iterating through stdout_lines

6 views
Skip to first unread message

Marcos Georgopoulos

unread,
Mar 6, 2018, 5:49:30 AM3/6/18
to Ansible Project
Hi, I am trying to work out how to iterate through some results from a command I am running on a network device. the idea is when I find the line that matches what I am looking for based on a regexp I want to store it.



tasks
:

- name: get the existing access-list
 ios_command
:
 provider
: "{{ cli }}"
 commands
:
 
- show access-list mgtaccess
 
register: result

- debug: var=result.stdout_lines

- name: Do something with each result
 debug
:
 msg
: "{{ item }}"
 with_items
:
 
- "{{ result.stdout_lines }}"


This is the output from the task




ok
: [172.16.1.135] => (item=[u'Standard IP access list mgtaccess', u'    20 permit 10.1.1.1', u'    10 permit 192.168.0.1', u'    30 permit any (2566 matches)']) => {
   
"changed": false,
   
"failed": false,
   
"item": [
       
"Standard IP access list mgtaccess",
       
"    20 permit 10.1.1.1",
       
"    10 permit 192.168.0.1",
       
"    30 permit any (2566 matches)"
   
],


   
"msg": [
       
"Standard IP access list mgtaccess",
       
"    20 permit 10.1.1.1",
       
"    10 permit 192.168.0.1",
       
"    30 permit any (2566 matches)"

   
]
}


I'm confused shouldn't I be seeing multiple individual 'msg's??


Many thanks.

Kai Stian Olstad

unread,
Mar 6, 2018, 1:36:12 PM3/6/18
to ansible...@googlegroups.com
result.stdout_lines is a list that contains lists
So you have result.stdout_lines.0.0 the first line, result.stdout_lines.0.1 the second one and so on.

You can do
with_items: '{{ result.stdout_lines.0 }}'
to get what you are after.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages