Print out only failed tasks - Ansible

2019-08-27 14:19发布

I have created an Ansible playbook which just checks the connection to target hosts on a specific port. Here is my 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 }}"

The output i get when i execute the playbook contains both changed(success) and failed.

In real scenario i have many number of target hosts, & my output is very large.

What i wanted here is, i want to have final report at bottom which shows only list of all failed connections between source and target.

Thanks

标签: ansible
1条回答
ら.Afraid
2楼-- · 2019-08-27 14:39

i want to have final report at bottom which shows only list of all failed connections between source and target

I believe the knob you are looking for is stdout callback plugins. While I don't see one that does exactly as you wish, there are two of them that seem like they may get you close:

The actionable one claims it will only emit Failed and Changed events:

$ ANSIBLE_STDOUT_CALLBACK=actionable ansible-playbook ...

Then, moving up the complexity ladder, the json one will, as its name implies, emit a record of the steps in JSON, allowing you to filter the output for exactly what you want (.failed is a boolean on each task that indicates just that):

$ ANSIBLE_STDOUT_CALLBACK=json ansible-playbook ...

Then, as the "plugins" part implies, if you were so inclined you could also implement your own that does exactly what you want; there are a lot of examples provided, in addition to the docs.

查看更多
登录 后发表回答