【问题标题】:Ansible - Get Facts from Remote Windows HostsAnsible - 从远程 Windows 主机获取事实
【发布时间】:2016-08-15 20:30:15
【问题描述】:

我正在使用 Ansible / Ansible Tower,并想确定我的 Windows 主机上可用的事实。 documentation 声明我可以运行以下命令:

ansible hostname -m setup

如何将其合并到从 Tower 运行的剧本中,以便从主机收集信息?

根据所提供的帮助,这是当前的剧本:

# This play outputs the facts of the Windows targets in scope

- name: Gather Windows Facts 
  hosts: "{{ target }}"
  gather_facts: yes
  tasks:
  - setup:
    register: ansible_facts
  - debug: item
    with_dict: ansible_facts

但是,运行它会产生以下错误:

错误!此任务“调试”具有额外的参数,仅允许在 以下模块:命令、shell、脚本、包含、include_vars、 add_host, group_by, set_fact, raw, meta

【问题讨论】:

    标签: ansible ansible-playbook ansible-facts


    【解决方案1】:

    使用gather_facts,默认为真。相当于运行setup模块。

    - hosts: ....
      gather_facts: yes
    

    事实保存在可用于剧本的 ansible 变量中。见System Facts

    有很多方法可以显示 ansible 事实。为了让您了解它的工作原理,请尝试以下操作:

    - hosts: 127.0.0.1
      gather_facts: true
    
      tasks:
      - setup:
        register: ansible_facts
      - debug: item
        with_dict: ansible_facts
    

    【讨论】:

    • 输出显示在哪里/如何输出?
    • 有没有办法输出大列表让我知道变量是什么?在他们的示例中,它提供了大量的 Ubuntu 输出。列出这些之后,我现在可以知道我可以将什么用作变量。这是一个 fact_path 列表,但不确定它在剧本中的工作原理docs.ansible.com/ansible/setup_module.html
    • 在 -debug 上收到一个错误(较新的 Playbooks):违规行似乎是:register: ansible_facts - debug: item ^ here
    • 发布你的剧本或尝试我发布的那个。
    • 完成,我有一个收集事实:是的,而你有真实的
    【解决方案2】:

    测试并通过它,这对我有用:

    - name: Gather Windows Facts 
      hosts: "{{ target }}"
      tasks:
        - debug: var=vars
        - debug: var=hostvars[inventory_hostname]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 2021-08-24
      • 2017-05-02
      相关资源
      最近更新 更多