fetch_server_certificates_from_ca.yml 7.3 KB

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