# how to get the total failed count using ansible

**URL:** <https://forum.ansible.com/t/how-to-get-the-total-failed-count-using-ansible/30247>\
**Category:** Ansible Project\
**Created:** [May 2, 2019, 11:56am UTC](https://forum.ansible.com/t/how-to-get-the-total-failed-count-using-ansible/30247 "2019-05-02T11:56:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bubunia\_Patra](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@Bubunia\_Patra](https://forum.ansible.com/u/Bubunia_Patra)\
**Post date:** [May 2, 2019, 11:56am UTC](https://forum.ansible.com/t/how-to-get-the-total-failed-count-using-ansible/30247/1 "2019-05-02T11:56:51Z")

</div>

Hi all,

I am writing a ansible playbook which will gather all the failed count from script and after sum it will display in the report.

For example: shell script fails that is called from the ansible fails with exit 1, 2 for different cases with diff error message after the script runs in different hosts.

I want to gather the total number of failed count(exit 1 + exit 2) and display it in stout. Can I use failed\_when and changed\_when to achieve this? How do I get the sum?

I want to handle this with in ansible to have a better control rather than in shell script. How can I achieve this?

If it's possible, Can anyone provide a small example to achieve this?

Regards  
Pradeep

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 8, 2019, 7:32pm UTC](https://forum.ansible.com/t/how-to-get-the-total-failed-count-using-ansible/30247/2 "2019-05-08T19:32:34Z")

</div>

I don't understand what you are trying to do, but can give you answer on how to get the error code.  
If you use "register: myvar" on a shell task the variable "myvar.rc" contains the return code.

Since Ansible stops a play on a host when rc \> 0 you need to use "failed\_when: false" or "ignore\_errors: true".

---

<div class="post-metadata">

**Author:** ![Bubunia\_Patra](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@Bubunia\_Patra](https://forum.ansible.com/u/Bubunia_Patra)\
**Post date:** [May 9, 2019, 3:50pm UTC](https://forum.ansible.com/t/how-to-get-the-total-failed-count-using-ansible/30247/3 "2019-05-09T15:50:34Z")

</div>

Thanks for your help.

Actually I am already doing that as you suggested. What I need is as follows:

1. Shell Script of ansible role → exit 1 for certain condition for m nodes for example
2. Shell Script of ansible role → exit 2 for another condition for n nodes for example

Both 1 and 2 are failure condition and it should return in the failed output. But I need to separately show in the fail report. I need to show cumulative report also i.e how many (1) and (2) occurred when the ansible playbook is run. The idea is to sum (1st Condition) +( 2nd Condition) and display the output. I am not sure how I can calculate the sum of failed conditions (m+n). I want to handle this in ansible rather than in shell script returning the list containing the number of counts which is more clean way to handle this. How can I achieve this? If you have any idea please let me know. Any code snippet will help.
