# How to figure out in callback plugin if Ansible runs in check-mode?

**URL:** <https://forum.ansible.com/t/how-to-figure-out-in-callback-plugin-if-ansible-runs-in-check-mode/20771>\
**Category:** Ansible Developer\
**Created:** [March 4, 2016, 12:19pm UTC](https://forum.ansible.com/t/how-to-figure-out-in-callback-plugin-if-ansible-runs-in-check-mode/20771 "2016-03-04T12:19:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Daniel\_Schroeder](https://forum.ansible.com/u/Daniel_Schroeder)\
**Post date:** [March 4, 2016, 12:19pm UTC](https://forum.ansible.com/t/how-to-figure-out-in-callback-plugin-if-ansible-runs-in-check-mode/20771/1 "2016-03-04T12:19:21Z")

</div>

In other plugin types there is something like _self.\_play\_context.check\_mode_ available. I can’t figure out how to check this in a callback plugin though. All callback plugins which ship with Ansible 2 do not prevent any execution when check-mode is on.

This is quite important for notification type callbacks. You probably do not want to send emails, hipchat messages etc when the playbook is executed with _–check_

When inspecting the _play_ parameter of the _callback v2\_playbook\_on\_play\_start_ it appears it has a _check\_mode_ attribute:

`def v2_playbook_on_play_start(self, play): pprint(dir(play))`

But any attempt to use it end in

> [WARNING]: Error when using \<bound method CallbackModule.v2\_playbook\_on\_play\_start of \<my.CallbackModule object at 0x2a0e050\>\>: ‘check\_mode’

Also looking at the class there is no such method or property defined. No idea why dir() would show that.

_lib/ansible/playbook/play\_context.py_ seems the only class which provides this info but it seems to be not available anywhere in the callback module.

Can anyone help with this?

Thanks,  
Daniel

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 4, 2016, 1:13pm UTC](https://forum.ansible.com/t/how-to-figure-out-in-callback-plugin-if-ansible-runs-in-check-mode/20771/2 "2016-03-04T13:13:41Z")

</div>

In your callback file add the following to your imports:

try:  
from **main** import cli  
except ImportError:  
cli = False

Then in **init** for your CallbackModule add something like:

if cli and cli.options.check:  
self.disabled = True

---

<div class="post-metadata">

**Author:** ![Daniel\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Daniel\_Schroeder](https://forum.ansible.com/u/Daniel_Schroeder)\
**Post date:** [March 4, 2016, 1:20pm UTC](https://forum.ansible.com/t/how-to-figure-out-in-callback-plugin-if-ansible-runs-in-check-mode/20771/3 "2016-03-04T13:20:12Z")

</div>

Awesome, that indeed works. Thank you Matt!

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [March 4, 2016, 4:44pm UTC](https://forum.ansible.com/t/how-to-figure-out-in-callback-plugin-if-ansible-runs-in-check-mode/20771/4 "2016-03-04T16:44:23Z")

</div>

To add to this, if you are ever planning to use python to call a playbook (with PlaybookCLI) that uses a callback that does this, you will need to add cli and mycli to your **main**.

e.g. make this a global variable:

`cli = PlaybookCLI(['ansible-playbook', '-i', 'hosts', 'site.yml', '--check'])`

That way, cli will still show up **main** so you won’t get an import error.
