# Is it possible to SKIP task programmatically? Using callback module?

**URL:** <https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140>\
**Category:** Ansible Project\
**Created:** [November 2, 2020, 12:49pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140 "2020-11-02T12:49:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_C](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@Luke\_C](https://forum.ansible.com/u/Luke_C)\
**Post date:** [November 2, 2020, 12:49pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/1 "2020-11-02T12:49:32Z")

</div>

Hey guys

I would like to ask whether it’s possible to SKIP task programmatically, using python & callback module?

I’ve been trying to do this for 3 days and I still don’t know how.  
Is there some special variable which I could use in **v2\_runner\_on\_start** or **v2\_playbook\_on\_task\_start** methods?

I don’t want to use ansible’s when condition, task should be omitted dynamically basing on the results gathered earlier.

e.g.:  
def v2\_runner\_on\_start(self, host, task):  
if self.checksomething():  
print(“Task should be omitted”)  
\<OMIT TASK, DO NOT RUN IT\>  
else:  
print(“Yes, task should be executed, proceed”)

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 2, 2020, 12:57pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/2 "2020-11-02T12:57:01Z")

</div>

> Hey guys
> 
> I would like to ask whether it's possible to SKIP task programmatically, using python & callback module?
> 
> I've been trying to do this for 3 days and I still don't know how.  
> Is there some special variable which I could use in \*v2\_runner\_on\_start \*or \*v2\_playbook\_on\_task\_start\* methods?
> 
> I don't want to use ansible's when condition, task should be omitted dynamically basing on the results gathered earlier.
> 
> e.g.:  
> def v2\_runner\_on\_start(self, host, task):  
> if self.checksomething():  
> print("Task should be omitted")  
> \<OMIT TASK, DO NOT RUN IT\>  
> else:  
> print("Yes, task should be executed, proceed")

Do you know that Ansible's when conditions can be very well based on results gathered earlier?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Luke\_C](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@Luke\_C](https://forum.ansible.com/u/Luke_C)\
**Post date:** [November 2, 2020, 1:08pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/3 "2020-11-02T13:08:26Z")

</div>

Thanks for the reply.  
Yes I know … 🙂

The problem is that I want to have dynamic when conditions for huge amount of tasks.  
With standard when condition I would have to provide a particular static condition for each task (which in fact are mostly external script executions).  
I thought it would be just easier and faster with additional callback module.

Kind regards  
Luke

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 2, 2020, 1:19pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/4 "2020-11-02T13:19:27Z")

</div>

> Thanks for the reply.  
> Yes I know ... 🙂
> 
> The problem is that I want to have dynamic when conditions for huge amount of tasks.  
> With standard when condition I would have to provide a particular static condition for each task (which in fact are  
> mostly external script executions).  
> I thought it would be just easier and faster with additional callback module.
> 
> Kind regards  
> Luke

Can you give an example? Speed is probably not the issue with the when conditions if they rely on external scripts.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [November 2, 2020, 1:39pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/5 "2020-11-02T13:39:39Z")

</div>

> Thanks for the reply.  
> Yes I know ... 🙂
> 
> The problem is that I want to have dynamic when conditions for huge amount of tasks.  
> With standard when condition I would have to provide a particular static condition for each task (which in fact are mostly external script executions).  
> I thought it would be just easier and faster with additional callback module.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I too am confused. I use when() to include an entire task file  
or just do a task. And I could swear I have whens that looked like

&nbsp;&nbsp;&nbsp;when:  
&nbsp;&nbsp;&nbsp;&nbsp;- variable-I-populated-or-registered-a-few-lines-ago.stdout ==  
variable-I-populated-in-an-earlier-task-file-on-my-way-here.stdout

---

<div class="post-metadata">

**Author:** ![Luke\_C](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@Luke\_C](https://forum.ansible.com/u/Luke_C)\
**Post date:** [November 2, 2020, 2:30pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/6 "2020-11-02T14:30:33Z")

</div>

All right, so I created a callback plugin which saves information automatically about each executed task to external database, to put it concisely it holds of lots of information about task execution (output, hostname etc) + other needed by users.  
For each task unique identifier (not Anisble TASKUUID) is also generated - it is a combination of few fields.  
That Custom UUID is being saved to that database as well.

The idea is to not execute any tasks in that playbook(s) more than once, for this purpose Custom UUID should be checked. Tasks shouldn’t be executed more than once on a server because of irreversible actions done by those external legacy scripts. (no comments on that, can’t be changed for now)

With when condition it would be problematic to check Custom\_UUID for each task because it’s not and can’t be hardcoded (can differ).  
So, my first thought was to create or rather enhance my callback plugin to GET data from DB to check whether this particular task has been already executed or not.  
If yes, it should be omitted.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 2, 2020, 2:49pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/7 "2020-11-02T14:49:33Z")

</div>

> All right, so I created a callback plugin which saves information automatically about each executed task to external  
> database, to put it concisely it holds of lots of information about task execution (output, hostname etc) + other needed  
> by users.  
> For each task unique identifier (not Anisble TASKUUID) is also generated - it is a combination of few fields.  
> That Custom UUID is being saved to that database as well.
> 
> The idea is to not execute any tasks in that playbook(s) more than once, for this purpose Custom UUID should be checked.  
> Tasks shouldn't be executed more than once on a server because of irreversible actions done by those external legacy  
> scripts. (no comments on that, can't be changed for now)
> 
> With when condition it would be problematic to check Custom\_UUID for each task because it's not and can't be hardcoded  
> (can differ).  
> So, my first thought was to create or rather enhance my callback plugin to GET data from DB to check whether this  
> particular task has been already executed or not.  
> If yes, it should be omitted.

I'm not sure if your setup really makes sense ... it has a smell of overengineering. Ansible is suppose to at least  
go through the tasks to ensure that to ascertain that the desired status has been reached.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Luke\_C](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@Luke\_C](https://forum.ansible.com/u/Luke_C)\
**Post date:** [November 2, 2020, 3:15pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/8 "2020-11-02T15:15:26Z")

</div>

Yes, that’s true and I am aware of that. However, just like I said I though it would be quick and easy 🙂

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [November 2, 2020, 10:27pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/9 "2020-11-02T22:27:37Z")

</div>

What I’ve done for cases like this is use “when:” blocks on an include to pull in (or not) additional tasks.

From [the ‘include:’ module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_module.html):

```auto

- name: Include task list in play only if the condition is true
include: "{{ hostvar }}.yaml"
static: no
when: hostvar is defined

```

Or if you don’t have them broken out well, you can use Ansible blocks with “when:” conditionals as shown in [the block documentation](https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html#grouping-tasks-with-blocks).

I’m kind of an Ansible purist, so you mention of the callback updating/querying a database raised concerns.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 3, 2020, 3:38pm UTC](https://forum.ansible.com/t/is-it-possible-to-skip-task-programmatically-using-callback-module/34140/10 "2020-11-03T15:38:47Z")

</div>

short answer to subject: no, callbacks cannot influence play flow

As for your particular case i would argue that most modules are  
designed to prevent doing the same action x2 on the target machines,  
if using shell/command look at creates/removes checks on sideeffects.  
I don't think this is something you want to build into the controller  
but into each action as it is sometimes too hard for the controller to  
verify such things.

For example, if the task succeeds but then the update to your database  
fails, you will re-execute the task, while if the action itself  
verifies 'do i need to execute' by examining the expected state, you  
are always safe to run the play no matter what. What you are  
attempting to do goes against the design of Ansible itself and it's  
actions, so i expect it to be full of problems going forward.

jic you still insist on this direction, here are some tips:

possible solution: strategy plugin, these are the plugins designed to  
control play/task flow.

other info: tasks already have a uuid but it is generated on task  
definition, which might mean you will see it more than one time,  
example: execute same role x2 (task definitions are the same).
