Ansible send mail when a pre task is completed

You can use the mail module in Ansible to send an email when a pre-task is completed. Here's an example of how you can do this:

---
- name: Send email when pre-task is completed
  hosts: localhost
  tasks:
  - name: Pre-task
    command: echo "Pre-task completed"
  - name: Send email
    mail:
      subject: Pre-task completed
      body: "The pre-task has been completed successfully"
      from: "[email protected]"
      to: "[email protected]"

In this example, the mail module is used to send an email with the subject "Pre-task completed" and a body that includes the message "The pre-task has been completed successfully". The from and to parameters specify the sender and recipient of the email, respectively.

You can also use the register keyword to store the output of the pre-task in a variable, and then use that variable in the email body. For example:

---
- name: Send email when pre-task is completed
  hosts: localhost
  tasks:
  - name: Pre-task
    command: echo "Pre-task completed"
    register: pre_task_output
  - name: Send email
    mail:
      subject: Pre-task completed
      body: "The pre-task has been completed successfully: {{ pre_task_output.stdout }}"
      from: "[email protected]"
      to: "[email protected]"

In this example, the output of the pre-task is stored in the pre_task_output variable, and then used in the email body using the {{ }} syntax.

You can also use the when keyword to send the email only if the pre-task is successful. For example:

---
- name: Send email when pre-task is completed
  hosts: localhost
  tasks:
  - name: Pre-task
    command: echo "Pre-task completed"
    register: pre_task_output
  - name: Send email
    mail:
      subject: Pre-task completed
      body: "The pre-task has been completed successfully: {{ pre_task_output.stdout }}"
      from: "[email protected]"
      to: "[email protected]"
    when: pre_task_output.stdout == "Pre-task completed"

In this example, the email will only be sent if the output of the pre-task is "Pre-task completed".