get dict value from variable key in ansible

2020-07-05 06:48发布

问题:

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:

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



标签: ansible