# Immediate notifies?

**URL:** <https://forum.ansible.com/t/immediate-notifies/12254>\
**Category:** Ansible Project\
**Created:** [January 26, 2013, 11:59pm UTC](https://forum.ansible.com/t/immediate-notifies/12254 "2013-01-26T23:59:57Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [January 26, 2013, 11:59pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/1 "2013-01-26T23:59:57Z")

</div>

I'm writing a bunch of tasks and playbooks to bring up new VM nodes:

1) installing postfix  
2) modifying /etc/postfix/main.cf to configuring it as a satellite  
&nbsp;&nbsp;&nbsp;&nbsp;a) register a handler to restart postfix  
3) install apt-listchanges  
4) apt-get update  
5) apt-get -y dist-upgrade

However, it's only after 5 does the postfix handler fire, so the email that apt-listchanges sends goes to /var/mail/root instead of me. So I'm now using:

- name: write /etc/aliases to forward all email to me.  
&nbsp;&nbsp;&nbsp;action: lineinfile dest=/etc/aliases regexp="^root:.\*" line=root:blair@orcaware.com backup=yes  
&nbsp;&nbsp;&nbsp;register: last\_result

- name: rebuild /etc/aliases.db  
&nbsp;&nbsp;&nbsp;action: command /usr/bin/newaliases  
&nbsp;&nbsp;&nbsp;only\_if: '${last\_result.changed}'

Is there an idiom for notify that we can use an immediate notify to fire the handler then instead of writing register and only\_if? Is there an 'immediate\_notify'?

Blair

---

<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:** [January 27, 2013, 2:10am UTC](https://forum.ansible.com/t/immediate-notifies/12254/2 "2013-01-27T02:10:23Z")

</div>

There is nothing better than what you have, well, "when\_boolean" is  
better, but I would like to make this more idiomatic

Some sort of syntactic sugar where you give it the name of the task  
could be interesting

- name: name of task  
&nbsp;&nbsp;action: foo

- name: name of notifier  
&nbsp;&nbsp;when\_changed: name of task

In this case, we would, however, have to register the result of every  
single task automagically. Eats a bit more memory. Maybe we don't  
care.

Thoughts? (Daniel? Stephen? others?)

---

<div class="post-metadata">

**Author:** ![fdavis](https://avatars.discourse-cdn.com/v4/letter/f/a698b9/32.png) [@fdavis](https://forum.ansible.com/u/fdavis)\
**Post date:** [January 27, 2013, 8:41pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/3 "2013-01-27T20:41:42Z")

</div>

If we are only interested in the last task, maybe we register that automatically?  
Then last\_task gets overwritten on each task.

- name: name of task  
action: foo

- name: name of notifier  
when\_changed: $last\_task

---

<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:** [January 27, 2013, 9:02pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/4 "2013-01-27T21:02:02Z")

</div>

We used to register $last\_result exactly like this.

I think I'm fine with reinstating it.

---

<div class="post-metadata">

**Author:** ![fdavis](https://avatars.discourse-cdn.com/v4/letter/f/a698b9/32.png) [@fdavis](https://forum.ansible.com/u/fdavis)\
**Post date:** [January 27, 2013, 10:46pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/5 "2013-01-27T22:46:39Z")

</div>

On Ansible’s Devel branch you should also be able to use  
when\_changed: $last\_result  
instead of  
only\_if: ‘${last\_result.changed}’

---

<div class="post-metadata">

**Author:** ![Andrew\_Berman](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Andrew\_Berman](https://forum.ansible.com/u/Andrew_Berman)\
**Post date:** [January 31, 2013, 9:09pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/6 "2013-01-31T21:09:27Z")

</div>

I just tried using this with the most current devel branch and it didn’t work as expected. I had to manually register last\_result for it to work, but also there is an inherent issue in this. If the task you want the last result from is skipped (in the case of using command and the creates attribute), you do not get a last\_result variable set. Perhaps instead of having the command module skip the task it should register it with “ok”?

Any thoughts?

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![Andrew\_Berman](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Andrew\_Berman](https://forum.ansible.com/u/Andrew_Berman)\
**Post date:** [January 31, 2013, 9:15pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/7 "2013-01-31T21:15:38Z")

</div>

Sorry, what I meant was the last\_result should be registered with no change instead of the last\_result variable not existing.

Thanks,

Andrew

---

<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:** [January 31, 2013, 9:19pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/8 "2013-01-31T21:19:24Z")

</div>

lsat\_result does have to be manually registered, this is still true

register on skipped is a known issue we have to fix for sure

---

<div class="post-metadata">

**Author:** ![Andrew\_Berman](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Andrew\_Berman](https://forum.ansible.com/u/Andrew_Berman)\
**Post date:** [January 31, 2013, 11:23pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/9 "2013-01-31T23:23:57Z")

</div>

Ok, cool. Thanks Michael, loving Ansible!

---

<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:** [January 31, 2013, 11:52pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/10 "2013-01-31T23:52:33Z")

</div>

Andrew,

A work around that worked for me is to declare the variable with a value of zero, so when the task is skipped, it behaves as if the tasks had not run or failed. Look at this thread:

[https://groups.google.com/forum/?fromgroups=#!topic/ansible-project/02Hl4WzRzD0](https://groups.google.com/forum/?fromgroups=#!topic/ansible-project/02Hl4WzRzD0)

---

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [February 1, 2013, 6:39am UTC](https://forum.ansible.com/t/immediate-notifies/12254/11 "2013-02-01T06:39:20Z")

</div>

Why not just use multiple plays.

The first play does the postfix stuff and has a handler at the end of that play.

The second play does the upgrades, etc

Afaik - handlers get run at the end of each play, right?

-sv

---

<div class="post-metadata">

**Author:** ![Andrew\_Berman](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Andrew\_Berman](https://forum.ansible.com/u/Andrew_Berman)\
**Post date:** [February 1, 2013, 8:06am UTC](https://forum.ansible.com/t/immediate-notifies/12254/12 "2013-02-01T08:06:40Z")

</div>

Cool, I’ll use this workaround for now. Thanks!

---

<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:** [February 1, 2013, 1:35pm UTC](https://forum.ansible.com/t/immediate-notifies/12254/13 "2013-02-01T13:35:32Z")

</div>

> Afaik - handlers get run at the end of each play, right?

Yes!
