在 Ansible 临时命令中使用 Ansible 事实 [英] Use Ansible facts in an Ansible ad-hoc command
问题描述
是否可以在 Ansible adhoc 命令中使用通常包含在 ansible_facts
中的内容?
Is it possible to use what would normally be included in ansible_facts
in an Ansible adhoc command?
例如,我在所有服务器上的 /tmp/myFile
都有一个文件,我想这样做:
For example, I have a file at /tmp/myFile
on all of my servers and I'd like to do:
ansible all -i [inventory file] -m fetch -a "src=/tmp/myFile dest=myFile-[insert ansible_hostname here]"
无需为此编写完整的剧本.
Without having to make a whole playbook for it.
推荐答案
不,您不能在 ansible cli 中引用 ansible 事实.这是因为当您运行 ansible ... -m fetch
时,您没有获得正在运行的主机的事实.事实是使用 setup
模块收集的(您可以通过执行 ansible ... -m setup
来尝试.无论如何,这可以通过像
No you cannot refer to ansible facts in ansible cli. This is because when you run ansible ... -m fetch
you are not getting the facts of the host(s) you are running on. The facts are gathered with setup
module ( you can try that by doing ansible ... -m setup
.
Anyway, this can be addressed with a simple playbook like
# file: fetchfile.yml
- hosts: all
tasks:
- fetch: src=/tmp/myFile dest=myFile-{{ inventory_hostname }}
$ ansible-playbook -i [inventory_file] fetchfile.yml
ansible-playbook
隐式运行 setup
模块,因此您可以访问所有作为变量的事实.
ansible-playbook
runs the setup
module implicitly, so you will have access to all the facts as variables.
这篇关于在 Ansible 临时命令中使用 Ansible 事实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!