Reputation: 21
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
Upvotes: 2
Views: 7423
Reputation: 555
The answer by @mdaniel on 2018 Aug 18 is unfortunately no longer correct as the 'actionable' callback has been removed:
community.general.actionable has been removed. Use the 'default' callback plugin with 'display_skipped_hosts = no' and 'display_ok_hosts = no' options. This feature was removed from community.general in version 2.0.0. Please update your playbooks.
We are to use the default callback with some parameters instead.
I used this in my ansible.cfg
stdout_callback = default
display_skipped_hosts = no
display_ok_hosts = no
It worked for the live output but still showed all the ok and skipped hosts in the summary at the end.
Also, $ DISPLAY_SKIPPED_HOSTS=no DISPLAY_OK_HOSTS=no ansible-playbook ...
didn't seem to work which is unfortunate because I prefer the yaml
callback normally and only occasionally want to override this with the above.
Upvotes: 3
Reputation: 15
I have a similar playbook and want the same list of failed connection, what I do is:
register: nc
ignore_errors: true
My playbook:
---
- 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"
An output example for the failed connections:
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"
Upvotes: 0
Reputation: 33203
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.
Upvotes: 3