# Safer way to fail playbook on callback instead exit(1)

**URL:** <https://forum.ansible.com/t/safer-way-to-fail-playbook-on-callback-instead-exit-1/38375>\
**Category:** AWX Project\
**Tags:** awx\
**Created:** [August 25, 2023, 11:17am UTC](https://forum.ansible.com/t/safer-way-to-fail-playbook-on-callback-instead-exit-1/38375 "2023-08-25T11:17:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vlado\_Portos](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@Vlado\_Portos](https://forum.ansible.com/u/Vlado_Portos)\
**Post date:** [August 25, 2023, 11:17am UTC](https://forum.ansible.com/t/safer-way-to-fail-playbook-on-callback-instead-exit-1/38375/1 "2023-08-25T11:17:31Z")

</div>

Hi all,

I’m running AWX on single node k3s cluster and need to fail a job if “Could not match supplied host pattern, ignoring:…” basically hook into v2\_playbook\_on\_no\_hosts\_matched

I have a callback\_plugin that basically does that a brute force way by exiting 1 but is there any other way to mark the job failed… because if I rise error the job is still marked as Successful in AWX which is nonsense.

from ansible.plugins.callback import CallbackBase  
from ansible.errors import AnsibleError  
from ansible import constants as C

class CallbackModule(CallbackBase):  
CALLBACK\_VERSION = 2.0  
CALLBACK\_TYPE = ‘notification’  
CALLBACK\_NAME = ‘fail\_on\_no\_hosts’

def v2\_playbook\_on\_no\_hosts\_matched(self):

# fail the Ansible tower Job

self.\_display.error(“No hosts matched”)  
exit(1)
