Ansible для установки почтовых сервисов на CentOS7


SMTP - Simple Mail Transfer Protocol (Port 25, Secure 587 and 465)

IMAP - Internet Message Access Protocol (Port 143, Secure 993)

POP3 - Post Office Protocol 3 (Port 110, Secure 995)

TLS - Tranposrt Layer Security

MTA - Mail Tranfer Agent (Exim, Postfix, Sendmail)

MDA - Mail delivery agent (dovecot, getmail, fdm)

MUA - Mail User Agent (Thunderbird, Evolution)




Ansible playbook: установка postfix и dovecot на CentOS7 (не самый лучший вариант использовать пользователя root в playbook Ansible, но пока времени не хватает сделать иначе и все перепроверить) 

$ cat inventory 
[test]
1.1.1.1 ansible_user=root ansible_ssh_pass=yourrootpassword

$ cat install-mail.yaml
---
- name: Install mail servers postfix and dovecot on centos7 
  hosts: test
  become: yes
  tasks:
    - name: Upgrade all packages
      vars:
        ansible_python_interpreter: python2
      ansible.builtin.yum:
        name: '*'
        state: latest
    - name: Wait 15 seconds
      ansible.builtin.pause:
        seconds: 15       
    - name: Reboot the server
      ansible.builtin.command: reboot
      async: 1
      poll: 0
      ignore_errors: true  
    - name: Wait one minute
      pause:
        minutes: 1    
    - name: Install the latest version of Postfix
      vars:
        ansible_python_interpreter: python2
      ansible.builtin.yum:
        name: postfix
        state: latest       
    - name: Make sure a Postfix is running
      ansible.builtin.systemd_service:
       state: started
       name: postfix
    - name: Change myhostname in Postfix config
      lineinfile:
        path: /etc/postfix/main.cf
        regexp: '^#myhostname = host.domain.tld'
        line: 'myhostname = node1.mylabserver.com'
    - name: Change mydomain in Postfix config
      lineinfile:
        path: /etc/postfix/main.cf
        regexp: '#mydomain = domain.tld'
        line: 'mydomain = mylabserver.com'
    - name: Restart postfix daemon
      ansible.builtin.systemd_service:
        state: restarted
        daemon_reload: true
        name: postfix      
    - name: Install the latest version of dovecot
      vars:
        ansible_python_interpreter: python2
      ansible.builtin.yum:
        name: dovecot 
        state: latest
    - name: Make sure a Dovecot is running
      ansible.builtin.systemd_service:
        state: started
        name: dovecot
    - name: Install the latest version of dovecot
      vars:
        ansible_python_interpreter: python2
      ansible.builtin.yum:
        name: dovecot-pigeonhole
        state: latest
    - name: Change mail_location in Dovecot config
      lineinfile:
        path: /etc/dovecot/conf.d/10-mail.conf
        regexp: '^#mail_location'
        line: 'mail_location = maildir:~/Maildir'
    - name: Change mailbox_command in Postfix config
      lineinfile:
        path: /etc/postfix/main.cf
        regexp: 'mailbox_command = /some/where/procmail'
        line: 'mailbox_command = /usr/libexec/dovecot/deliver'
    - name: Restart postfix daemon
      ansible.builtin.systemd_service:
        state: restarted
        daemon_reload: true
        name: postfix
    - name: Change lda_mailbox_autocreate = yes in Dovecot config
      lineinfile:
        path: /etc/dovecot/conf.d/15-lda.conf
        regexp: '#lda_mailbox_autocreate = no'
        line: 'lda_mailbox_autocreate = yes'
    - name: Change lda_mailbox_autocreate = yes in Dovecot config
      lineinfile:
        path: /etc/dovecot/conf.d/15-lda.conf
        regexp: '#lda_mailbox_autosubscribe = no'
        line: 'lda_mailbox_autosubscribe = yes'
    - name: Copy file with owner and permissions
      ansible.builtin.copy:
        src: /home/username/.ansible/15-lda.conf
        dest: /etc/dovecot/conf.d/15-lda.conf
        owner: root
        group: root
        mode: '0644'      
    - name: Add the user cloud_user
      ansible.builtin.user:
        name: cloud_user
        password: $y$j9T$V/TyXSACyzBWAFEi3aowJ.$BzxU3ygyCxFOvSXM4q93KvFNsUCdt8erAp.KnpQgTK7
        comment: cloud user
        uid: 1066
        create_home: true  
        group: wheel     
    - name: Create a directory if it does not exist
      ansible.builtin.file:
        path: /home/cloud_user/.dovecot.sieve
        state: touch
        owner: cloud_user
        mode: '0644'      
    - name: Insert/Update configuration /home/cloud_user/.dovecot.sieve 
      ansible.builtin.blockinfile:
        path: /home/cloud_user/.dovecot.sieve
        block: |
            require ["fileinto"];
       
            if header :contains "subject" "spam"
            {
                fileinto "trash";
            }
    - name: Restart dovecot daemon
      ansible.builtin.systemd_service:
        state: restarted
        daemon_reload: true
        name: dovecot


$ cat 15-lda.conf
##
## LDA specific settings (also used by LMTP)
##

# Address to use when sending rejection mails.
# Default is postmaster@<your domain>. %d expands to recipient domain.
#postmaster_address =

# Hostname to use in various parts of sent mails (e.g. in Message-Id) and
# in LMTP replies. Default is the system's real hostname@domain.
#hostname = 

# If user is over quota, return with temporary failure instead of
# bouncing the mail.
#quota_full_tempfail = no

# Binary to use for sending mails.
#sendmail_path = /usr/sbin/sendmail

# If non-empty, send mails via this SMTP host[:port] instead of sendmail.
#submission_host =

# Subject: header to use for rejection mails. You can use the same variables
# as for rejection_reason below.
#rejection_subject = Rejected: %s

# Human readable error message for rejection mails. You can use variables:
#  %n = CRLF, %r = reason, %s = original subject, %t = recipient
#rejection_reason = Your message to <%t> was automatically rejected:%n%r

# Delimiter character between local-part and detail in email address.
#recipient_delimiter = +

# Header where the original recipient address (SMTP's RCPT TO: address) is taken
# from if not available elsewhere. With dovecot-lda -a parameter overrides this. 
# A commonly used header for this is X-Original-To.
#lda_original_recipient_header =

# Should saving a mail to a nonexistent mailbox automatically create it?
lda_mailbox_autocreate = yes

# Should automatically created mailboxes be also automatically subscribed?
lda_mailbox_autosubscribe = yes

protocol lda {
  # Space separated list of plugins to load (default is global mail_plugins).
mail_plugins = $mail_plugins sieve
}
#copied from local machine to remote server

$ ansible all -m ping -u root -i inventory

$ ansible-playbook  -i inventory install-mail.yaml

Комментарии