Uncomplicated list of lists - flattening

I am using set_fact in playbook to collect data using regex_findall (). I pull out two groups with a regex and the end result becomes a list of lists.

set_fact: nestedList="{{ myOutput.stdout[0] | regex_findall('(.*?)\n markerText(.*)')}}"

      

An example of a list dump looks like this:

[[a,b],[c,d],[e,f],[g,h]]

      

I need to go through the parent list and take the two parts of each sub-list and use them together. I tried with_items and with_nested, but didn't get the results I'm looking for.

Using the above example, in one pass through the loop, I need to work with 'a' and 'b'. An example would be item.0 = 'a' and item.1 = 'b'. On the next pass through the loop, item.0 = 'c' and item.1 = 'd'.

I cannot figure out if this is correct when it is a list of lists. If I take the list above and just output it, the "item" is repeated through every item in all the sub-lists.

- debug:
    msg: "{{ item }}"
    with_items: "{{ nestedList }}"

      

The result looks like this:

a
b
c
d
e
f
g
h

      

How can I iterate through the parent list and use the elements in the signatures?

+3


source to share


1 answer


You want to use with_list

instead with_items

.

with_items

forces nested lists to align and with_list

passes the argument as is.



---
- hosts: localhost
  gather_facts: no
  vars:
    nested_list: [[a,b],[c,d],[e,f],[g,h]]
  tasks:
    - debug: msg="{{ item[0] }} {{ item[1] }}"
      with_list: "{{ nested_list }}"

      

+4


source







All Articles