forward.yml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. - hosts: [forward]
  2. become: true
  3. vars:
  4. hostname: forward.example.com
  5. virtual_alias_domains:
  6. - example.co
  7. - example.com
  8. - example.info
  9. # docs recommend against whitelist
  10. tls_protocols: ['!SSLv2', '!SSLv3', '!TLSv1', '!TLSv1.1']
  11. tasks:
  12. - docker_network:
  13. name: mail
  14. - docker_volume:
  15. volume_name: postfix_config
  16. register: config_volume
  17. - docker_volume:
  18. volume_name: postfix_queue
  19. register: queue_volume
  20. - stat:
  21. path: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}'
  22. register: config_volume_stat
  23. - name: create virtual alias map
  24. copy:
  25. # http://www.postfix.org/virtual.5.html
  26. content: |
  27. /^alice/ alice@gmail.com
  28. /^bob/ bob@gmail.com
  29. /^postmaster\@/ alice@gmail.com
  30. dest: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/virtual'
  31. mode: u=r,g=,o=
  32. # workaround if userns remapping enabled
  33. # postmap: fatal: open /etc/postfix/virtual.db: Permission denied
  34. owner: '{{ config_volume_stat.stat.uid }}'
  35. register: virtual_alias_map
  36. - openssl_privatekey:
  37. path: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/key.pem'
  38. type: RSA
  39. size: 4096
  40. owner: '{{ config_volume_stat.stat.uid }}'
  41. mode: o=r,g=,o=
  42. - openssl_csr:
  43. path: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/cert-request.pem'
  44. privatekey_path: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/key.pem'
  45. common_name: '{{ hostname }}'
  46. subject_alt_name: ['DNS:{{ hostname }}']
  47. country_name: AT
  48. basic_constraints: ['CA:FALSE']
  49. basic_constraints_critical: yes
  50. digest: sha256
  51. mode: a=r
  52. - openssl_certificate:
  53. backup: yes
  54. path: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/cert.pem'
  55. csr_path: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/cert-request.pem'
  56. privatekey_path: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/key.pem'
  57. provider: selfsigned
  58. mode: a=r
  59. register: smtpd_cert
  60. - name: postsrsd secrets volume
  61. docker_volume:
  62. volume_name: postsrsd_secrets
  63. register: postsrsd_secrets_volume
  64. - name: postsrsd secrets dir
  65. file:
  66. path: '{{ postsrsd_secrets_volume.ansible_facts.docker_volume.Mountpoint }}/secrets'
  67. state: directory
  68. # arbitrary user, see https://github.com/fphammerle/docker-postsrsd/blob/docker/0.1.1-postsrsd1.6-amd64/Dockerfile
  69. mode: a=rwx,+t
  70. - name: postsrsd
  71. docker_container:
  72. name: postsrsd
  73. # docker/0.1.1-postsrsd1.6-amd64
  74. image: fphammerle/postsrsd@sha256:486d79d63ce716994b7baca55172334aca525557e6609ee5864924040b6ad2d3
  75. networks: [name: mail]
  76. purge_networks: yes
  77. env:
  78. SRS_DOMAIN: '{{ hostname }}'
  79. volumes:
  80. - '{{ postsrsd_secrets_volume.ansible_facts.docker_volume.Mountpoint }}/secrets:/etc/postsrsd/secrets:rw'
  81. restart_policy: always
  82. - name: create config
  83. copy:
  84. content: |
  85. smtpd_tls_security_level = may
  86. smtpd_tls_cert_file=/smtpd-cert.pem
  87. smtpd_tls_key_file=/smtpd-key.pem
  88. smtpd_tls_protocols = {{ tls_protocols | join(', ') }}
  89. smtpd_tls_ciphers = high
  90. smtpd_tls_session_cache_database = btree:${data_directory}/smtpd-tls-session-cache
  91. # $myhostname prefix is a RFC requirement
  92. smtpd_banner = $myhostname ESMTP $mail_name quid agis?
  93. # http://www.postfix.org/postconf.5.html#smtpd_relay_restrictions
  94. smtpd_relay_restrictions = reject_non_fqdn_recipient, reject_unauth_destination
  95. mydestination =
  96. # http://www.postfix.org/VIRTUAL_README.html#virtual_alias
  97. virtual_alias_domains = {{ virtual_alias_domains | join(', ') }}
  98. virtual_alias_maps = regexp:/etc/postfix/virtual
  99. # include TLS protocol & cipher in 'Received' header
  100. smtpd_tls_received_header = yes
  101. # bytes
  102. message_size_limit = {{ 32 * 1024 * 1024 }}
  103. delay_warning_time = 1h
  104. sender_canonical_maps = tcp:postsrsd:10001
  105. sender_canonical_classes = envelope_sender
  106. recipient_canonical_maps = tcp:postsrsd:10002
  107. recipient_canonical_classes= envelope_recipient,header_recipient
  108. smtp_tls_security_level = encrypt
  109. smtp_tls_mandatory_protocols = {{ tls_protocols | join(', ') }}
  110. smtp_tls_session_cache_database = btree:${data_directory}/smtp-tls-session-cache
  111. # http://www.postfix.org/MAILLOG_README.html
  112. maillog_file = /dev/stdout
  113. # http://www.postfix.org/COMPATIBILITY_README.html
  114. compatibility_level = 2
  115. dest: '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/main.cf'
  116. # postfix: warning: not owned by root
  117. owner: '{{ config_volume_stat.stat.uid }}'
  118. mode: u=r,g=,o=
  119. register: config
  120. - name: postfix
  121. docker_container:
  122. name: postfix
  123. # 1.0.1-postfix3.4.5r0-amd64
  124. image: fphammerle/postfix@sha256:b2d214d66f1760bdcbfa3156efa7cb08cef5d80e5f6607e181f79fdde409b82d
  125. hostname: '{{ hostname }}'
  126. volumes:
  127. - '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/main.cf:/etc/postfix/main.cf:ro'
  128. - '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/virtual:/etc/postfix/virtual:ro'
  129. - '{{ queue_volume.ansible_facts.docker_volume.Mountpoint }}:/var/spool/postfix:rw'
  130. - '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/key.pem:/smtpd-key.pem:ro'
  131. - '{{ config_volume.ansible_facts.docker_volume.Mountpoint }}/cert.pem:/smtpd-cert.pem:ro'
  132. networks: [name: mail]
  133. purge_networks: yes
  134. published_ports: ['25:25']
  135. restart_policy: unless-stopped
  136. restart: '{{ config.changed or virtual_alias_map.changed or smtpd_cert.changed }}'
  137. - name: send test mail
  138. command: docker exec postfix sendmail check@ssl-tools.net
  139. - name: ssl-tools test url
  140. debug:
  141. msg: 'https://ssl-tools.net/mailservers/{{ hostname }}'