Sending Mail with Ansible
Contents
Hello, In this short post, I will share you simple ansible playbook to send mail. I wrote this playbook informing Project Office and Clients about OS system updates. In order for that Ansible has a mail module. Only things we need to define smtp server, smtp server port, Subject, Mail body, sender and recipients.
Example Usage:
[root@7133075243 ~]# ansible-playbook main.yml -i inventory/coreutils.ini -e INV=inventory/coreutils.ini -k -s
send_mail.yml
---
- name: 'Sending Mail'
mail:
host: 10.19.9.5
port: 25
subject: OS Updates
from: [email protected] (OSupdate)
to: [email protected]
charset: utf8
body: "{{ lookup('file', '/home/ansible/playbooks/inventory/body.txt','{{ INV }}') }}"
run_once: True
delegate_to: localhost
body.txt
Hello all,
There will be an OS update in short time. No server outage expected.
In case of problem please send mail to technolog[email protected]. Please find affected servers below.
.
main.yml
---
- hosts: all
pre_tasks:
- include: send_mail.yml
tasks:
- name: "UPDATE core-utils sec."
shell: yum update --advisory RHSA-2017:2685
coreutils.ini(Ansible host inventory)
[targets]
server1
server2
server3
Sample Mail Output:
Hello all,
There will be an OS update in short time. No server outage expected.
In case of problem please send mail to [email protected]. Please find
affected servers below.
.
[targets]
server1
server2
server3
Happy mailing and patching :)