How to filter Play Recap after run Playbook in Ansible

Hi guys! I’m new to Ansible and i’m using it for a project where i run my playbook in 1024 hosts. I managed to get my playbook running well but right now i need a way to filter the Play Recap that’s showed after running the playbook.

PLAY RECAP ********************************************************************************************

100.80.80.1                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=2
100.80.80.3                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=2
100.80.80.4                : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
100.80.80.5                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=2
100.80.80.6                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=2
100.80.80.7                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=2
100.80.80.8                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=2
100.80.80.9                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=2

This is a example of what it shows in my Play Recap, in this example i have only 8 hosts so it’s easy to see what host changed or failed, the problem it’s when i run my real playbook over the 1024 hosts and my play recap goes to 1024 lines and it’s almost impossible to go reading every line to see what changed or failed.

So my question is, how do i filter the Play Recap to only show changed and failed hosts? Hiding OK and Skipped

I tried to use “display_skipped_hosts= False” and “display_ok_hosts= False” in my ansible.cfg but nothing changes.

ansible [core 2.12.10]
Debian 11

In my script there’s 3 tasks only:

  1. The first ping the host to see if he is up, if it’s then it’s changed, if it’s not then it’s ignored.
  2. The second task group only the hosts that responded to the ping, so if it was ignored on the task 1 it will be ignored again, if it was changed on the task 1 it will be changed again.
  3. Than the 3 task applies only to hosts grouped in task 2, and here it’s the only task where i perform changes in the hosts, so or it goes changed or failed in this task.

Appreciate the help!

If I were you I’d set the output to JSON and then write something to parse that, for example in ansible.cfg:

[defaults]
callback_result_format = json
stdout_callback = ansible.posix.json

I’ve used this approach so Ansible can be run locally to run Ansible on a remote server using the chroot connection have the result from running of each role returned, see here for example.

3 Likes

Thanks for the answer man. I bet that this should work but unfortunately i don’t know how to programming in JSON, i came from a network field and programming it’s not my strength haha

I tried to filter that using the “cat log | grep changed=3” and it does work but my log it’s storing data of everything in just one file so when i use cat i’m seeing logs from olders playbooks runs. Can i have log file per playbook run? if could do that it will be easy to filter the log using “cat”

The logging documentation doesn’t indicate a method for a separate log for each run, but you could update the log_path in ansible.cfg prior to each run?

I think you need to change your approach. The recap, imo, is not where you should be trying to parse through. For instance, let’s say I have a playbook that deploys a template for the first task. Well if Ansible cannot connect to that host, it’s going to be marked as failed. If it’s able to deploy that template, that particular task will be successful. The approach I would recommend is create list of hosts that are unreachable and another list of reachable.

- name: create a list of failed hosts
  ansible.builtin.set_fact:
     my_failed_hosts: "{{ ansible_play_hosts_all|difference(ansible_play_hosts) }}"

- name: Print failed hosts
  ansible.builtin.debug:
     var: my_failed_hosts | list

You can do the inverse of that to get the successful hosts.

2 Likes

Thanks for the help and great suggestions guys!

I find a solution for what i need and here goes the instructions for anyone with the same need as i.

Replace all fields with marked with ()


cd /etc/ansible/playbooks
ansible-playbook (playbook_name).yaml | tee /etc/ansible/log/(log_name).txt

I go to my playbooks directory and exec my playbook using “tee” to set a specific log file


cd /etc/ansible/log
sed -n '/PLAY RECAP/,/-0300/p' (log_name).txt | grep -v "ok=2"

Then i go to directory of my file and using the command sed along with grep i filter only the lines with actual changes in hosts because it’s what i needed, but you can tweak for your needs.


And that is it! You should see a output like that:

    PLAY RECAP *********************************************************************
100.80.80.4                : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Playbook run took 0 days, 0 hours, 0 minutes, 19 seconds

That way i solved two problems at once which was a way to filter changed hosts and a way to have one log per playbook run.

3 Likes

Hey,

Glad you figured something out ! :slight_smile:

May I also suggest you to have a look on diy callback plugin; specifically display_ok_hosts and display_skipped_hosts parameters ?

3 Likes

Thanks for the reply!
I will check it out too!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.