Ansible - 在远程主机模板中使用来自本地主机的事实 [英] Ansible - use fact from local host in remote host template
本文介绍了Ansible - 在远程主机模板中使用来自本地主机的事实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含本地主机角色和远程主机角色的剧本.
I have a playbook that contains roles for localhost and roles for remote hosts.
在其中一个 localhost 角色中,我设置了一个名为 git_tag
的事实.
In one of the localhost roles I set a fact called git_tag
.
我想在远程主机的模板中使用这个事实.
I want to use this fact in a template for the remote hosts.
我试过了:
- name: Read Version
set_fact:
git_tag: "{{ package_json.stdout | from_json | json_query('version')}}"
delegate_to: "test-server"
但是当 Ansible 到达读取具有 {{ git_tag }}
的模板的角色时,它说 git_tag 未定义
.
But when Ansible reaches the role that reads the template that has {{ git_tag }}
it says that git_tag is undefined
.
我确定我做错了什么.我该怎么做?
I'm sure I'm doing something wrong. How can I do it?
推荐答案
您应该使用 hostvars
魔法变量:
You should use a hostvars
magic variable:
{{ hostvars['localhost']['git_tag'] }}
这篇关于Ansible - 在远程主机模板中使用来自本地主机的事实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文