Use Ansible facts in an Ansible ad-hoc command

偶尔善良 提交于 2021-01-27 06:05:00

问题


Is it possible to use what would normally be included in ansible_facts in an Ansible adhoc command?

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.


回答1:


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 runs the setup module implicitly, so you will have access to all the facts as variables.



来源:https://stackoverflow.com/questions/36484980/use-ansible-facts-in-an-ansible-ad-hoc-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!