【问题标题】:How to use Ansible inventory variables in templates如何在模板中使用 Ansible 库存变量
【发布时间】:2016-05-18 15:09:36
【问题描述】:

在为其他组运行播放时甚至可以引用组变量吗?

我有一个这样的具体案例:

/etc/ansible/hosts

[group1]
server1.test.org

[group2]
server2.test.com

[group2:vars]
foo=bar

为 group1 运行一场比赛

- name: test variables...
  hosts: group1
  gather_facts: no
  tasks:
       - debug: msg="foo={{ groups[group2].foo }}" 

它不起作用,我尝试了其他语法变体但没有成功。

【问题讨论】:

    标签: templates variables ansible


    【解决方案1】:

    当清单初始化时,这些组实际上并没有为它们定义变量。主机获得为组变量定义的内容的副本。所以要做你想做的事,你需要从主机读取。试试这个:

    - name: test variables...
      hosts: group1
      gather_facts: no
      tasks:
      - debug: msg="foo={{ hostvars[groups['group2'][0]].foo }}" 
    

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多