Playbooks to a new Lilik
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.2 KiB

  1. - include_role:
  2. name: service
  3. # static: yes # see static include issue: https://github.com/ansible/ansible/issues/13485
  4. vars:
  5. service_name: dovecot
  6. service_packages:
  7. - dovecot-ldap
  8. - dovecot-imapd
  9. - rsyslog
  10. - lineinfile: dest=/etc/postfix/main.cf line="virtual_transport = dovecot" state=present
  11. notify: restart postfix
  12. - blockinfile:
  13. dest: /etc/postfix/master.cf
  14. block: |
  15. dovecot unix - n n - - pipe
  16. flags=DRhu user=postman:postman argv=/usr/lib/dovecot/deliver -d ${recipient} -f ${sender}
  17. notify: restart postfix
  18. - name: create postman group
  19. group:
  20. name: postman
  21. state: present
  22. - name: create postman user
  23. user:
  24. name: postman
  25. state: present
  26. shell: /dev/null
  27. - name: edit dovecot configuration
  28. lineinfile:
  29. dest: /etc/dovecot/conf.d/10-master.conf
  30. line: ' port = 143'
  31. insertafter: 'inet_listener imap {'
  32. state: present
  33. notify: restart dovecot
  34. - blockinfile:
  35. dest: /etc/dovecot/conf.d/10-master.conf
  36. insertafter: 'inet_listener imaps {'
  37. marker: '#{mark} ANSIBLE BLOCK FOR IMAPS PORT'
  38. block: |
  39. port = 993
  40. ssl = yes
  41. notify: restart dovecot
  42. - blockinfile:
  43. dest: "/etc/dovecot/conf.d/10-master.conf"
  44. insertafter: "unix_listener auth-userdb {"
  45. marker: '#{mark} ANSIBLE BLOCK FOR AUTH USER'
  46. block: |
  47. group = postman
  48. mode = 0664
  49. user = postman
  50. notify: restart dovecot
  51. - lineinfile:
  52. dest: /etc/dovecot/conf.d/10-mail.conf
  53. regexp: "{{ item.regexp }}"
  54. line: "{{ item.line }}"
  55. state: present
  56. with_items:
  57. - { regexp: '^mail_location = ', line: 'mail_location = maildir:/home/postman/%d/%n' }
  58. - { regexp: 'mail_gid = ', line: 'mail_gid = postman' }
  59. - { regexp: 'mail_uid = ', line: 'mail_uid = postman' }
  60. notify: restart dovecot
  61. - lineinfile:
  62. dest: /etc/dovecot/conf.d/10-auth.conf
  63. regexp: "{{ item.regexp }}"
  64. line: "{{ item.line }}"
  65. state: "{{ item.state }}"
  66. with_items:
  67. - { regexp: None, line: 'mail_location = maildir:/home/postman/%d/%n', state: 'absent'}
  68. - { regexp: None, line: '!include auth-ldap.conf.ext', state: 'present'}
  69. - { regexp: 'auth_default_realm =', line: 'auth_default_realm = {{ domain }}', state: 'present'}
  70. - { regexp: 'auth_mechanisms =', line: 'auth_mechanisms = login plain', state: 'present'}
  71. - { regexp: None, line: '!include auth-ldap.conf.ext', state: 'present'}
  72. notify: restart dovecot
  73. - name: enable ssl key
  74. blockinfile:
  75. dest: /etc/dovecot/conf.d/10-ssl.conf
  76. block: |
  77. ssl = yes
  78. ssl_cert = </etc/dovecot/dovecot.cert
  79. ssl_key = </etc/dovecot/private/dovecot.key
  80. - name: generate the RSA key
  81. # TODO: reenable openssl_privatekey when moving to ansible 2.3
  82. # openssl_privatekey:
  83. # path: "/etc/dovecot/private/dovecot.key"
  84. # size: 2048
  85. # state: present
  86. # type: RSA
  87. shell: "openssl genrsa -out /etc/dovecot/private/dovecot.key 2048"
  88. args:
  89. creates: /etc/dovecot/private/dovecot.key
  90. notify: restart dovecot
  91. - name: generate CSR
  92. # TODO: reenable openssl_csr when moving to ansible 2.3
  93. # openssl_csr:
  94. # commonName: "{{ fqdn_domain }}"
  95. # countryName: "IT"
  96. # digest: sha256
  97. # localityName: "TUSCANY"
  98. # organizationName: "IT"
  99. # path: "/etc/dovecot/private/dovecot.csr"
  100. # privatekey_path: "/etc/dovecot/private/dovecot.key"
  101. # state: present
  102. # stateOrProvinceName: "ITALY"
  103. shell: 'openssl req -new -sha256 -subj "/C=IT/ST=ITALY/L=TUSCANY/O=IT/CN={{ fqdn_domain }}" -key /etc/dovecot/private/dovecot.key -out /etc/dovecot/private/dovecot.csr'
  104. args:
  105. creates: /etc/dovecot/private/dovecot.csr
  106. notify: restart dovecot
  107. - name: lookup ssl ca key
  108. set_fact:
  109. ssl_ca_key: "{{ lookup('file', 'lilik_ca_w1.pub') }}"
  110. - name: Update ssl CA key
  111. copy:
  112. content: "{{ ssl_ca_key }}"
  113. dest: "/etc/dovecot/ssl_ca.crt"
  114. - name: check if dovecot cert is valid
  115. command: 'openssl verify -CAfile /etc/dovecot/ssl_ca.crt /etc/dovecot/dovecot.cert'
  116. register: dovecot_cert_is_valid
  117. changed_when: false
  118. failed_when: false
  119. - block:
  120. - name: generate host request
  121. cert_request:
  122. host: "{{ inventory_hostname }}.lilik.it"
  123. path: "/etc/dovecot/private/dovecot.csr"
  124. proto: "ssl"
  125. register: ca_request
  126. - name: start sign request
  127. include: ca-dialog.yaml
  128. - set_fact:
  129. request_output: "{{ request_result.stdout|string|from_json }}"
  130. - debug:
  131. var: request_result
  132. - name: generate get request
  133. set_fact:
  134. ca_request:
  135. type: 'get_certificate'
  136. requestID: '{{ request_output.requestID }}'
  137. - debug:
  138. msg: "Please manualy confirm sign request with id {{ request_output.requestID }}"
  139. - name: wait for cert
  140. include: ca-dialog.yaml
  141. - set_fact:
  142. cert_key: "{{ request_result.stdout|string|from_json }}"
  143. - debug:
  144. var: request_result
  145. verbosity: 2
  146. - name: set pub key
  147. copy:
  148. content: "{{ cert_key.result }}"
  149. dest: "/etc/dovecot/dovecot.cert"
  150. register: set_pub_key
  151. when: 'dovecot_cert_is_valid.rc != 0'
  152. - template:
  153. src: dovecot-ldap.conf.ext.j2
  154. dest: /etc/dovecot/dovecot-ldap.conf.ext
  155. notify: restart dovecot