ansible-postfix Ansible Role

repository·master·Indexed 41 days ago

https://github.com/oefenweb/ansible-postfix

An Ansible role for installing and configuring the Postfix mail server on Debian-based systems. It provides comprehensive management of package installation, hostname identity, relay hosts with SASL authentication, TLS security for inbound and outbound SMTP, and mapping tables for aliases and transport.

Tokens
3.1K
Snippets
8
Records
14
Agent score
2%

What's inside ansible-postfix

  1. Set up a Postfix server on Debian-like systems

    master

    Use the postfix Ansible role to install and configure a Postfix mail server on Debian-based distributions. The role handles package installation, hostname configuration, and various Postfix settings including relay hosts, SASL authentication, and TLS security.

    ## postfix
    
    Set up a postfix server in Debian-like systems.
  2. Configure Postfix TLS security

    master

    Use these variables to manage encryption for the SMTP client and server:

    SMTP Client TLS (Outbound)

    • postfix_smtp_tls_security_level: Default SMTP TLS security level. Defaults to encrypt.
    • postfix_smtp_tls_wrappermode: Use legacy SMTPS protocol instead of STARTTLS. Defaults to false.
    • postfix_smtp_tls_cafile: Path to CA certificates file (e.g., /etc/ssl/certs/ca-certificates.crt).
    • postfix_smtp_tls_mandatory_ciphers: Minimum TLS cipher grade for mandatory TLS.
    • postfix_smtp_tls_mandatory_protocols: TLS protocols for mandatory TLS.
    • postfix_smtp_tls_protocols: TLS protocols for opportunistic TLS.

    SMTP Server TLS (Inbound)

    • postfix_smtpd_tls_cert_file: Path to certificate file. Defaults to /etc/ssl/certs/ssl-cert-snakeoil.pem.
    • postfix_smtpd_tls_key_file: Path to key file. Defaults to /etc/ssl/certs/ssl-cert-snakeoil.key.
    • postfix_smtpd_security_level: SMTP TLS security level for the server.
    • postfix_smtpd_tls_mandatory_ciphers: Minimum TLS cipher grade for mandatory TLS.
    • postfix_smtpd_tls_mandatory_protocols: TLS protocols for mandatory TLS.
    • postfix_smtpd_tls_protocols: TLS protocols for opportunistic TLS.
  3. Configure Postfix relay hosts and SASL authentication

    master

    To route mail through a relay host (like an external SMTP provider), use these variables:

    • postfix_relayhosts: List of hostnames to relay all email to. (Note: setting multiple hosts requires Postfix 3.5+). Defaults to [].
    • postfix_relayhost_mxlookup: Whether to use MX records instead of A records for relayhosts. Defaults to false.
    • postfix_relayhost_port: Relay port. Defaults to 587.
    • postfix_relaytls: Whether to use TLS when sending with a relay host. Defaults to false.
    • postfix_sasl_auth_enable: Enable SASL authentication in the SMTP client. Defaults to true.
    • postfix_sasl_user: SASL relay username. Defaults to postmaster@{{ ansible_facts['domain'] }}.
    • postfix_sasl_password: SASL relay password. Warning: Change the default value!
    • postfix_sasl_security_options: SMTP client SASL security options. Defaults to noanonymous.
    • postfix_sasl_tls_security_option: SMTP client SASL TLS security options. Defaults to noanonymous.
    • postfix_sasl_mechanism_filter: SMTP client SASL authentication mechanism filter.
  4. Configure Postfix installation and basic identity

    master

    The following variables control the core installation and identity of the Postfix server:

    • postfix_install: List of packages to install. Defaults to [postfix, mailutils, libsasl2-2, sasl2-bin, libsasl2-modules].
    • postfix_hostname: The host name used for myhostname and mydestination. Defaults to {{ ansible_facts['fqdn'] }}.
    • postfix_mailname: The mail name used for myorigin (stored in /etc/mailname). Defaults to {{ ansible_facts['fqdn'] }}.
    • postfix_mydestination: Domains this machine delivers locally. Defaults to ["{{ postfix_hostname }}", 'localdomain', 'localhost', 'localhost.localdomain'].
    • postfix_mynetworks: List of trusted remote SMTP clients. Defaults to ['127.0.0.0/8', '[::ffff:127.0.0.0]/104', '[::1]/128'].
    • postfix_inet_interfaces: Network interfaces to bind. Defaults to all.
    • postfix_inet_protocols: Internet protocols to use. Defaults to all.
  5. Configure Postfix mapping and lookup tables

    master

    Postfix uses various mapping files for address rewriting and lookups. You can specify the content and the database type (e.g., hash, regexp) for each:

    • postfix_aliases: Aliases in /etc/aliases.
    • postfix_aliases_database_type: Database type for aliases. Defaults to {{ postfix_default_database_type }}.
    • postfix_virtual_aliases: Virtual aliases in /etc/postfix/virtual.
    • postfix_sender_canonical_maps: Sender address rewriting in /etc/postfix/sender_canonical_maps.
    • postfix_recipient_canonical_maps: Recipient address rewriting in /etc/postfix/recipient_canonical_maps.
    • postfix_transport_maps: Transport mapping in /etc/postfix/transport_maps.
    • postfix_sender_dependent_relayhost_maps: Transport mapping based on sender address.
    • postfix_header_checks: Content inspection for non-MIME headers in /etc/postfix/header_checks. Defaults to regexp database type.
    • postfix_smtp_generic_maps: Generic table address mapping in /etc/postfix/generic. (Note: postfix_generic is deprecated; use this instead).
    • postfix_default_database_type: The default database type for most maps. Defaults to hash.
  6. Add raw Postfix configuration options

    master

    If you need to set specific Postfix parameters not covered by the role's variables, use the postfix_raw_options list. Each item should be a multi-line string containing the key = value syntax.

    --- 
    - hosts: all
      roles:
        - oefenweb.postfix
      vars:
        postfix_raw_options:
          - |
            milter_default_action = accept
            milter_protocol = 6
            smtpd_milters = unix:opendkim/opendkim.sock unix:opendmarc/opendmarc.sock unix:spamass/spamass.sock unix:clamav/clamav-milter.ctl
            milter_connect_macros = "i j {daemon_name} v {if_name} _"
            policyd-spf_time_limit = 3600
  7. Configure basic Postfix aliases and virtual aliases

    master

    You can configure local aliases and virtual mail forwarding using the postfix_aliases and postfix_virtual_aliases variables.

    To set up a simple local alias for root:

    postfix_aliases:
      - user: root
        alias: you@yourdomain.org

    To set up virtual aliases for mail forwarding (e.g., forwarding webmaster mail to a personal address):

    postfix_mydestination:
      - "{{ postfix_hostname }}"
      - '$mydomain'
      - localdomain
      - localhost
      - localhost.localdomain
    postfix_virtual_aliases:
      - virtual: webmaster@yourdomain.com
        alias: personal_email@gmail.com
      - virtual: billandbob@yourdomain.com
        alias: bill@gmail.com, bob@gmail.com
    --- 
    - hosts: all
      roles:
        - oefenweb.postfix
      vars:
        postfix_aliases:
          - user: root
            alias: you@yourdomain.org
  8. Configure AWS SES relaying

    master

    To use AWS SES as a relay, configure the SES endpoint, enable TLS, and provide your AWS IAM SES credentials (Note: use IAM credentials, not the standard AWS Access Key).

    --- 
    - hosts: all
      roles:
        - oefenweb.postfix
      vars:
        postfix_aliases:
          - user: root
            alias: sesverified@yourdomain.org
        postfix_relayhost: [email-smtp.us-east-1.amazonaws.com]
        postfix_relaytls: true
        # AWS IAM SES credentials (not access key):
        postfix_sasl_user: AKIXXXXXXXXXXXXXXXXX
        postfix_sasl_password: ASDFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  9. Configure Postfix relay hosts and MX lookup

    master

    To enable mail relaying, provide a relay host name via postfix_relayhosts.

    To relay to a specific host:

    postfix_relayhosts: [mail.yourdomain.org]

    To enable relaying to the DNS MX records of a domain, set postfix_relayhost_mxlookup to true:

    postfix_relayhosts: [yourdomain.org]
    postfix_relayhost_mxlookup: true
    --- 
    - hosts: all
      roles:
        - oefenweb.postfix
      vars:
        postfix_aliases:
          - user: root
            alias: you@yourdomain.org
        postfix_relayhosts: [mail.yourdomain.org]
  10. Configure MailHog support

    master

    To use MailHog for local testing, point the relay host to the MailHog IP/address and specify the port (default 1025), ensuring SASL authentication is disabled.

    --- 
    - hosts: all
      roles:
        - oefenweb.postfix
      vars:
        postfix_aliases:
          - user: root
            alias: you@yourdomain.org
        postfix_relayhost: ["{{ ansible_facts['lo']['ipv4']['address'] }}"]
        postfix_relayhost_port: 1025
        postfix_sasl_auth_enable: false
  11. Configure Gmail SMTP relaying

    master

    To use Gmail as a relay, provide the Gmail SMTP server, enable TLS, and specify the CA certificate file.

    Note for 2-Step Verification users: If your Google account uses 2-step verification, standard passwords will fail. You must generate an 'App Password' via the Google Account Security settings and use that generated string as your postfix_sasl_password.

    --- 
    - hosts: all
      roles:
        - oefenweb.postfix
      vars:
        postfix_aliases:
          - user: root
            alias: you@yourdomain.org
        postfix_relayhost: [smtp.gmail.com]
        postfix_relaytls: true
        postfix_smtp_tls_cafile: /etc/ssl/certs/ca-certificates.crt
        postfix_sasl_user: 'foo'
        postfix_sasl_password: 'bar'
  12. Configure conditional relaying with transport and sender maps

    master

    You can implement conditional relaying using postfix_transport_maps and postfix_sender_dependent_relayhost_maps. This allows you to route specific senders or patterns to different destinations (e.g., a local SMTP server or a specific relay).

    Example of routing specific patterns to a local SMTP port (1025) while letting others pass through:

    --- 
    - hosts: all
      roles:
        - oefenweb.postfix
      vars:
        postfix_transport_maps:
          - pattern: 'root@yourdomain.org'
            result: ':'
          - pattern: '*'
            result: "smtp:{{ ansible_facts['lo']['ipv4']['address'] }}:1025"
        postfix_sender_dependent_relayhost_maps:
          - pattern: 'logcheck@yourdomain.org'
            result: 'DUNNO'
          - pattern: 'pflogsumm@yourdomain.org'
            result: 'DUNNO'
          - pattern: '*'
            result: "smtp:{{ ansible_facts['lo']['ipv4']['address'] }}:1025"