Using variable lists in ansible returns undefined variable
Solution 1:
The format of your variable file is wrong. The top level is not a list, it should look like this:
---
stuff:
- stuff1: bill
stuff2: sue
Additionally, the path to the vars file should start with a / from the Ansible root:
vars_files:
- /vars/blah.yml
Solution 2:
TL;DR
loop: "{{ stuff }}"
Full story
On the contrary of the former and still widely defaulty used with_items:
, a bare loop:
does not apply an automatic flatten(level=1)
on the passed arguments.
For further info about this feature, you can see:
- the item lookup documentation
-
the
with_items
toloop
migration procedure on the ansible loops documentation.
If your example was using with_items
with_items:
- "{{ stuff }}"
the resulting list would still be exactly the one you defined in your file.
Now used with loop
loop:
- "{{ stuff }}"
you are looping over a list of lists which looks like (note the solo dash on top of the below example and the indentation of the rest of the content: it's not a typo).
-
- stuff1: bill
stuff2: sue
So the first element you get in your loop is actually your full list in your var file.
To fix that, just pass the variable correctly to loop
, i.e.
loop: "{{ stuff }}"