fetch_server_certificates_from_ca.yml 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ---
  2. - name: Install etcd
  3. package:
  4. name: "etcd{{ '-' + etcd_version if etcd_version is defined else '' }}"
  5. state: present
  6. when: not etcd_is_containerized | bool
  7. register: result
  8. until: result is succeeded
  9. - name: Check status of etcd certificates
  10. stat:
  11. path: "{{ item }}"
  12. with_items:
  13. - "{{ etcd_cert_config_dir }}/{{ etcd_cert_prefix }}server.crt"
  14. - "{{ etcd_cert_config_dir }}/{{ etcd_cert_prefix }}peer.crt"
  15. - "{{ etcd_cert_config_dir }}/{{ etcd_cert_prefix }}ca.crt"
  16. register: g_etcd_server_cert_stat_result
  17. when: not etcd_certificates_redeploy | default(false) | bool
  18. - set_fact:
  19. etcd_server_certs_missing: "{{ true if etcd_certificates_redeploy | default(false) | bool
  20. else (False in (g_etcd_server_cert_stat_result.results
  21. | default({})
  22. | oo_collect(attribute='stat.exists')
  23. | list)) }}"
  24. - name: Ensure generated_certs directory present
  25. file:
  26. path: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  27. state: directory
  28. mode: 0700
  29. when: etcd_server_certs_missing | bool
  30. delegate_to: "{{ etcd_ca_host }}"
  31. - name: Create the server csr
  32. command: >
  33. openssl req -new -keyout {{ etcd_cert_prefix }}server.key
  34. -config {{ etcd_openssl_conf }}
  35. -out {{ etcd_cert_prefix }}server.csr
  36. -reqexts {{ etcd_req_ext }} -batch -nodes
  37. -subj /CN={{ etcd_hostname }}
  38. args:
  39. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  40. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  41. ~ etcd_cert_prefix ~ 'server.csr' }}"
  42. environment:
  43. SAN: "IP:{{ etcd_ip }},DNS:{{ etcd_hostname }}"
  44. when: etcd_server_certs_missing | bool
  45. delegate_to: "{{ etcd_ca_host }}"
  46. # Certificates must be signed serially in order to avoid competing
  47. # for the serial file.
  48. - name: Sign and create the server crt
  49. delegated_serial_command:
  50. command: >
  51. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  52. -out {{ etcd_cert_prefix }}server.crt
  53. -in {{ etcd_cert_prefix }}server.csr
  54. -extensions {{ etcd_ca_exts_server }} -batch
  55. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  56. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  57. ~ etcd_cert_prefix ~ 'server.crt' }}"
  58. environment:
  59. SAN: "IP:{{ etcd_ip }}"
  60. when: etcd_server_certs_missing | bool
  61. delegate_to: "{{ etcd_ca_host }}"
  62. - name: Create the peer csr
  63. command: >
  64. openssl req -new -keyout {{ etcd_cert_prefix }}peer.key
  65. -config {{ etcd_openssl_conf }}
  66. -out {{ etcd_cert_prefix }}peer.csr
  67. -reqexts {{ etcd_req_ext }} -batch -nodes
  68. -subj /CN={{ etcd_hostname }}
  69. args:
  70. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  71. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  72. ~ etcd_cert_prefix ~ 'peer.csr' }}"
  73. environment:
  74. SAN: "IP:{{ etcd_ip }},DNS:{{ etcd_hostname }}"
  75. when: etcd_server_certs_missing | bool
  76. delegate_to: "{{ etcd_ca_host }}"
  77. # Certificates must be signed serially in order to avoid competing
  78. # for the serial file.
  79. - name: Sign and create the peer crt
  80. delegated_serial_command:
  81. command: >
  82. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  83. -out {{ etcd_cert_prefix }}peer.crt
  84. -in {{ etcd_cert_prefix }}peer.csr
  85. -extensions {{ etcd_ca_exts_peer }} -batch
  86. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  87. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  88. ~ etcd_cert_prefix ~ 'peer.crt' }}"
  89. environment:
  90. SAN: "IP:{{ etcd_ip }}"
  91. when: etcd_server_certs_missing | bool
  92. delegate_to: "{{ etcd_ca_host }}"
  93. - file:
  94. src: "{{ etcd_ca_cert }}"
  95. dest: "{{ etcd_generated_certs_dir}}/{{ etcd_cert_subdir }}/{{ etcd_cert_prefix }}ca.crt"
  96. state: hard
  97. when: etcd_server_certs_missing | bool
  98. delegate_to: "{{ etcd_ca_host }}"
  99. - name: Create local temp directory for syncing certs
  100. local_action: command mktemp -d /tmp/etcd_certificates-XXXXXXX
  101. become: no
  102. register: g_etcd_server_mktemp
  103. changed_when: False
  104. when: etcd_server_certs_missing | bool
  105. - name: Create a tarball of the etcd certs
  106. command: >
  107. tar -czvf {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz
  108. -C {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }} .
  109. args:
  110. creates: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  111. # Disables the following warning:
  112. # Consider using unarchive module rather than running tar
  113. warn: no
  114. when: etcd_server_certs_missing | bool
  115. delegate_to: "{{ etcd_ca_host }}"
  116. - name: Retrieve etcd cert tarball
  117. fetch:
  118. src: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  119. dest: "{{ g_etcd_server_mktemp.stdout }}/"
  120. flat: yes
  121. fail_on_missing: yes
  122. validate_checksum: yes
  123. when: etcd_server_certs_missing | bool
  124. delegate_to: "{{ etcd_ca_host }}"
  125. - name: Ensure certificate directory exists
  126. file:
  127. path: "{{ item }}"
  128. state: directory
  129. with_items:
  130. - "{{ etcd_cert_config_dir }}"
  131. when: etcd_server_certs_missing | bool
  132. - name: Unarchive cert tarball
  133. unarchive:
  134. src: "{{ g_etcd_server_mktemp.stdout }}/{{ etcd_cert_subdir }}.tgz"
  135. dest: "{{ etcd_cert_config_dir }}"
  136. when: etcd_server_certs_missing | bool
  137. - name: Create a tarball of the etcd ca certs
  138. command: >
  139. tar -czvf {{ etcd_generated_certs_dir }}/{{ etcd_ca_name }}.tgz
  140. -C {{ etcd_ca_dir }} .
  141. args:
  142. creates: "{{ etcd_generated_certs_dir }}/{{ etcd_ca_name }}.tgz"
  143. warn: no
  144. when: etcd_server_certs_missing | bool
  145. delegate_to: "{{ etcd_ca_host }}"
  146. - name: Retrieve etcd ca cert tarball
  147. fetch:
  148. src: "{{ etcd_generated_certs_dir }}/{{ etcd_ca_name }}.tgz"
  149. dest: "{{ g_etcd_server_mktemp.stdout }}/"
  150. flat: yes
  151. fail_on_missing: yes
  152. validate_checksum: yes
  153. when: etcd_server_certs_missing | bool
  154. delegate_to: "{{ etcd_ca_host }}"
  155. - name: Ensure ca directory exists
  156. file:
  157. path: "{{ item }}"
  158. state: directory
  159. with_items:
  160. - "{{ etcd_ca_dir }}"
  161. when: etcd_server_certs_missing | bool
  162. - name: Delete temporary directory
  163. local_action: file path="{{ g_etcd_server_mktemp.stdout }}" state=absent
  164. become: no
  165. changed_when: False
  166. when: etcd_server_certs_missing | bool
  167. - name: Validate permissions on certificate files
  168. file:
  169. path: "{{ item }}"
  170. mode: 0600
  171. owner: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  172. group: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  173. when: etcd_url_scheme == 'https'
  174. with_items:
  175. - "{{ etcd_ca_file }}"
  176. - "{{ etcd_cert_file }}"
  177. - "{{ etcd_key_file }}"
  178. - name: Validate permissions on peer certificate files
  179. file:
  180. path: "{{ item }}"
  181. mode: 0600
  182. owner: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  183. group: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  184. when: etcd_peer_url_scheme == 'https'
  185. with_items:
  186. - "{{ etcd_peer_ca_file }}"
  187. - "{{ etcd_peer_cert_file }}"
  188. - "{{ etcd_peer_key_file }}"
  189. - name: Validate permissions on the config dir
  190. file:
  191. path: "{{ etcd_conf_dir }}"
  192. state: directory
  193. owner: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  194. group: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  195. mode: 0700