本文介绍了从ansible中的变量键获取dict值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,我需要从键中获取字典值.但关键也是一个 var.例如,我有一个 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.

vars/main.yml中,我定义了vars如下:

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

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

所以在我的任务中:tasks/main.yml.如何使用变量获取/net/server2/pathb".我在下面的任务中尝试过,都不起作用.

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.

推荐答案

答案是{{ source[location] }}.

这篇关于从ansible中的变量键获取dict值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:25