--- - name: "POSTFIX | Install postfix package" ansible.builtin.apt: name: - postfix - postfix-pcre # Often useful for advanced matching - libsasl2-modules # Required for SASL authentication state: present update_cache: true tags: - postfix_install - name: "POSTFIX | Configure /etc/mailname" ansible.builtin.copy: content: "{{ postfix_mail_domain }}\n" dest: /etc/mailname owner: root group: root mode: '0644' tags: - postfix_config - name: "POSTFIX | Configure main.cf" ansible.builtin.template: src: main.cf.j2 dest: /etc/postfix/main.cf owner: root group: root mode: '0644' validate: 'postfix check -c %s' # Validates the template before deploying notify: Restart Postfix # Triggers the handler to restart the service tags: - postfix_config - name: "POSTFIX | Configure smarthost credentials (if defined)" when: postfix_relayhost_user is defined and postfix_relayhost_password is defined block: - name: "POSTFIX | Template the SASL password file" ansible.builtin.template: src: sasl_passwd.j2 dest: /etc/postfix/sasl_passwd owner: root group: root mode: '0600' # Secure permissions for file with credentials no_log: true # Prevents credentials from being displayed in Ansible logs notify: Restart Postfix - name: "POSTFIX | Create hash map for SASL password file" ansible.builtin.command: cmd: postmap hash:/etc/postfix/sasl_passwd changed_when: true # The postmap command always updates the .db file notify: Restart Postfix tags: - postfix_config - postfix_smarthost - name: "DOVECOT | Install Dovecot packages" when: dovecot_enabled | default(false) ansible.builtin.apt: name: "{{ ['dovecot-core', 'dovecot-imapd', 'dovecot-pop3d'] + (['dovecot-lmtpd'] if dovecot_postfix_lmtp_enable | default(false) else []) }}" state: present tags: - dovecot_install - name: "DOVECOT | Configure dovecot.conf" when: dovecot_enabled | default(false) ansible.builtin.template: src: dovecot.conf.j2 dest: /etc/dovecot/dovecot.conf owner: root group: dovecot mode: '0644' notify: Restart Dovecot tags: - dovecot_config - name: "DOVECOT | Configure conf.d files" when: dovecot_enabled | default(false) ansible.builtin.template: src: "{{ item.src }}" dest: "/etc/dovecot/conf.d/{{ item.dest }}" owner: root group: dovecot mode: '0644' loop: - { src: '10-auth.conf.j2', dest: '10-auth.conf' } - { src: '10-master.conf.j2', dest: '10-master.conf' } - { src: '10-ssl.conf.j2', dest: '10-ssl.conf' } - { src: '10-mail.conf.j2', dest: '10-mail.conf' } notify: Restart Dovecot tags: - dovecot_config