# exec vs. notify - An improvement proposal

**URL:** <https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734>\
**Category:** Ansible Project\
**Created:** [April 29, 2013, 6:25am UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734 "2013-04-29T06:25:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Mills](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@James\_Mills](https://forum.ansible.com/u/James_Mills)\
**Post date:** [April 29, 2013, 6:25am UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/1 "2013-04-29T06:25:09Z")

</div>

Hi all,

I’ve run into a situation that for me demands  
an improvement to “notify” or an additional  
parameter that you can attach to tasks.

e.g:

- name: Build/Install foo  
action: command make install  
exec: refresh ld cache

Where “refresh ld cache” is defined as:

- name: refresh ld cache  
action: command /sbin/ldconfig

Basically as I understand it, notify has a  
many:1 relationship meaning that no matter  
how many tasks refer to a “handler”, it is  
only executed once. More over, it is only  
executed at the end of a play.

This works for the use-case(s) of:

- Restart services (e.g: Apache)
- Reboot

and possibly some others…

But does not work in general for the use case of:

“After this task has successfully completed”  
“Do this task right now”

I’m not sure if notify could be improved to handle thie use-case  
without breaking existing user-cases. But it sounds ot me l ike  
we need an “exec \<task\_name\>” that I described above.

Has anyone else ran into this as well?

What are your thoughts?

Right now, I’m solving this problem by doing  
the following in my roles:

- include: …/…/common/tasks/ldconfig.yml

Where ldconfig.yml looks like:

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [April 29, 2013, 6:53am UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/2 "2013-04-29T06:53:26Z")

</div>

It sounds like you are doing it right to me.

You have an ordered list of dependent tasks. If one task fails, don't  
continue because it'll likely bork you system,  
unless you know for sure its okay, in which case set 'ignore\_errors'.

For more fine grained control, you might want to add something like a

&nbsp;&nbsp;&nbsp;&nbsp;register: foo

for the first task and a

&nbsp;&nbsp;&nbsp;&nbsp;when: foo.changed

to the second task (or appropriate variations).

K

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [April 29, 2013, 6:58am UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/3 "2013-04-29T06:58:33Z")

</div>

​You should do this with a  
&nbsp;&nbsp;register: task​

then putting your exec in a second task with  
when: task.changed  
&nbsp;&nbsp;&nbsp;&nbsp;(1.2 syntax)

Serge

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 29, 2013, 12:25pm UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/4 "2013-04-29T12:25:02Z")

</div>

Correct.

Under 1.1, which does not have the benefit of cleaned up variables, it looks roughly like this:

when\_boolean: ${task.changed}

---

<div class="post-metadata">

**Author:** ![James\_Mills](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@James\_Mills](https://forum.ansible.com/u/James_Mills)\
**Post date:** [April 29, 2013, 12:29pm UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/5 "2013-04-29T12:29:50Z")

</div>

Thank you both for your responses.

Whilst I understand I can “register a variable” and “conditionally perform a task”  
I don’t find this particularly very intuitive. It makes far more sense to me (except  
that notify isn’t designed this way) to “call a task” (i.e: notify) when one taks has  
successfully completed. think of an event rather than a conditional execution.

- name: foo  
action command foo  
register: foo

- name: on foo  
action: foo\_callback  
when: foo.changed

This isn’t particular intuitive. But this is:

- name: foo  
action: command foo  
notify: on foo

- name: on foo  
action: foo\_callback

Except notify isn’t designed like this 😕

cheers  
James

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 29, 2013, 2:13pm UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/6 "2013-04-29T14:13:19Z")

</div>

Understood. One of the the challenges of open source is that things intuitive to one person aren’t intuitive to everyone.

We will be sticking with “when: foo.changed” as that allows you to keep the task at _exactly the point where it will run_ in the task list, which makes things clear, and also allows stuff like:

when: foo.changed and bar.changed and x \> 3

etc

We’ve got thousands of people using things this way and they’ve been ok with it so far.

While I am open to adding new language constructs, we add them rather conservatively, as we aim to support things indefinitely.

---

<div class="post-metadata">

**Author:** ![James\_Mills](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@James\_Mills](https://forum.ansible.com/u/James_Mills)\
**Post date:** [April 30, 2013, 1:51am UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/7 "2013-04-30T01:51:40Z")

</div>

I have ried this using register but I’m unable  
to make this work. See this snippet:

- name: Build/Install GDAL  
action: command make install  
chdir=/usr/local/src/gdal-$gdal\_version  
creates=/usr/local/bin/gdal-config  
register: last\_result

- include: …/…/common/tasks/ldconfig.yml  
when: last\_result.changed

This cannot work because of the “creates” condition  
applied to the build task. the condition “last\_result.changed”  
failed because it’s never defined.

I’m not sure how to solve this.

cheers  
James

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [April 30, 2013, 2:18am UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/8 "2013-04-30T02:18:06Z")

</div>

I often find, with the boolean conditional, I have to do things like

when: last\_result is defined and last\_result.changed

Or variations.

Hope this helps.

K

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [April 30, 2013, 5:05am UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/9 "2013-04-30T05:05:30Z")

</div>

You used to be able to use "when\_set: last\_result", but the new documentation only shows example of "when".

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 30, 2013, 12:31pm UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/10 "2013-04-30T12:31:57Z")

</div>

“This cannot work because of the “creates” condition  
applied to the build task. the condition “last\_result.changed”  
failed because it’s never defined.”

Ok, this is an easy one. I need to make the “skipped” data that returns from the command module also return “changed=False”.

I will make this happen.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 30, 2013, 12:36pm UTC](https://forum.ansible.com/t/exec-vs-notify-an-improvement-proposal/12734/11 "2013-04-30T12:36:11Z")

</div>

Actually this is already done and works fine in latest git for me.

Did you test it on recent code?
