仅打印失败的任务-Ansible

仅打印失败的任务-Ansible,ansible,Ansible,我创建了一个Ansible playbook,它只检查与特定端口上的目标主机的连接。 这是我的剧本 - name: ACL check to target machines. hosts: all gather_facts: False vars: target_hosts: - server1 18089 - server1 8089 - server1 18000 tasks: - name: execute the

我创建了一个Ansible playbook,它只检查与特定端口上的目标主机的连接。 这是我的剧本

- name: ACL check to target machines.
  hosts: all
  gather_facts: False

  vars:

    target_hosts:
      - server1 18089
      - server1 8089
      - server1 18000

  tasks:
    - name: execute the command
      command: "nc -vz {{ item }}"
      with_items: "{{ target_hosts }}"
当我执行剧本时得到的输出既包含更改(成功)也包含失败

在实际场景中,我有许多目标主机,我的输出非常大

这里我想要的是,我想在底部有一个最终报告,它只显示源和目标之间所有失败连接的列表

谢谢

我想在底部有一个最终报告,它只显示源和目标之间所有失败连接的列表

我相信你要找的旋钮是。虽然我没有看到一个能完全按照你的意愿去做,但有两个似乎能让你接近:

一个声明它只会发出失败和更改的事件:

$ ANSIBLE_STDOUT_CALLBACK=actionable ansible-playbook ...
然后,向上移动复杂度阶梯,正如其名称所示,它将发出JSON中的步骤记录,允许您过滤输出,以获得您想要的内容(
。failed
是每个任务上的一个布尔值,表示:


然后,正如“插件”部分所暗示的那样,如果您愿意,您也可以实现您自己的插件,它完全满足您的需求;除了文档之外,还提供了一些文档。

我有一个类似的剧本,希望得到相同的失败连接列表,我要做的是:

  • 保存输出命令
    寄存器:nc
  • 添加
    ignore\u错误:true
  • 在调试部分,显示请求返回代码的ok和failed连接
我的剧本:

---
  - name: test-connection
    gather_facts: false
    hosts: "{{ group_hosts }}"
    tasks:
      - name: test connection 
        shell: "nc -w 5 -zv {{ inventory_hostname }} {{ listen_port }}"
        register: nc
        ignore_errors: true

      - name: ok connections
        debug: var=nc.stderr
        when: "nc.rc == 0"

      - name: failed connections
        debug: var=nc.stderr
        when: "nc.rc != 0"
失败连接的输出示例:

TASK [failed connections] **********************************************************************************************
ok: [10.1.2.100] => {
    "nc.stderr": "nc: connect to 10.1.2.100 port 22 (tcp) timed out: Operation now in progress"
}
ok: [10.1.2.101] => {
    "nc.stderr": "nc: connect to 10.1.2.101 port 22 (tcp) timed out: Operation now in progress"
}
ok: [10.1.2.102] => {
    "nc.stderr": "nc: connect to 10.1.2.102 port 22 (tcp) timed out: Operation now in progress"

请不要发布文字截图;这会减少问题的可搜索性,并且通常会增加阅读问题所需的工作量
TASK [failed connections] **********************************************************************************************
ok: [10.1.2.100] => {
    "nc.stderr": "nc: connect to 10.1.2.100 port 22 (tcp) timed out: Operation now in progress"
}
ok: [10.1.2.101] => {
    "nc.stderr": "nc: connect to 10.1.2.101 port 22 (tcp) timed out: Operation now in progress"
}
ok: [10.1.2.102] => {
    "nc.stderr": "nc: connect to 10.1.2.102 port 22 (tcp) timed out: Operation now in progress"