17
votes

Here is my problem I need to get a dict value from key. But the key is also a var. For example, I had an ansible role.

In vars/main.yml, I defined vars as below:

---
location:   "USA"
source:     {
             "China":  "/net/server1/patha",
             "USA":  "/net/server2/pathb",
             "Japan": "/net/server3/pathc"
            }

So in my tasks: tasks/main.yml. How do get "/net/server2/pathb" using the vars. I tried below in tasks, all did not work.

-shell: "perl run.perl {{ source.location }}/script.pl"

-shell: "perl run.perl {{ source.{{ location }} }}/script.pl"

This may be a simple question. But I searched many posts for a long time and still cannot get a right answer. So please help and many thanks.

1

1 Answers

27
votes

The answer is {{ source[location] }}.