在Ansible临时命令中使用Ansible事实 [英] Use Ansible facts in an Ansible ad-hoc command

查看:85
本文介绍了在Ansible临时命令中使用Ansible事实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在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.

推荐答案

不,您不能在anscli 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆