Ansible list of lists - flattening

ansible

Solution

You want to use `with_list` instead of `with_items`.

`with_items` forcefully flattens nested lists, while `with_list` feeds 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 }}"

Problem

I am using a set_fact in a playbook to gather data using a regex_findall(). I'm pulling out two groups with the regex and the ending result becomes a list of lists. ``` set_fact: nestedList="{{ myOutput.stdout[0] | regex_findall('(.*?)\n markerText(.*)')}}" ``` A example dump of the list looks like: ``` [[a,b],[c,d],[e,f],[g,h]] ``` I need to iterate 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 do not get the results I'm looking for. Using the example above, in one loop pass I need to work with 'a' and 'b'. An example could be item.0 = 'a' and item.1 = 'b'. On the next loop pass, item.0 = 'c' and item.1 = 'd'. I can't seem to get it correct when it is a list of lists like that. If I take the list above and just output it, the 'item' iterates through every item in all of the sub-lists. ``` - debug: msg: "{{ item }}" with_items: "{{ nestedList }}" ``` The result from this looks like: ``` a b c d e f g h ``` How can I iterate through the parent list, and use the items in the sub-lists?

Original source