'How to alert via email in Ansible

I have setup a mail task in ansible to send emails if yum update is marked as 'changed'.

Here is my current working code:

- name: Send mail alert if updated
      community.general.mail:
        to:
        - 'recipient1'
        cc:
        - 'recipient2'
        subject: Update Alert
        body: 'Ansible Tower Updates have been applied on the following system: {{ ansible_hostname }}'
        sender: "[email protected]"
      delegate_to: localhost
      when: yum_update.changed

This works great, however, every system that gets updated per host group sends a separate email. Last night for instance I had a group of 20 servers update and received 20 separate emails. I'm aware of why this happens, but my question is how would I script this to add all the systems to one email? Is that even possible or should I just alert that the group was updated and inform teams of what servers are in each group? (I'd prefer not to take the second option)

Edit 1:

I have added the code suggested and am now unable to receive any emails. Here's the error message:

"msg": "The conditional check '_changed|length > 0' failed. The error was: error while evaluating conditional (_changed|length > 0): {{ hostvars|dict2items| selectattr('value.yum_update.changed')| map(attribute='key')|list }}: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'yum_update'\n\nThe error appears to be in '/tmp/bwrap_1073_o8ibkgrl/awx_1073_0eojw5px/project/yum-update-ent_template_servers.yml': line 22, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Send mail alert if updated\n      ^ here\n",

I am also attaching my entire playbook for reference:

---
- name: Update enterprise template servers
  hosts: ent_template_servers

  tasks:

    - name: Update all packages
      yum:
        name: '*'
        state: latest
      register: yum_update

    - name: Reboot if needed
      import_tasks: /usr/share/ansible/tasks/reboot-if-needed-centos.yml

    - name: Kernel Cleanup
      import_tasks: /usr/share/ansible/tasks/kernel-cleanup.yml

    - debug:
        var: yum_update.changed

    - name: Send mail alert if updated
      community.general.mail:
        to:
        - '[email protected]'
        subject: Update Alert
        body: |-
          Updates have been applied on the following system(s):
          {{ _changed }}
        sender: "[email protected]"
      delegate_to: localhost
      run_once: true
      when: _changed|length > 0
      vars:
        _changed: "{{ hostvars|dict2items|
                      selectattr('yum_update.changed')|
                      map(attribute='key')|list }}"

...

Ansible version is: 2.9.27 Ansible Tower version is: 3.8.3

Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source