Accessing elements in a list

2,435 views
Skip to first unread message

David Vosbury

unread,
Mar 19, 2015, 11:23:17 AM3/19/15
to ansible...@googlegroups.com
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?

David Vosbury

unread,
Mar 20, 2015, 12:56:03 AM3/20/15
to ansible...@googlegroups.com
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?

Dmitry Malinovsky

unread,
Mar 20, 2015, 2:54:03 AM3/20/15
to ansible...@googlegroups.com
Hi David,

Try {{ servername[iteration] }}

David Vosbury

unread,
Mar 20, 2015, 8:44:44 AM3/20/15
to ansible...@googlegroups.com
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'

Brian Coca

unread,
Mar 20, 2015, 8:55:33 AM3/20/15
to ansible...@googlegroups.com
that means that your list is empty and has no elements, you could
check that before hand by looking at list|length



--
Brian Coca

David Vosbury

unread,
Mar 20, 2015, 9:17:38 AM3/20/15
to ansible...@googlegroups.com
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?

Brian Coca

unread,
Mar 20, 2015, 9:22:06 AM3/20/15
to ansible...@googlegroups.com
I just tested this and i cannot reproduce your issue, i would need to
see the actual template having the problem



--
Brian Coca

David Vosbury

unread,
Mar 20, 2015, 11:08:28 AM3/20/15
to ansible...@googlegroups.com
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

- include: vm_provision.yml server=vcenter.mydomain.com username=v...@mydomain.com vmhost=esxi1.mydomain.com vmhost_destination=esxi1.mydomain.com template='vm-ubuntu124apl-x' datastore='iSCSI-datastore-1' iteration=0

That in turn calls this script

---
#####################
#vm_provision.yml
#An ansible script to provision individual vms based on the parameters fed to the script.
#See start_provision.yml and script documentation for other required parameters.
#####################
- hosts: vmaservers
  vars_files:
    - server_name.yml
  user: vi-admin
  tasks:
  - name: clone VM taking parameters from start_provision.yml and calling vmprovision.pl on the vma server.
    action: shell /home/vi-admin/bin/vmprovision.pl --server {{ server }} --username {{ username }} --credstore ~/.credentials.xml --op clone --vmhost {{ vmhost }} --vmhost_destination {{ vmhost_destination }} --vmname {{ template }} --datastore {{ datastore }} --vmname_destination {{ servername[iteration] }}

I get back the error One or more undefined variables: 'list' object has no attribute '0'

server_name.yml is a list like this

---
# Server names to use for each VM
servername:
- "vm-testans-1"
- "vm-testans-2"
- "vm-testans-3"






David Vosbury

unread,
Mar 20, 2015, 11:15:24 AM3/20/15
to ansible...@googlegroups.com
I should also include a copy of the script as it works today.

---
#####################
#vm_provision.yml
#An ansible script to provision individual vms based on the parameters fed to the script from
#start_provision.yml and the iteration parameter which pulls vm names from the server_name.yml.

#See start_provision.yml and script documentation for other required parameters.
#####################
- hosts: vmaservers
  vars_files:
    - server_name.yml
  user: vi-admin
  tasks:
  - name: clone VM taking names from server_name.yml calling vmprovision.pl on the vma server.
    action: shell /home/vi-admin/bin/vmprovision.pl --server $server --username $username --credstore ~/.credentials.xml --op clone --vmhost $vmhost --vmhost_destination $vmhost_destination --vmname $template --datastore $datastore --vmname_destination ${servername[$iteration]}
Reply all
Reply to author
Forward
0 new messages