Question:
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:
1 2 3 4 5 6 |
--- modules: - ['module','version','extra'] - ['module2','version','extra'] - ['module3','version','extra'] |
My task looks like this:
1 2 3 4 5 6 |
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:
1 2 |
fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! int object has no element 0"} |
When I try:
1 2 3 |
- 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)
Answer:
Unfortunately, this is the intended behavior. Please see this discussion on with_tems and nested lists