Browse Source

Add support for slowsmtp transport in postfix

Emmanuel Bouthenot 8 years ago
parent
commit
cfefb23d8f

+ 1 - 0
roles/common/defaults/main.yml

@@ -43,6 +43,7 @@ firewall_public:
 with_smartd: False
 
 with_postfix: False
+with_postfix_slowsmtp: False
 with_mail_aliases: False
 
 with_ssh: True

+ 9 - 4
roles/common/handlers/smtp.yml

@@ -1,5 +1,10 @@
-- name: Regenerate aliases cache
-  command: newaliases
+- name: 'Regenerate aliases cache'
+  command: 'newaliases'
 
-- name: Restart postfix
-  service: name=postfix state=restarted
+- name: 'Restart postfix'
+  service:
+    name: 'postfix'
+    state: 'restarted'
+
+- name: 'Rehash slowsmtp transport map'
+  command: 'postmap /etc/postfix/transport_slowsmtp'

+ 50 - 11
roles/common/tasks/smtp.yml

@@ -1,19 +1,58 @@
-- name: Install postfix packages
-  apt: pkg={{ item }} install_recommends=no state=installed update_cache=yes
+- name: 'Install postfix packages'
+  apt:
+    pkg: '{{ item }}'
+    install_recommends: 'no'
+    state: 'installed'
+    update_cache: 'yes'
   with_items:
-    - postfix
-    - ca-certificates
-    - procmail
+    - 'postfix'
+    - 'ca-certificates'
+    - 'procmail'
   when: with_postfix
 
-- name: Install mail aliases file
-  template: src=aliases.j2 dest=/etc/aliases owner=root group=root mode=0644
+- name: 'Install mail aliases file'
+  template:
+    src: 'aliases.j2'
+    dest: '/etc/aliases'
+    owner: 'root'
+    group: 'root'
+    mode: '0644'
   notify:
-    - Regenerate aliases cache
+    - 'Regenerate aliases cache'
   when: with_postfix or with_mail_aliases
 
-- name: Install postfix main configuration
-  template: src=postfix/main.cf.j2 dest=/etc/postfix/main.cf owner=root group=root mode=0644
+- name: 'Install postfix main configuration'
+  template:
+    src: 'postfix/main.cf.j2'
+    dest: '/etc/postfix/main.cf'
+    owner: 'root'
+    group: 'root'
+    mode: '0644'
   notify:
-    - Restart postfix
+    - 'Restart postfix'
   when: with_postfix
+
+- name: 'Update postfix master config for slowsmtp transport'
+  blockinfile:
+    dest: '/etc/postfix/master.cf'
+    marker: '# ======= {mark} slowsmtp transport (managed by ansible) ======'
+    content: |
+        slowsmtp  unix  -       -       n       -       5       smtp
+            -o smtp_destination_concurrency_limit=3
+            -o slowsmtp_destination_rate_delay=1
+            -o syslog_name=${multi_instance_name:postfix}${multi_instance_name?$multi_instance_name}/slow
+  notify:
+    - 'Restart postfix'
+  when: with_postfix and with_postfix_slowsmtp
+
+- name: 'Install postfix slowsmtp transport map'
+  template:
+    src: 'postfix/transport_slowsmtp.j2'
+    dest: '/etc/postfix/transport_slowsmtp'
+    owner: 'root'
+    group: 'root'
+    mode: '0644'
+  notify:
+    - 'Rehash slowsmtp transport map'
+    - 'Restart postfix'
+  when: with_postfix and with_postfix_slowsmtp

+ 10 - 3
roles/common/templates/postfix/main.cf.j2

@@ -54,12 +54,19 @@ myorigin = $myhostname
 mydomain = $myhostname
 mydestination = {{ ansible_fqdn }} {{ ansible_hostname }} localhost localhost.localdomain
 
+{% if smtp_relay_host is defined %}
+relayhost = {{ smtp_relay_host }}
+{% endif %}
+
 alias_maps = hash:/etc/aliases
 alias_database = $alias_maps
 
-{% if smtp_relay_host is defined -%}
-relayhost = {{ smtp_relay_host }}
-{%- endif %}
+{% if with_postfix_slowsmtp %}
+transport_maps =
+    hash:/etc/postfix/transport_slowsmtp
+slowsmtp_destination_recipient_limit = 20
+slowsmtp_destination_concurrency_limit = 2
+{% endif %}
 
 {% if smtp_relay_domains is defined %}
 relay_domains =

+ 9 - 0
roles/common/templates/postfix/transport_slowsmtp.j2

@@ -0,0 +1,9 @@
+{% if ansible_prolog -%}
+{% from 'templates/ansible/prolog.j2' import prolog with context %}
+{{ prolog() }}
+{% endif -%}
+wanadoo.fr      slowsmtp:
+wanadoo.com     slowsmtp:
+orange.fr       slowsmtp:
+orange.com      slowsmtp:
+laposte.net     slowsmtp: