notify on file existence?

169 views
Skip to first unread message

Ben Coleman

unread,
Aug 2, 2021, 11:03:52 AM8/2/21
to Ansible Project
I've got a playbook where a couple of tasks are notifying a 'Reboot
server' handler when a lineinfile generates a change. I *also* want to
notify the same handler when the reboot-required file exists (this is on
Ubuntu). My brain is glitching as to how to get this done.

Essentially I have code:

---------------------
- name: Get reboot-required status
stat: path=/var/run/reboot-required
register: file_reboot_required


- name: trigger Server Reboot when reboot_required exists
# do something to cause notify to happen if reboot_required exists
notify: Reboot server
---------------------

What needs to replace the comment? What can I do with the
file_reboot_required variable that will result in 'Reboot server' being
notified when this file exists? Or should I be taking another approach?

I'm sure I'll have a forehead slap/"D'oh!" moment when this is answered,
but for the moment, my brain isn't going there.

Ben
--
Ben Coleman olo...@benshome.net | For the wise man, doing right trumps
http://oloryn.benshome.net/ | looking right. For the fool, looking
Amateur Radio NJ8J | right trumps doing right.

OpenPGP_signature

Ben Coleman

unread,
Aug 2, 2021, 11:27:05 AM8/2/21
to Ansible Project
On 8/2/2021 11:03 AM, Ben Coleman wrote:
> I've got a playbook where a couple of tasks are notifying a 'Reboot
> server' handler when a lineinfile generates a change.  I *also* want to
> notify the same handler when the reboot-required file exists (this is on
> Ubuntu).  My brain is glitching as to how to get this done.

<quote voice="Gilda Radnor">Never mind</quote>

A breather, a better specified Google, and I"ve found what I need.

Looks like what I want is more like this:

- name: check if a reboot is required
shell: "[ -f /var/run/reboot-required ]"
failed_when: False
register: reboot_required
changed_when: reboot_required.rc == 0
notify: reboot
OpenPGP_signature

Stefan Hornburg (Racke)

unread,
Aug 2, 2021, 11:31:20 AM8/2/21
to ansible...@googlegroups.com
On 02/08/2021 17:26, Ben Coleman wrote:
> On 8/2/2021 11:03 AM, Ben Coleman wrote:
>> I've got a playbook where a couple of tasks are notifying a 'Reboot server' handler when a lineinfile generates a change.  I *also* want to notify the same handler when the reboot-required file exists (this is on Ubuntu).  My brain is glitching as to how to get this done.
>
> <quote voice="Gilda Radnor">Never mind</quote>
>
> A breather, a better specified Google, and I"ve found what I need.
>
> Looks like what I want is more like this:
>
>    - name: check if a reboot is required
>      shell: "[ -f /var/run/reboot-required ]"
>      failed_when: False
>      register: reboot_required
>      changed_when: reboot_required.rc == 0
>      notify: reboot
>
> Ben

Though this is more efficient and easier to understand:

- name: Get reboot-required status
stat: path=/var/run/reboot-required
register: file_reboot_required
changed_when: file_reboot_required.stat.exists
notify: reboot

Regards
Racke

--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.

Reply all
Reply to author
Forward
0 new messages