Iscriviti per attivare i feed 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. 


Sull'autore

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

Ricerca per canale

automation icon

Automazione

Novità sull'automazione IT di tecnologie, team e ambienti

AI icon

Intelligenza artificiale

Aggiornamenti sulle piattaforme che consentono alle aziende di eseguire carichi di lavoro IA ovunque

open hybrid cloud icon

Hybrid cloud open source

Scopri come affrontare il futuro in modo più agile grazie al cloud ibrido

security icon

Sicurezza

Le ultime novità sulle nostre soluzioni per ridurre i rischi nelle tecnologie e negli ambienti

edge icon

Edge computing

Aggiornamenti sulle piattaforme che semplificano l'operatività edge

Infrastructure icon

Infrastruttura

Le ultime novità sulla piattaforma Linux aziendale leader a livello mondiale

application development icon

Applicazioni

Approfondimenti sulle nostre soluzioni alle sfide applicative più difficili

Virtualization icon

Virtualizzazione

Il futuro della virtualizzazione negli ambienti aziendali per i carichi di lavoro on premise o nel cloud