# how to count success and failure of a task ?

**URL:** <https://forum.ansible.com/t/how-to-count-success-and-failure-of-a-task/24698>\
**Category:** Ansible Project\
**Created:** [June 12, 2017, 12:16pm UTC](https://forum.ansible.com/t/how-to-count-success-and-failure-of-a-task/24698 "2017-06-12T12:16:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurminder\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@Gurminder\_Singh](https://forum.ansible.com/u/Gurminder_Singh)\
**Post date:** [June 12, 2017, 12:16pm UTC](https://forum.ansible.com/t/how-to-count-success-and-failure-of-a-task/24698/1 "2017-06-12T12:16:42Z")

</div>

Hi Guys,

I am verifying Cisco IOS image via this playbook, this runs great. However, I would like to add a count on successful and failure of the task I run. How can I achieve that?

For eg. I have 100 devices to verify code out of which 95 made a successful upgrade and 5 does not make, from the below playbook how can I add a count ? to capture no. of successful upgrade and failure?

Please help ?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 12, 2017, 5:10pm UTC](https://forum.ansible.com/t/how-to-count-success-and-failure-of-a-task/24698/2 "2017-06-12T17:10:40Z")

</div>

If you have the results registered in 'myvar' (assuming loop) you  
should be able to get a count like:

debug: msg="{{(myvar.results|select('failed'))|length}}"

---

<div class="post-metadata">

**Author:** ![Gurminder\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@Gurminder\_Singh](https://forum.ansible.com/u/Gurminder_Singh)\
**Post date:** [June 13, 2017, 2:45pm UTC](https://forum.ansible.com/t/how-to-count-success-and-failure-of-a-task/24698/3 "2017-06-13T14:45:17Z")

</div>

Hi Brian,

I tried your solution, my playbooks run successfully but not giving desired output.

I used it like below in my playbook:-

- name: Standard OS Validation after Upgrade.  
set\_fact:  
standard\_OS: “{{ item.1 }}”  
when:

- item.1 == “15.0(2)SG10,”  
with\_indexed\_items: “{{ sh\_ver.stdout[0].split() }}”

- fail:  
msg:

- “Standard OS is not on this device {{ lookup(‘dig’, hostip, ‘qtype=PTR’) }}”  
when:

- ‘standard\_OS is undefined’  
register: myvar.results

- debug: msg=“{{(myvar.results|select(‘failed’))|length}}”

That’s output I get:-

TASK [fail] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/user/ansible/po-ios-va.yml:36  
skipping: [switch\_A] =\> {“changed”: false, “skip\_reason”: “Conditional check failed”, “skipped”: true}  
fatal: [switch\_B]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: [“Standard OS is not on this device switch\_B.”]}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/user/ansible/po-ios-va.yml:42  
fatal: [switch\_A]: FAILED! =\> {“failed”: true, “msg”: “Unexpected templating type error occurred on ({{ (myvar.results|select("failed"))| length }}): object of type ‘generator’ has no len()”}  
to retry, use: --limit @/home/user/ansible/po-ios-va.retry

Please Suggest.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 13, 2017, 2:54pm UTC](https://forum.ansible.com/t/how-to-count-success-and-failure-of-a-task/24698/4 "2017-06-13T14:54:15Z")

</div>

register: myvar.results \<= this is incorrect, you cannot register to  
a subindex.

correct:  
register: myvar

you get myvar.results IF that task has with\_.

I really don't understand what you are trying to do or why the first  
task would even continue if there are any failed items as that would  
fail the whole task.

---

<div class="post-metadata">

**Author:** ![Gurminder\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@Gurminder\_Singh](https://forum.ansible.com/u/Gurminder_Singh)\
**Post date:** [June 14, 2017, 4:02pm UTC](https://forum.ansible.com/t/how-to-count-success-and-failure-of-a-task/24698/5 "2017-06-14T16:02:37Z")

</div>

sure thing, guess I need work on my code more, it is still workable as expected.

Appreciate your help Brian.
