【问题标题】:Ansible - print gathered facts for debugging purposes [duplicate]Ansible - 出于调试目的打印收集的事实[重复]
【发布时间】:2018-09-15 14:02:02
【问题描述】:

是否有某种方法可以在控制台上打印收集到的事实?
我的意思是使用setup 模块控制事实。我想打印收集到的事实。可能吗 ?如果可能的话,有人可以举个例子吗?

【问题讨论】:

  • 此命令显示有关主机的所有信息:ansible -i path_to_inventory -m setup hostname

标签: ansible


【解决方案1】:

使用setup 模块作为ad-hoc 命令:

ansible myhost -m setup

【讨论】:

  • 好的,但是。我有一些剧本。在这个剧本中,我执行了setup 模块。我可以在执行此操作后检查收集了哪些事实吗?
  • 使用-vv 开关运行剧本?
  • 我总是忘记使用临时命令的可能性。
  • 这种方法几乎没有问题:要使用它,您需要运行ansible -v myhost -m setup | grep ansible_hostname 之类的东西,因为只有在详细程度 >=1 和 grepping 时才会打印事实,因为太多了。理想情况下,ansible 应该支持链接模块并执行以下操作:ansible -m setup -m debug -a var=ansible_hostname
  • ansible -i somehost, all -m setup 如果您正在运行这个真正 ad-hoc 并且甚至没有任何库存。 (后面的,是正确的)
【解决方案2】:

你可以简单地转储hostvars:

dump.yml

---
 - name: Dump
   hosts: "{{ target|default('localhost') }}"
   tasks:
   - name: Facts
     setup:
   - name: Dump
     delegate_to: localhost
     run_once: true
     copy:
       content: "{{ hostvars[inventory_hostname] | to_nice_json }}"
       dest: /tmp/setup-dump.json

使用ansible-playbook dump.yml -e target=hostname 或直接不使用主机名调用此剧本。

【讨论】:

  • 谢谢,但它似乎不起作用。 TASK [Dump] ******************************************************************************************************************************************************************************** changed: [localhost -> localhost] 我从 target=localhost 开始
  • 它有效。输出保存在 /tmp/setup-dump.json
猜你喜欢
  • 1970-01-01
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多