From 715315528942b2eec227e7b272c49ed6ed859a01 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 29 Apr 2018 02:21:08 +0800 Subject: [PATCH] Debian version will remove other MTAs, added Vagrantfile --- .gitignore | 18 +++++++++++ README.md | 36 +++++++++++----------- Vagrantfile | 71 ++++++++++++++++++++++++++++++++++++++++++++ defaults/main.yml | 32 ++++++++++---------- handlers/main.yml | 4 +-- meta/main.yml | 13 ++++---- mstmp.yml | 6 ---- tasks/main.yml | 61 ++++++++++++++++++++++++++++++++++++- tasks/msmtp.yml | 44 --------------------------- templates/msmtprc.j2 | 16 ++++++---- tests/vagrant.yml | 7 +++++ 11 files changed, 211 insertions(+), 97 deletions(-) create mode 100644 .gitignore create mode 100644 Vagrantfile delete mode 100644 mstmp.yml delete mode 100644 tasks/msmtp.yml create mode 100644 tests/vagrant.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..610faad --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# OS generated files # +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db + +# Vagrant files # +.virtualbox/ +.vagrant/ +vagrant_ansible_inventory_* +ansible.cfg + +# Other files # +!empty diff --git a/README.md b/README.md index 844fe9e..e3b278c 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,20 @@ [![Build Status](https://travis-ci.org/ahelal/ansible-msmtp.svg?branch=master)](https://travis-ci.org/ahelal/ansible-msmtp) #Readme -This ansible role deploys msmtp for Ubuntu 12.04 (tested on vagrant) +This ansible role deploys msmtp for Debian 9 stretch (tested on vagrant) ##Prerequisite -* Having ansible installed on your workstation. -* Having an SMTP server +* Having ansible installed on your workstation. +* Having an SMTP server ##How to install * Use github to clone/fork in your role directory * ansible galaxy ```ansible-galaxy install adham.helal.msmtp``` -##Variables - All the default variables are located **defaults/main.yml**. Mostly you would need to configure the following variables. - - *msmtp_accounts:* You can define one or more smtp account - +##Variables + All the default variables are located **defaults/main.yml**. Mostly you would need to configure the following variables. + - *msmtp_accounts:* You can define one or more smtp account + ``` msmtp_accounts: - account : "gmail" @@ -30,23 +30,23 @@ This ansible role deploys msmtp for Ubuntu 12.04 (tested on vagrant) user : "myuser" password : "123456" ``` - - *msmtp_default_account:* Default smtp account to use + - *msmtp_default_account:* Default smtp account to use ```msmtp_default_account: "gmail"``` - *msmtp_from:* From field ```msmtp_from : "No Reply"``` - - - Logging + + - Logging - Option A (syslog) - + ``` msmtp_log : "syslog" ``` - Option B (file logging) - + ``` msmtp_log : "file" msmtp_logfile : /var/log/msmtp.log @@ -58,8 +58,8 @@ This ansible role deploys msmtp for Ubuntu 12.04 (tested on vagrant) msmtp_log : "no" ``` - - Mail aliases - - *msmtp_alias_default:* default email this required + - Mail aliases + - *msmtp_alias_default:* default email this required ```msmtp_alias_default : ops@example.com``` @@ -69,7 +69,7 @@ This ansible role deploys msmtp for Ubuntu 12.04 (tested on vagrant) - *msmtp_alias_cron:* cron email this optional - ```msmtp_alias_cron : cron@example.com``` + ```msmtp_alias_cron : cron@example.com``` ##Configure You can configure your variables in ansible with one of the following @@ -81,12 +81,12 @@ You can configure your variables in ansible with one of the following ##Run **By default the mstmp will fail because the configuration uses a bogus smtp server you need to use a valid smtp server** - + ```ansible-playbook -l hostname msmtp.yml``` ##Test You should get a test mail if it works on the root mail ##Possible issues - From field requires more work -[http://msmtp.sourceforge.net/doc/msmtp.html#Envelope_002dfrom-address](http://msmtp.sourceforge.net/doc/msmtp.html#Envelope_002dfrom-address) + From field requires more work +[http://msmtp.sourceforge.net/doc/msmtp.html#Envelope_002dfrom-address](http://msmtp.sourceforge.net/doc/msmtp.html#Envelope_002dfrom-address) diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..8948245 --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,71 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby ts=2 sw=2 tw=0 et : + +boxes = [ + # { + # :name => "ubuntu-1204", + # :box => "bento/ubuntu-12.04", + # :ip => '10.0.0.11', + # :cpu => "50", + # :ram => "256" + # }, + # { + # :name => "ubuntu-1404", + # :box => "bento/ubuntu-14.04", + # :ip => '10.0.0.12', + # :cpu => "50", + # :ram => "256" + # }, + # { + # :name => "ubuntu-1604", + # :box => "bento/ubuntu-16.04", + # :ip => '10.0.0.13', + # :cpu => "50", + # :ram => "256" + # }, + # { + # :name => "debian-7", + # :box => "bento/debian-7", + # :ip => '10.0.0.14', + # :cpu => "50", + # :ram => "256" + # }, + # { + # :name => "debian-8", + # :box => "bento/debian-8", + # :ip => '10.0.0.15', + # :cpu => "50", + # :ram => "256" + # }, + { + :name => "debian-9", + :box => "bento/debian-9", + :ip => '10.0.0.16', + :cpu => "50", + :ram => "512" + }, +] + +role = File.basename(File.expand_path(File.dirname(__FILE__))) + +Vagrant.configure("2") do |config| + boxes.each do |box| + config.vm.define box[:name] do |vms| + vms.vm.box = box[:box] + vms.vm.hostname = "#{role}-#{box[:name]}" + + vms.vm.provider "virtualbox" do |v| + v.customize ["modifyvm", :id, "--cpuexecutioncap", box[:cpu]] + v.customize ["modifyvm", :id, "--memory", box[:ram]] + end + + vms.vm.network :private_network, ip: box[:ip] + + vms.vm.provision :ansible do |ansible| + ansible.playbook = "tests/vagrant.yml" + ansible.verbose = "vv" + ansible.compatibility_mode = "2.0" + end + end + end +end diff --git a/defaults/main.yml b/defaults/main.yml index 98dab02..f2b5df6 100755 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,28 +1,30 @@ --- msmtp_accounts: #Account 1 - - account : "gmail" - host : "smtp.gmail.com" + - account : gmail + host : smtp.gmail.com port : 587 auth : "on" - user : "example@gmail.example" - password : "some password" + from : username@gmail.com + user : username@gmail.com + password : plain-text-password1 #Account 2 - - account : "mysmtp" - host : "smtp.example" + - account : mysmtp + host : smtp.example port : 587 auth : "on" - user : "myuser" - password : "123456" + from : admin@example.org + user : myuser@example.org + password : plain-text-password2 -msmtp_default_account: "gmail" -msmtp_from : "My Org" +msmtp_default_account: mysmtp +msmtp_domain : example.org ## Logging (Select A) "syslog" or B) "file" logging or C) "no" log -msmtp_log : "syslog" -#msmtp_logfile : /var/log/msmtp.log +#msmtp_log : "syslog" +msmtp_logfile : /var/log/msmtp.log ## Aliases mail account ( only msmtp_alias_default is required the rest is optional ) -msmtp_alias_default : ops@example. -msmtp_alias_root : root@example.com -msmtp_alias_cron : cron@example.com +msmtp_alias_default : devops@example.org +msmtp_alias_root : root@example.org +msmtp_alias_cron : cron@example.org diff --git a/handlers/main.yml b/handlers/main.yml index 75ead5e..3c78181 100755 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,5 +1,5 @@ --- # send test mail to admin - name: test mail - shell: echo "Test mail from new/updated MSTMP at" `hostname` | mail root -s "Test SMTP `hostname`" - tags: mail \ No newline at end of file + shell: echo "Test mail from new/updated MSTMP at" `hostname -f` | mail root -s "Test SMTP at `hostname -f`" + tags: mail diff --git a/meta/main.yml b/meta/main.yml index 268388a..81a54b3 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,11 +1,12 @@ galaxy_info: - author: Adham Helal - description: "Deploy msmtp mail for Ubuntu 12.04" - min_ansible_version: 1.4 + author: Adham Helal, modified by Christian Wagner + description: "Deploy msmtp mail for Debian" + min_ansible_version: 2.4 platforms: - - name: Ubuntu + - name: Debian versions: - - precise + - jessie + - stretch categories: - system - - networking \ No newline at end of file + - networking diff --git a/mstmp.yml b/mstmp.yml deleted file mode 100644 index 81b9f46..0000000 --- a/mstmp.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- hosts: all - sudo: True - roles: - - ansible-msmtp - diff --git a/tasks/main.yml b/tasks/main.yml index 897f971..57c91a5 100755 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,3 +1,62 @@ --- +# Deploy msmtp +- name: Update apt cache. + apt: + upgrade: yes + update_cache: yes + cache_valid_time: 14400 # 4 hours + tags: mail + when: ansible_os_family == "Debian" -- include: msmtp.yml \ No newline at end of file +- name: Remove other MTAs. + apt: + pkg: "{{ item }}" + state: absent + autoremove: yes + purge: yes + with_items: + - exim4* + - postfix* + - sendmail* + tags: + - mail + +- name: Install msmtp and mailx. + apt: + pkg: "{{item}}" + state: present + with_items: + - msmtp-mta + - bsd-mailx + tags: mail + notify: test mail + when: ansible_os_family == "Debian" + +# not tested my modifications with Archlinux +- name: Install msmtp and mailx. + pacman: name={{item}} state=present + with_items: + - msmtp + - msmtp-mta + - mailutils + when: ansible_os_family == "Archlinux" + +- name: Copy mstprc conf file. + template: + src: msmtprc.j2 + dest: /etc/msmtprc + owner: root + group: root + mode: 0644 + tags: mail + notify: test mail + +- name: Copy aliases conf file. + template: + src: aliases.j2 + dest: /etc/aliases + owner: root + group: root + mode: 0644 + tags: mail + notify: test mail diff --git a/tasks/msmtp.yml b/tasks/msmtp.yml deleted file mode 100644 index e1884f4..0000000 --- a/tasks/msmtp.yml +++ /dev/null @@ -1,44 +0,0 @@ ---- - -# Author: Adham Helal -# -# Objective: Deploy msmtp - -- name: Update apt cache - apt: update_cache=yes cache_valid_time=3600 - tags: mail - when: ansible_os_family == "Debian" - -- name: Install msmtp and mailx - apt: pkg={{item}} state=present - with_items: - - msmtp - - msmtp-mta - - bsd-mailx - tags: mail - notify: test mail - when: ansible_os_family == "Debian" - -- name: Install msmtp and mailx - pacman: name={{item}} state=present - with_items: - - msmtp - - msmtp-mta - - mailutils - when: ansible_os_family == "Archlinux" - -- name: Copy mstprc conf file - template: - src=msmtprc.j2 - dest=/etc/msmtprc - owner=root - group=root - mode=0644 - tags: mail - notify: test mail - -- name: Copy aliases conf file - template: src=aliases.j2 dest=/etc/aliases - owner=root group=root mode=0644 - tags: mail - notify: test mail diff --git a/templates/msmtprc.j2 b/templates/msmtprc.j2 index aefbecc..c9511fa 100644 --- a/templates/msmtprc.j2 +++ b/templates/msmtprc.j2 @@ -1,15 +1,17 @@ # {{ ansible_managed }} -# Default settings that all others account inherit + +# Default settings that all others accounts inherit defaults auth on tls on +tls_starttls on {% if msmtp_tls_trust_file is defined %} tls_trust_file {{msmtp_tls_trust_file}} {% else %} tls_trust_file /etc/ssl/certs/ca-certificates.crt {% endif %} -# Logging +# Logging {% if msmtp_log is defined %} {% if msmtp_log == "syslog" %} syslog on @@ -18,9 +20,12 @@ logfile {{msmtp_logfile}} {% endif %} {% endif %} - -from {{msmtp_from}} -keepbcc on +# default is ‘localhost’. Use domain part of your email address or FQDN of host. +domain {{msmtp_domain}} +# default is ‘off’. When on, an envelope-from address of the form user@domain will be generated. +auto_from off +# The default is to remove BCC headers. +# remove_bcc_headers on|off {% if msmtp_accounts is defined %} {% for msmtp_account in msmtp_accounts %} @@ -29,6 +34,7 @@ account {{msmtp_account.account}} host {{msmtp_account.host}} port {{msmtp_account.port}} auth {{msmtp_account.auth}} +from {{msmtp_account.from}} user {{msmtp_account.user}} password {{msmtp_account.password}} {% endfor %} diff --git a/tests/vagrant.yml b/tests/vagrant.yml new file mode 100644 index 0000000..c7d0a93 --- /dev/null +++ b/tests/vagrant.yml @@ -0,0 +1,7 @@ +# test file for fail2ban +--- +- hosts: all + remote_user: vagrant + become: true + roles: + - ../../