# Playbook fails if nothing matches?

**URL:** <https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510>\
**Category:** Ansible Developer\
**Created:** [June 15, 2015, 10:18pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510 "2015-06-15T22:18:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajtalbot1](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@ajtalbot1](https://forum.ansible.com/u/ajtalbot1)\
**Post date:** [June 15, 2015, 10:18pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/1 "2015-06-15T22:18:32Z")

</div>

Dumb n00b question. I am trying to run several play books against one system. If any one of the play books fails, Ansible exits with an RC = 1. I think I have it distilled this down to the below code:

[root@pdxvnxutil ansible]# ansible-playbook -l farmer playbooks/test.yml  
SSH password:

PLAY: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [farmer]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
farmer : ok=1 changed=0 unreachable=0 failed=0

[root@pdxvnxutil ansible]# echo $?  
0  
[root@pdxvnxutil ansible]# cat playbooks/test.yml

- hosts: farmer  
[root@pdxvnxutil ansible]#

As farmer does not match fuzzy\_not\_farmer, I would expect the below to fail the first hosts test, but not fail totally out! The play book exits and never gets to the rest of the play book?  
[root@pdxvnxutil ansible]# ansible-playbook -l farmer playbooks/test.yml  
SSH password:

PLAY: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: no hosts matched

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

[root@pdxvnxutil ansible]# echo $?  
1

[root@pdxvnxutil ansible]# cat playbooks/test.yml

- hosts: fuzzy\_not\_farmer (Exits with 1, and never gets to the next host)

- hosts: farmer

[root@pdxvnxutil ansible]#

Any ideas?

---

<div class="post-metadata">

**Author:** ![ajtalbot1](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@ajtalbot1](https://forum.ansible.com/u/ajtalbot1)\
**Post date:** [June 16, 2015, 4:23am UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/2 "2015-06-16T04:23:17Z")

</div>

Bug in Ansible 2.0.0 dev branch.

---

<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 16, 2015, 4:19pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/3 "2015-06-16T16:19:05Z")

</div>

this is by design, if all hosts fail in a play or no hosts are  
matched, ansible ends the execution.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [June 16, 2015, 4:42pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/4 "2015-06-16T16:42:05Z")

</div>

v1 doesnt stop execution if no hosts were matched in the play.

---

<div class="post-metadata">

**Author:** ![ajtalbot1](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@ajtalbot1](https://forum.ansible.com/u/ajtalbot1)\
**Post date:** [June 16, 2015, 4:47pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/5 "2015-06-16T16:47:42Z")

</div>

So, version 1 works great.

I was more worried about v2 failing with rc 1.

If this is a known “feature”, how do I define multiple sets of hosts in one play book; assuming the first set of hosts don’t match the rule and fails?

---

<div class="post-metadata">

**Author:** ![Greg\_DeKoenigsberg1](https://avatars.discourse-cdn.com/v4/letter/g/838e76/32.png) [@Greg\_DeKoenigsberg1](https://forum.ansible.com/u/Greg_DeKoenigsberg1)\
**Post date:** [June 16, 2015, 5:20pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/6 "2015-06-16T17:20:43Z")

</div>

We'll check the behavior and get back to you.

---

<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 16, 2015, 5:35pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/7 "2015-06-16T17:35:34Z")

</div>

Confirmed, this is a bug, a play that doesn't match any hosts to begin  
with should be skipped but subsequent plays should run.

But a play that runs out of hosts due to errors, does interrupt the  
whole playbook run.

---

<div class="post-metadata">

**Author:** ![ajtalbot1](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@ajtalbot1](https://forum.ansible.com/u/ajtalbot1)\
**Post date:** [June 16, 2015, 5:47pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/8 "2015-06-16T17:47:34Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 16, 2015, 5:58pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/9 "2015-06-16T17:58:26Z")

</div>

Just fixed on the devel branch (4b28a51f25226a1c6a86892b774a8bcea5a63883).

Thanks!

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [July 15, 2015, 11:27pm UTC](https://forum.ansible.com/t/playbook-fails-if-nothing-matches/18510/10 "2015-07-15T23:27:11Z")

</div>

A few days ago while my colleague was writing a deploy playbook, he encountered a problem where he wanted the whole playbook to fail if any hos in a play failed, but it didn’t work like that. If at least one host in a play didn’t fail, Ansible continued with the next play. It only stopped if all hosts in a play failed.

Then we tried his playbook with 2.0 and it didn’t work the same. With 2.0 if one host in a play failed, Ansible would stop execution. Is this intentionally, or is it also a bug?

I really hope it is intentionally, since it’s really annoying if you need every host in a play to succeed, to make workaround checks to see if any of the hosts failed.
