Skip to main content

How to use Ansible to send an email using Gmail

Here's a brief introductory article that describes how to configure Gmail with Ansible.
Image
Using Ansible to configure Gmail

Photo by Laura Stanley from Pexels

A lot of people use Gmail daily to send and receive mail. The estimated number of global users in 2020 was 1.8 billion. Gmail works on the SMTP protocol over port number 587. In this article, I demonstrate how to configure your SMTP web server and send mail automatically from Ansible and using ansible-vault to secure passwords.

Encrypt your password file

The ansible-vault command creates an encrypted file where you can store your confidential details.

ansible-vault create secret.yml
Image
Create secret
(Sarthak Jain, CC BY-SA 4.0)

[ You might also like: A sysadmin's guide to configuring an email server ]

Gmail setup

By default, Gmail doesn’t permit Less secure app access to their web server. For this, you have a solution: You can change the setting to enable Less secure app access.

Go to your Google account:

Image
Google account
(Sarthak Jain, CC BY-SA 4.0)

Click on Security and scroll down:

Image
Gmail security settings
(Sarthak Jain, CC BY-SA 4.0)

Click on Less secure app access and allow it:

Image
Provide less secure app access
(Sarthak Jain, CC BY-SA 4.0)

Email Playbook

Configure the localhost as your web server, which is working on SMTP protocol over port 587. Username, password, and to fields are the Ansible modules. Subject and body are modules in which you can provide our subject and body content. The following is the email.yml file.

---
- hosts: localhost
  vars_files: secret.yml
  tasks:
  - name: Sending an email using Ansible
    mail:
      host: smtp.gmail.com
      port: 587
      username: 500069614@stu.upes.ac.in
      password: "{{ p }}"
      to: mrsarthak001@gmail.com
      subject: Email By Ansible
      body: Test successful
      delegate_to: localhost

Run the playbook

Run the playbook with the given command below. Use –ask-vault-pass flag while running the playbook with this keyword Ansible ask vault password and run your playbook.

ansible-playbook –ask-vault-pass email.yml
Image
Send email
(Sarthak Jain, CC BY-SA 4.0)

The email is sent successfully:

Image
Email sent by Ansible
(Sarthak Jain, CC BY-SA 4.0)

[ A free guide from Red Hat: 5 steps to automate your business. ] 

Wrap up

These steps demonstrate how to configure Gmail to send email messages from Ansible. In addition, you can secure your passwords in Ansible Vault. 

Image
Galaxy
Ansible Galaxy is a repository for Ansible Roles that are available to drop directly into your Playbooks to streamline your automation projects.
Topics:   Linux   Linux administration   Ansible  
Author’s photo

Sarthak Jain

Sarthak Jain is a Pre-Final Year Computer Science undergraduate from the University of Petroleum and Energy Studies (UPES). He is a cloud and DevOps enthusiast, knowing various tools and methodologies of DevOps. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.