# iterate over "failed" hosts?

**URL:** <https://forum.ansible.com/t/iterate-over-failed-hosts/16459>\
**Category:** Ansible Project\
**Created:** [October 7, 2014, 7:07pm UTC](https://forum.ansible.com/t/iterate-over-failed-hosts/16459 "2014-10-07T19:07:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [October 7, 2014, 7:07pm UTC](https://forum.ansible.com/t/iterate-over-failed-hosts/16459/1 "2014-10-07T19:07:53Z")

</div>

I’ve got an interesting case: playbook is failing on certain hosts either due to SSH connection issues or because some things are not installed.

I realize that I should really just write a playbook that makes all my machines “compliant” with the playbook and then re-run it. However I am curious whether there is a way to do a “group\_by” or something similar to pick up all the “failed” hosts, group them by cause and then run some actions against them: be it an addition of a record to DB indicating above failure or attempt at resolving an underlying problem.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [October 7, 2014, 7:12pm UTC](https://forum.ansible.com/t/iterate-over-failed-hosts/16459/2 "2014-10-07T19:12:53Z")

</div>

BTW I did read [http://docs.ansible.com/developing\_api.html](http://docs.ansible.com/developing_api.html) and [http://jpmens.net/2012/12/13/obtaining-remote-data-with-ansible-s-api/](http://jpmens.net/2012/12/13/obtaining-remote-data-with-ansible-s-api/) that provide fine examples of doing it via API code. I’m more curious whether I can sneak something in through the playbook…

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 8, 2014, 12:42pm UTC](https://forum.ansible.com/t/iterate-over-failed-hosts/16459/3 "2014-10-08T12:42:30Z")

</div>

When Ansible playbooks fail, Ansible generates a retry file to limit a playbook run to just failed hosts.

You can use this file to just target those specific hosts.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [October 9, 2014, 3:13pm UTC](https://forum.ansible.com/t/iterate-over-failed-hosts/16459/4 "2014-10-09T15:13:18Z")

</div>

I was thinking more “inline” kind of action. While I have all the facts/vars loaded for the entire fleet (in case I need them). Kind of like “branch off to fix things that I know how to fix…” But I guess this goes more and more into the “use Ansible via API” kind of territory?
