简体   繁体   English

Ansible Playbook中变量的变量

[英]Variable of variable in Ansible Playbook

I am trying to access variables which is defined in group_vars 我正在尝试访问在group_vars定义的group_vars

group_vars/all group_vars /全部

parent1:
  child1: somevalue1
  child2: somevalue2

parent2:
  child1: somevalue1
  child2: somevalue2

Now I am passing parent detail from ansible playbook extra vars like this 现在我从像这样的ansible剧本的额外变量中传递parent细节

ansible-playbook playbook.yml -e "parent=parent1"

Now How can I access parent1.child1 value where parent1 comes in {{ parent }} vars? 现在,我如何才能获得parent1.child1值,其中parent1进来{{ parent }}瓦尔?

My playbook look like this:- 我的剧本看起来像这样:

playbook.yml playbook.yml

- hosts: local
  user: roop
  gather_facts: no
  connection: local

  vars: 
     parent: ""

  tasks: 

  #get parent value
  - debug: msg={{ parent }}

  #trying to access parent1.child1 value here
  - debug: msg={{ {{ parent }}.child1 }}

Playbook output:- 剧本输出:-

PLAY [local] ******************************************************************

TASK: [debug msg=local] *******************************************************
ok: [127.0.0.1] => {
    "msg": "parent1"
}

TASK: [debug msg={{{{parent}}.child1}}] ***************************************
ok: [127.0.0.1] => {
    "msg": "{{{{parent}}.child1}}"
}

PLAY RECAP ********************************************************************
127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0

Can anyone guide how can I achieve this or any alternate solution. 任何人都可以指导我如何实现此目标或任何其他解决方案。

How I have done this 我是如何做到的

Change group_vars/all like below:- 更改group_vars /如下所示:-

data:
    parent1:
      child1: somevalue1
      child2: somevalue2

    parent2:
      child1: somevalue1
      child2: somevalue2

Change in playbook.yml:- 在playbook.yml中进行更改:-

 - debug: msg={{ data[parent].child1 }}

Please share if you have any better solution :) 如果您有更好的解决方案,请分享:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM