Using variable to access specific node in dict

1,703 views
Skip to first unread message

Chip Selden

unread,
Jan 22, 2015, 1:05:20 PM1/22/15
to ansible...@googlegroups.com
Greetings,

I'm trying to use a variable to determine which node in a dictionary to get information from. I will later want to use this for iterating over lists of variables so that I can use specific nodes in a dict without looping over all of them.

I have the following vars:
myperson: bob
people:
  alice:
    name: alice
    age: 30
  bob:
    name: bob
    age: 30


I using the following task to test the result:
- name: Debug out myperson's name
  debug: msg="name = {{people.{{myperson}}.name}}"


This returns the following error:
template error while templating string: expected name or number

Note the only way that I have gotten this to evaluate correctly is if I use the following task where I only need one set of braces:
- name: Debug out myperson's name
  debug: var=people.{{myperson}}.name


However, I cannot use this syntax in other cases (such as the shell or lineinfile modules). Is there some syntax for nesting variables in this way that I'm not aware of?

Thanks,
Chip

Matt Martz

unread,
Jan 22, 2015, 1:37:19 PM1/22/15
to ansible...@googlegroups.com
Try:

{{people[myperson].name}}
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f47bbc60-a904-4616-87d6-901ac1a07a83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Chip Selden

unread,
Jan 22, 2015, 1:57:47 PM1/22/15
to ansible...@googlegroups.com
Matt,

Thanks, that's a good thought, but it returned the same error.
Try:

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Chip Selden

unread,
Jan 26, 2015, 5:05:16 PM1/26/15
to ansible...@googlegroups.com
Matt,

I revisited this and I'm not sure what I did wrong before, but it worked! Thanks for the tip.
Reply all
Reply to author
Forward
0 new messages