I’m not sure if this is the right mailing list group to ask general questions or if this is just for developers. If not, then let me know what the correct group is. Anyway, my question is I’m trying to update some old playbooks to conform to the newer syntax for variables ie. {{ foo }} instead of $foo. In many of these playbooks I pass in a variable from a master includes playbook to sub-playbooks and I then use that variable as a positional parameter to access a specific element in a list. So instead of writing ${servername[0]} I do ${servername[$iteration]} where iteration is my position in the list I want to access in my vars file. I may just rewrite the whole bunch of playbooks to accomplish my task differently but for now I’d just like to update my playbooks to use the newer standard. (Since updating Ansible last time I get the warning that the old variable style will not be supported soon) I don’t want to upgrade and find out all my playbooks don’t work anymore.
Anyway, I’ve been trying to figure out how to change the ${servername[$iteration]} to meet the new standards and just can’t figure it out. Does anybody know how to do this now?
Since nobody has answered back on this post, does my question not make sense or need more explanation? Or does no one know the answer?
Hi David,
Try {{ servername[iteration] }}
Dmitry,
I think I already tried that, but I went ahead and tried it again. I get back this error when setting iteration=0 in my calling includes playbook:
fatal: [servername here] => One or more undefined variables: ‘list’ object has no attribute ‘0’
that means that your list is empty and has no elements, you could
check that before hand by looking at list|length
But I’m referencing the same list/vars file that I’ve always used with this script. I have data in it. If I actually put in the script {{ servername[0] }} it pulls the first item in the list.yml file and the script works. But when I try to do a variable substitution for the position in the list it gives me the error. When I use the old syntax ${servername[$iteration]} it works. Could this be a bug or am I just missing something here?
I just tested this and i cannot reproduce your issue, i would need to
see the actual template having the problem
Here is an example of one of the scripts where I use this method of calling an item from a list where it works fine using the old $var syntax but fails when changing to the jinja2 style variables.
First I call the script from my main .yml file that is a bunch of includes
That in turn calls this script
I should also include a copy of the script as it works today.