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.

209 lines
5.7 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', 'test_ssl_ca.crt') }}"
  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: get pub key
  121. slurp:
  122. src: "/etc/dovecot/private/dovecot.csr"
  123. register: pub_key
  124. - debug:
  125. var: pub_key
  126. verbosity: 2
  127. - name: generate host request
  128. set_fact:
  129. ca_request:
  130. type: 'sign_request'
  131. request:
  132. keyType: 'ssl_host'
  133. hostName: '{{ inventory_hostname }}.lilik.it'
  134. keyData: "{{ pub_key.content| b64decode}}"
  135. - debug:
  136. var: ca_request
  137. verbosity: 2
  138. - name: start sign request
  139. include: ca-dialog.yaml
  140. - debug:
  141. var: request_result
  142. verbosity: 2
  143. - set_fact:
  144. request_output: "{{ request_result.stdout|string|from_json }}"
  145. - debug:
  146. var: request_result
  147. - name: generate get request
  148. set_fact:
  149. ca_request:
  150. type: 'get_certificate'
  151. requestID: '{{ request_output.requestID }}'
  152. - debug:
  153. var: ca_request
  154. verbosity: 2
  155. - debug:
  156. msg: "Please manualy confirm sign request with id {{ request_output.requestID }}"
  157. - name: wait for cert
  158. include: ca-dialog.yaml
  159. - debug:
  160. var: request_result
  161. verbosity: 2
  162. - set_fact:
  163. cert_key: "{{ request_result.stdout|string|from_json }}"
  164. - debug:
  165. var: request_result
  166. verbosity: 2
  167. - name: set pub key
  168. copy:
  169. content: "{{ cert_key.result }}"
  170. dest: "/etc/dovecot/dovecot.cert"
  171. register: set_pub_key
  172. when: 'dovecot_cert_is_valid.rc != 0'
  173. - template:
  174. src: dovecot-ldap.conf.ext.j2
  175. dest: /etc/dovecot/dovecot-ldap.conf.ext
  176. notify: restart dovecot