fetch_server_certificates_from_ca.yml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. | lib_utils_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. # delegated_serial_command is a custom module in lib_utils
  49. - name: Sign and create the server crt
  50. delegated_serial_command:
  51. command: >
  52. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  53. -out {{ etcd_cert_prefix }}server.crt
  54. -in {{ etcd_cert_prefix }}server.csr
  55. -extensions {{ etcd_ca_exts_server }} -batch
  56. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  57. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  58. ~ etcd_cert_prefix ~ 'server.crt' }}"
  59. environment:
  60. SAN: "IP:{{ etcd_ip }}"
  61. when: etcd_server_certs_missing | bool
  62. delegate_to: "{{ etcd_ca_host }}"
  63. - name: Create the peer csr
  64. command: >
  65. openssl req -new -keyout {{ etcd_cert_prefix }}peer.key
  66. -config {{ etcd_openssl_conf }}
  67. -out {{ etcd_cert_prefix }}peer.csr
  68. -reqexts {{ etcd_req_ext }} -batch -nodes
  69. -subj /CN={{ etcd_hostname }}
  70. args:
  71. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  72. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  73. ~ etcd_cert_prefix ~ 'peer.csr' }}"
  74. environment:
  75. SAN: "IP:{{ etcd_ip }},DNS:{{ etcd_hostname }}"
  76. when: etcd_server_certs_missing | bool
  77. delegate_to: "{{ etcd_ca_host }}"
  78. # Certificates must be signed serially in order to avoid competing
  79. # for the serial file.
  80. # delegated_serial_command is a custom module in lib_utils
  81. - name: Sign and create the peer crt
  82. delegated_serial_command:
  83. command: >
  84. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  85. -out {{ etcd_cert_prefix }}peer.crt
  86. -in {{ etcd_cert_prefix }}peer.csr
  87. -extensions {{ etcd_ca_exts_peer }} -batch
  88. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  89. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  90. ~ etcd_cert_prefix ~ 'peer.crt' }}"
  91. environment:
  92. SAN: "IP:{{ etcd_ip }}"
  93. when: etcd_server_certs_missing | bool
  94. delegate_to: "{{ etcd_ca_host }}"
  95. - file:
  96. src: "{{ etcd_ca_cert }}"
  97. dest: "{{ etcd_generated_certs_dir}}/{{ etcd_cert_subdir }}/{{ etcd_cert_prefix }}ca.crt"
  98. state: hard
  99. when: etcd_server_certs_missing | bool
  100. delegate_to: "{{ etcd_ca_host }}"
  101. - name: Create a tarball of the etcd certs
  102. command: >
  103. tar -czvf {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz
  104. -C {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }} .
  105. args:
  106. creates: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  107. # Disables the following warning:
  108. # Consider using unarchive module rather than running tar
  109. warn: no
  110. when: etcd_server_certs_missing | bool
  111. delegate_to: "{{ etcd_ca_host }}"
  112. - name: Retrieve etcd cert tarball
  113. fetch:
  114. src: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  115. dest: "/tmp"
  116. fail_on_missing: yes
  117. validate_checksum: yes
  118. when: etcd_server_certs_missing | bool
  119. delegate_to: "{{ etcd_ca_host }}"
  120. - name: Ensure certificate directory exists
  121. file:
  122. path: "{{ item }}"
  123. state: directory
  124. with_items:
  125. - "{{ etcd_cert_config_dir }}"
  126. when: etcd_server_certs_missing | bool
  127. - name: Unarchive cert tarball
  128. unarchive:
  129. src: "/tmp/{{ inventory_hostname }}/{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  130. dest: "{{ etcd_cert_config_dir }}"
  131. when: etcd_server_certs_missing | bool
  132. - name: Create a tarball of the etcd ca certs
  133. command: >
  134. tar -czvf {{ etcd_generated_certs_dir }}/{{ etcd_ca_name }}.tgz
  135. -C {{ etcd_ca_dir }} .
  136. args:
  137. creates: "{{ etcd_generated_certs_dir }}/{{ etcd_ca_name }}.tgz"
  138. warn: no
  139. when: etcd_server_certs_missing | bool
  140. delegate_to: "{{ etcd_ca_host }}"
  141. - name: Retrieve etcd ca cert tarball
  142. fetch:
  143. src: "{{ etcd_generated_certs_dir }}/{{ etcd_ca_name }}.tgz"
  144. dest: "/tmp"
  145. fail_on_missing: yes
  146. validate_checksum: yes
  147. when: etcd_server_certs_missing | bool
  148. delegate_to: "{{ etcd_ca_host }}"
  149. - name: Ensure ca directory exists
  150. file:
  151. path: "{{ item }}"
  152. state: directory
  153. with_items:
  154. - "{{ etcd_ca_dir }}"
  155. when: etcd_server_certs_missing | bool
  156. - name: Delete temporary directory
  157. local_action: file path="/tmp/{{ inventory_hostname }}" state=absent
  158. changed_when: False
  159. when: etcd_server_certs_missing | bool
  160. - name: Validate permissions on certificate files
  161. file:
  162. path: "{{ item }}"
  163. mode: 0600
  164. owner: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  165. group: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  166. when: etcd_url_scheme == 'https'
  167. with_items:
  168. - "{{ etcd_ca_file }}"
  169. - "{{ etcd_cert_file }}"
  170. - "{{ etcd_key_file }}"
  171. - name: Validate permissions on peer certificate files
  172. file:
  173. path: "{{ item }}"
  174. mode: 0600
  175. owner: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  176. group: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  177. when: etcd_peer_url_scheme == 'https'
  178. with_items:
  179. - "{{ etcd_peer_ca_file }}"
  180. - "{{ etcd_peer_cert_file }}"
  181. - "{{ etcd_peer_key_file }}"
  182. - name: Validate permissions on the config dir
  183. file:
  184. path: "{{ etcd_conf_dir }}"
  185. state: directory
  186. owner: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  187. group: "{{ 'etcd' if not etcd_is_containerized | bool else omit }}"
  188. mode: 0700