0

I am trying to send a mail via ansible yaml that looks similar as below. The requirement is to send this email a week ahead of the patching. I can schedule it in cron, but I need to send an email 1 week prior and put the actual date of the reboot into the message text.

I changed the email ID. I am looking for the date format in the body.

---
- name: sample mail
  mail:
    to:
      - Recipients
      - Pruds <[email protected]>
    subject: Ansible-test-mail for server {{ ansible_hostname }}
    body: ' Hello Team,

            The server {{ ansible_hostname }} will be patched and rebooted on `expr $`date '+%d'` + 7`, `date '+%b'`,`date '+%Y'`. Please be noted. 

            Regards,

            Unix Admins via Ansible
            {{ ansible_hostname }}'

1 Answer 1

2

In order to use variable substitution in a message body you first need to save the date in a variable.

The shell command produces a structure that is saved to a variable reboot_date. The actual output can be accessed through its stdout attribute.

Also note that POSIX date command is capable of computing the date a week in advance correctly without the need to call expr. In fact date would do a much better job rolling over to the next month or year as needed.

- name: Register reboot date
  shell: date -d "today + 7 days" +"%d %b %Y"
  register: reboot_date

Now your mail task needs only to make a reference to {{ reboot_date.stdout }}

- name: sample mail
  mail:
    to:
      - Recipients
      - Pruds <[email protected]>
    subject: "Ansible-test-mail for server {{ ansible_hostname }}"
    body: ' Hello Team,

        The server {{ ansible_hostname }} will be patched 
        and rebooted on {{ reboot_date.stdout }}. Please be noted. 

        Regards,

        Unix Admins via Ansible
        {{ ansible_hostname }}'
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .