Configure Postfix RealyHost and send e-mail with the help of Ansible
Postfix is OpenSource Mail Transfer Agent which is used to deliver E-MAIL. In this article I am going to demonstrate how to configure Postfix mail server on multiple nodes running on AWS Cloud. After this sending mail from every node to check mailserver is working or not.
# Requirements
1) AWS Account
2) AWS OS running instances
3) Ansible should be installed
# About Task
1) Configure Postfix RealyHost with the help of Ansible playbook
2) Use Jinja template for configuring Postfix for multiple nodes dynamically
3) Send E-Mails from every nodes to your email address
Step-1: Create playbook for configuring Postfix
a) Create required variables which we will use in playbook
- name: configuring postfix mail server hosts: mailserver vars: pkg: - postfix - cyrus-sasl-plain - mailx svc: postfix
b) Install all required packages and start and enable all required services
tasks: - name: installing {{ pkg }} packages yum: name: "{{ pkg }}" state: present - name: enabling and starting {{ svc }} service service: name: "{{ svc }}" state: started enabled: true
c) Add required lines in /etc/postfix/mail.cf
First create main.cf.j2 file and paste below lines in same directory
Here we using {{ ansible_hostname }} as jinja2 template because for every node hostname is different
myhostname = {{ ansible_hostname }}.example.com relayhost = [smtp.gmail.com]:587 smtp_use_tls = yes smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt smtp_sasl_security_options = noanonymous smtp_sasl_tls_security_options = noanonymous
Now add this lines in playbook
- name: adding required lines in /etc/postfix/main.cf file template: src: main.cf.j2 dest: /etc/postfix/main.cf
d) Configure postfix sasl credentials
replace your_email and your_password with your own email credentials
- name: configuring postfix sasl creds copy: content: '[smtp.gmail.com]:587 your_email:your_password' dest: /etc/postfix/sasl_passwd owner: root group: postfix mode: 0640
e) generate postfix lookup table
- name: command: postmap /etc/postfix/sasl_passwd
f) Restart all required services
- name: enabling and starting {{ svc }} service service: name: "{{ svc }}" state: restarted
At End Playbook will look like this:-
- name: configuring postfix mail server hosts: mailserver vars: pkg: - postfix - cyrus-sasl-plain - mailx svc: postfix tasks: - name: installing {{ pkg }} packages yum: name: "{{ pkg }}" state: present - name: enabling and starting {{ svc }} service service: name: "{{ svc }}" state: started enabled: true - name: adding required lines in /etc/postfix/main.cf file template: src: main.cf.j2 dest: /etc/postfix/main.cf - name: configuring postfix sasl creds copy: content: '[smtp.gmail.com]:587 your_email:your_password' dest: /etc/postfix/sasl_passwd owner: root group: postfix mode: 0640 - name: command: postmap /etc/postfix/sasl_passwd - name: enabling and starting {{ svc }} service service: name: "{{ svc }}" state: restarted
Now run this playbook from your ansible host machine.
In my case inventory file look like this
[mailserver] node1.example.com node2.example.com
And ansible.cfg file:-
[defaults] inventory = ./inventory remote_user = aws ask_pass = true [privilege_escalation] become = True become_method = sudo become_user = root become_ask_pass = true
Step-2: Create playbook to send e-mail from all node machine
- name: Sending mail using playbook hosts: mailserver tasks: - name: sending mail to [email protected] mail: host: smtp.gmail.com port: 587 username: your_email_address password: your_email_password to: Test <test@@gmail.com>
Now run this playbook.
Postfix RealyHost successfully configured to aws ec2 nodes