How to send email if task fails on something or any particular condition? I

1,052 views
Skip to first unread message

Milan Patel

unread,
Jun 22, 2022, 12:58:55 AM6/22/22
to Ansible Project
---
- name: This playbook is for Testing Disk Space
  hosts: proxy
  become: yes
  become_method: sudo
  serial: 1
  any_errors_fatal: true
  ignore_errors: yes
  gather_facts: yes
  vars:
          ansible_paython_interpreter: /usr/bin/python
  tasks:

       - name: disk usage from command module
         shell: df -h / | tail -n 1 | awk '{ print $5 }'
         register: used_space

       - debug:
           var: used_space.stdout
         register: one

       - name: decision making weather to proceed with update the system or not
         fail: msg="{{ inventory_hostname }} have low disk space. Please Make some space then run the updates."
         when: used_space.stdout >= "70%"
         ignore_errors: no


       - name: update the system
         yum:
           name: "*"
           state: latest
           exclude: 'kernel*'
           update_cache: yes
           update_only: yes

         register: yum_update

       - debug:
           msg: "{{ yum_update }}"

******************************

In the example if disk space is used by 75% then task stops playing but in this case i want to set email notification send to me so how can i do that ?? 

Brian Coca

unread,
Jun 22, 2022, 9:58:37 AM6/22/22
to Ansible Project
You have several options, depending on the scope:

- mail task (trigger when: yum_update is failed/ task in rescue/always
part of a block, disk_usage > 75, etc)
https://docs.ansible.com/ansible/5/collections/community/general/mail_module.html
- mail callback (handles any failures in run)
https://docs.ansible.com/ansible/latest/collections/community/general/mail_callback.html#ansible-collections-community-general-mail-callback

--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages