Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get dict value from variable key in ansible

Tags:

ansible

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.

like image 305
TangHongWan Avatar asked Jan 13 '17 09:01

TangHongWan


1 Answers

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

like image 194
Konstantin Suvorov Avatar answered Oct 15 '22 21:10

Konstantin Suvorov