This page looks best with JavaScript enabled

Sending Mail with Ansible

 ·  ☕ 1 min read  ·  🦉 admin

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:

1
[root@7133075243 ~]# ansible-playbook main.yml -i inventory/coreutils.ini -e INV=inventory/coreutils.ini -k -s

send_mail.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
---
- name: 'Sending Mail'
  mail:
  host: 10.19.9.5
  port: 25
  subject: OS Updates
  from: technologymanagement@manintheit.org (OSupdate)
  to: PO@manintheit.org
  charset: utf8
  body: "{{ lookup('file', '/home/ansible/playbooks/inventory/body.txt','{{ INV }}') }}"
  run_once: True
  delegate_to: localhost

body.txt

1
2
3
4
Hello all,
There will be an OS update in short time. No server outage expected.
In case of problem please send mail to technologymanagement@manintheit.org. Please find affected servers below.
.

main.yml

1
2
3
4
5
6
7
---
- 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)

1
2
3
4
5
[targets]
server1
server2
server3

Sample Mail Output:

1
2
3
4
5
Hello all, 
There will be an OS update in short time. No server outage expected. 
In case of problem please send mail to technologymanagement@manintheit.org. Please find 
affected servers below. 
. 
1
2
3
4
[targets]
server1
server2
server3

Happy mailing and patching :)

Share on

manintheit
WRITTEN BY
admin
IT Engineer