# Running task when difference filter is empty?

**URL:** <https://forum.ansible.com/t/running-task-when-difference-filter-is-empty/9641>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [October 2, 2024, 7:59pm UTC](https://forum.ansible.com/t/running-task-when-difference-filter-is-empty/9641 "2024-10-02T19:59:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stipemd](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@stipemd](https://forum.ansible.com/u/stipemd)\
**Post date:** [October 2, 2024, 7:59pm UTC](https://forum.ansible.com/t/running-task-when-difference-filter-is-empty/9641/1 "2024-10-02T19:59:37Z")

</div>

I have a playbook to update some servers. I want to send an email when a host fails to update. But I only want to send one email for all hosts that fail to update.

I figured out sending the email and getting the list of failed hosts but I’m having trouble with ONLY sending the email when a host fails. Currently the email sends every time I run a play.

```auto
tasks:
    - name: Update apt repo and cache
      ansible.builtin.apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600

    - name: update apt list
      ansible.builtin.apt:
        update_cache: yes
        cache_valid_time: 3600

    - name: Upgrade packages
      ansible.builtin.apt:
        upgrade: dist
        force_apt_get: yes

    - name: check if reboot required
      register: reboot_required_file
      ansible.builtin.stat:
        path: /var/run/reboot-required
        get_checksum: false

    - name: Send email for failed updates
      # when:
      delegate_to: localhost
      run_once: true
      community.general.mail:
        body: |
          The below servers failed.
          {{ ansible_play_hosts_all|difference(ansible_play_hosts) }}
      ...

```

Is it possible to include a `when:` statement in the email task where it only sends an email if the result of `{{ ansible_play_hosts_all|difference(ansible_play_hosts) }}` is empty?

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [October 2, 2024, 8:06pm UTC](https://forum.ansible.com/t/running-task-when-difference-filter-is-empty/9641/2 "2024-10-02T20:06:21Z")

</div>

> [@stipemd](#):
>
> Is it possible to include a `when:` statement in the email task where it only sends an email if the result of `{{ ansible_play_hosts_all|difference(ansible_play_hosts) }}` is empty?

You want it the other way around, surely? Send an email when there’s a difference?

Either way, it’s possible.

```yaml
# To alert when the difference is non-empty:
when: ansible_play_hosts_all | difference(ansible_play_hosts)
# Or, if you really only want alerts when there are no failures:
when: not ansible_play_hosts_all | difference(ansible_play_hosts)

```

---

<div class="post-metadata">

**Author:** ![stipemd](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@stipemd](https://forum.ansible.com/u/stipemd)\
**Post date:** [October 3, 2024, 1:44pm UTC](https://forum.ansible.com/t/running-task-when-difference-filter-is-empty/9641/3 "2024-10-03T13:44:07Z")

</div>

Yes sorry. I had a brain error. I did want the email when there’s a difference.

---

<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 2, 2024, 1:44pm UTC](https://forum.ansible.com/t/running-task-when-difference-filter-is-empty/9641/4 "2024-11-02T13:44:39Z")

</div>

Closed on November 2, 2024, 1:44pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
