manintheit.org

manintheit.org


Sending Mail with Ansible

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:

ansible-playbook main.yml -i inventory/coreutils.ini -e INV=inventory/coreutils.ini -k -s
---
- 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 [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 inventory)

[targets]
server1
server2
server3

Sample E-Mail

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 E-mailing and patching 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.