订阅 RSS 源

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
Create secret

[ 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:

Google account

Click on Security and scroll down:

Gmail security settings

Click on Less secure app access and allow it:

Provide less secure app access

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: [email protected]
      password: "{{ p }}"
      to: [email protected]
      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
Send email

The email is sent successfully:

Email sent by Ansible

[ 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. 


关于作者

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.

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来