Using variable to access specific node in dict

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

Try:

{{people[myperson].name}}

Matt,

Thanks, that’s a good thought, but it returned the same error.

Matt,

I revisited this and I’m not sure what I did wrong before, but it worked! Thanks for the tip.