# Abort Playbook on Play Failure

**URL:** <https://forum.ansible.com/t/abort-playbook-on-play-failure/20106>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [December 31, 2015, 7:40pm UTC](https://forum.ansible.com/t/abort-playbook-on-play-failure/20106 "2015-12-31T19:40:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Lucas](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Lucas](https://forum.ansible.com/u/James_Lucas)\
**Post date:** [December 31, 2015, 7:40pm UTC](https://forum.ansible.com/t/abort-playbook-on-play-failure/20106/1 "2015-12-31T19:40:25Z")

</div>

I’m trying to use Ansible to automate the creation of Amazon machine images. The basic process is: 1) create an instance 2) run ansible play on that instance 3) convert that instance to an AMI

Right now I’m cramming all of these steps into one playbook with three separate plays, primarily because the AWS API is run against the host ‘localhost’ whereas provisioning the instance with Ansible requires pointing Ansible to the instance. The problem is that the ‘convert instance to ami’ play will run regardless of whether the previous play succeeded or failed. I only want to create the AMI if the instance was actually successfully provisioned, but I haven’t found a good way to do it. Playbook looks like this:

---

<div class="post-metadata">

**Author:** ![chrismeyersfsu](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chrismeyersfsu/32/1016_2.png) [@chrismeyersfsu](https://forum.ansible.com/u/chrismeyersfsu)\
**Post date:** [December 31, 2015, 10:08pm UTC](https://forum.ansible.com/t/abort-playbook-on-play-failure/20106/2 "2015-12-31T22:08:04Z")

</div>

Use ansible maximum fail percentage and set it to 1% (or 0% if 0 isn't treated special). This way, if any tasks fail on the play executing on the play running against the nodejs group, the Ami play following will not run. [http://docs.ansible.com/ansible/playbooks\_delegation.html#maximum-failure-percentage](http://docs.ansible.com/ansible/playbooks_delegation.html#maximum-failure-percentage)

---

<div class="post-metadata">

**Author:** ![James\_Lucas](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Lucas](https://forum.ansible.com/u/James_Lucas)\
**Post date:** [January 4, 2016, 5:13pm UTC](https://forum.ansible.com/t/abort-playbook-on-play-failure/20106/3 "2016-01-04T17:13:26Z")

</div>

Thanks for the suggestion. Unfortunately, this does not work. I tried your suggestion:

- name: run ansible on all hosts in group  
become: yes  
hosts: nodejs  
max\_fail\_percentage: 1  
roles:
- nodejs

… as well as the ‘any\_errors\_fatal’ option, like this:

- name: run ansible on all hosts in group  
become: yes  
hosts: nodejs  
any\_errors\_fatal: True  
roles:
- nodejs

However, regardless of failure on the play run against nodejs, the playbook continues running straight into the ‘convert instance to ami’ play:

- name: convert instance to ami  
hosts: localhost  
vars:  
region: us-west-2  
tasks:
- name: build ami  
ec2\_ami:  
name: amlx-nodejs  
description: basic nodejs image  
instance\_id: “{{item.id}}”  
region: “{{region}}”  
wait: yes  
with\_items: ec2.tagged\_instances

Is this the way Ansible is supposed to work? Should I be separating this out into separate playbooks and having a service like Jenkins manage running multiple playbooks?

---

<div class="post-metadata">

**Author:** ![James\_Lucas](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Lucas](https://forum.ansible.com/u/James_Lucas)\
**Post date:** [January 6, 2016, 5:36am UTC](https://forum.ansible.com/t/abort-playbook-on-play-failure/20106/4 "2016-01-06T05:36:56Z")

</div>

My solution has been to build testing into the image-building step, so:

1. create instance
2. run nodejs ansible role on instance
3. bake instance into image… but don’t do it if port 80 isn’t open on the instance:

- name: convert instance to ami

hosts: localhost  
vars:  
region: us-west-2  
tags: { type: nodejs-pre-image }

# check that the web daemon will actually start before building the image

# – this test could definitely be more comprehensive, ie, with uri module

tasks:

- name: wait for port 80 open  
wait\_for:  
host: “{{item.private\_ip}}”  
port: 80  
with\_items: ec2.tagged\_instances
- name: build ami  
ec2\_ami:

[… and so on …]

This has the added benefit of forcing me to do what I’ve been meaning to do anyway, build some testing into my Ansible scripts. That’s a good thing!
