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
[ 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:
Click on Security and scroll down:
Click on Less secure app access and allow it:
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
The email is sent successfully:
[ 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.
About the author
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. Sarthak also Mentored more than 2,000 students Regarding the Latest Tech trends through their community Dot Questionmark.
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit