ansible with_items list of lists is flattening

ansible flatten list of dictionaries
ansible list of lists
ansible expand list
ansible parse list
ansible print list
ansible with_items json
ansible attributes list
ansible vars with_items

I'm trying to use ansible to loop over a list of lists to install some packages. But {{item}} is returning every element in the sub lists rather than the sublist itself. I have a yaml file which come from a manifest list from outside ansible and it looks like this:

---
modules:
 - ['module','version','extra']
 - ['module2','version','extra']
 - ['module3','version','extra']

My task looks like this:

task:
 - include_vars: /path/to/external/file.yml
 - name: install modules
   yum: name={{item.0}} state=installed
   with_items: "{{ modules }}"

When I run that I get:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! int object has no element 0"}

When I try:

- debug: msg="{{item}}"
  with_items: "{{module}}"

it prints every element (module, version, extra, and so on), not just the sublist (which is what I would expect)

Unfortunately, this is the intended behavior. Please see this discussion on with_tems and nested lists

Ansible list of lists - flattening, You want to use with_list instead of with_items . with_items forcefully flattens nested lists, while with_list feeds argument as is. --- - hosts:  unlike ‘items’ which only flattens 1 level, this plugin will continue to flatten until it cannot find lists anymore. aka highlander plugin, there can only be one (list).

An alternative way to solve this issue is to use a complex item instead of a list of list. Structure your variables like this:

- modules:
  - {name: module1, version: version1, info: extra1}
  - {name: module2, version: version2, info: extra2}
  - {name: module3, version: version3, info: extra3}

Then you can still use with_items, like this:

- name: Printing Stuffs...
  shell: echo This is "{{ item.name }}", "{{ item.version }}" and "{{ item.info }}"
  with_items: "{{modules}}"

with_items flatten list of lists · Issue #5913 · ansible/ansible · GitHub, Issue Type: Bug Report Ansible Version: ansible 1.4.4 Environment: N/A Summary: with_items flatten list of lists. Steps To Reproduce: - debug:  What they didn't foresee at that point is that somebody may just have a list of lists on with_items input: - hosts: all gather_facts: no vars: foo: [['abc', 'def'], ['ghi', 'jkl']] tasks: - debug: var=item with_items: foo and with the same run output as I pasted above in #5913 (comment).

@helloV already provided the answer that you can not do this using with_items, i am going to show you how you can use your current data structure with with_nested to get the desired output.

Here is an example playbook:

---
- hosts:
    - localhost
  vars:
    - modules:
      - ['module1','version1','extra1']
      - ['module2','version2','extra2']
      - ['module3','version3','extra3']

  tasks:
    - name: Printing Stuffs...
      shell: echo This is "{{ item.0 }}", "{{ item.1 }}" and "{{ item.2 }}"
      with_nested:
       - modules

Now you will get the following as stdout_lines:

This is module1, version1 and extra1
This is module2, version2 and extra2
This is module3, version3 and extra3

ansible with_items list of lists is flattening, I'm trying to use ansible to loop over a list of lists to install some packages. But {{​item}} is returning every element in the sub lists rather than the sublist itself. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

flattened – return single list completely flattened, unlike 'items' which only flattens 1 level, this plugin will continue to flatten until it cannot find lists anymore. aka highlander plugin, there can only be one (list). Iterating over two lists in ansible. Ask Question Asked 2 years, 6 months ago. ansible with_items list of lists is flattening. 3. Multiple nested loops in ansible. 0.

Nested Loops, Please note that with_items flattens the first depth of the list it is and can yield unexpected results if you pass a list which is composed of lists. SUMMARY with_items is replaced by loop and the flatten filter. But the two are actually different ISSUE TYPE Bug Report COMPONENT NAME keyword loop and filter flatten ANSIBLE VERSION [root@test71 ansible]# ansible --version ansible 2.6.2

[SOLVED] ansible with_items list of lists is flattening, Unfortunately, this is the intended behavior. Please see this discussion on with_tems and nested lists. Extending Ansible. Developer Guide; Common Ansible Scenarios. Public Cloud Guides; Network Technology Guides; Virtualization and Containerization Guides; Ansible for Network Automation. Ansible for Network Automation; Ansible Galaxy. Galaxy User Guide; Galaxy Developer Guide; Reference & Appendices. Module Index; Playbook Keywords; Return Values; Ansible Configuration Settings

ansible with_items list of lists is flattening - yaml - android, I'm trying to use ansible to loop over a list of lists to install some packages. But {{​item}} is returning every element in the sub lists rather than the sublist itself. ansible filter list when matches. Ansible list of lists - flattening. 0. Debug not working with_items in ansible play. 1. Ansible - items list from a variable

Comments
  • This helped my case with Ansible 2.2.1. Ran into a similar issue with list of lists where {{ item.0 }} was expanded incorrectly to '.' or '', and using a dictionary per list item fixed this.
  • I swear I tried this as I was iterating over various options. I just tried it again, and it worked exactly as I was hoping.
  • So I jumped the gun, it still isn't working. my datastructure has one line in it at the moment: cpann_modules: - ['Algorithm::Diff', '0'] error is: failed: [alpha.bluehost.com] => (item=[u'A', u'l', u'g', u'o', u'r', u'i', u't', u'h', u'm', u':', u':', u'D', u'i', u'f', u'f']) => {"changed": false, "failed": true, "item": ["A", "l", "g", "o", "r", "i", "t", "h", "m", ":", ":", "D", "i", "f", "f"], "msg": "No Package matching ... [ error truncated ] you can see that it is trying to iterate over every character in the first line, which is silly.
  • @Neybar As per questions example, it's working for me as you can see in my answer..could you add your current example to the question so that I can check..