fetch_server_certificates_from_ca.yml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ---
  2. - name: Install etcd
  3. package:
  4. name: "etcd"
  5. state: present
  6. when:
  7. - not etcd_is_atomic | bool
  8. register: result
  9. until: result is succeeded
  10. - name: Check status of etcd certificates
  11. stat:
  12. path: "{{ item }}"
  13. get_checksum: false
  14. get_attributes: false
  15. get_mime: false
  16. with_items:
  17. - "{{ etcd_cert_config_dir }}/{{ etcd_cert_prefix }}server.crt"
  18. - "{{ etcd_cert_config_dir }}/{{ etcd_cert_prefix }}peer.crt"
  19. - "{{ etcd_cert_config_dir }}/{{ etcd_cert_prefix }}ca.crt"
  20. register: g_etcd_server_cert_stat_result
  21. when: not etcd_certificates_redeploy | default(false) | bool
  22. - set_fact:
  23. etcd_server_certs_missing: "{{ true if etcd_certificates_redeploy | default(false) | bool
  24. else (False in (g_etcd_server_cert_stat_result.results
  25. | default({})
  26. | lib_utils_oo_collect(attribute='stat.exists')
  27. | list)) }}"
  28. - name: Ensure generated_certs directory present
  29. file:
  30. path: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  31. state: directory
  32. mode: 0700
  33. when: etcd_server_certs_missing | bool
  34. delegate_to: "{{ etcd_ca_host }}"
  35. - name: Create the server csr
  36. command: >
  37. openssl req -new -keyout {{ etcd_cert_prefix }}server.key
  38. -config {{ etcd_openssl_conf }}
  39. -out {{ etcd_cert_prefix }}server.csr
  40. -reqexts {{ etcd_req_ext }} -batch -nodes
  41. -subj /CN={{ etcd_hostname }}
  42. args:
  43. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  44. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  45. ~ etcd_cert_prefix ~ 'server.csr' }}"
  46. environment:
  47. SAN: "IP:{{ etcd_ip }},DNS:{{ etcd_hostname }}"
  48. when: etcd_server_certs_missing | bool
  49. delegate_to: "{{ etcd_ca_host }}"
  50. # Certificates must be signed serially in order to avoid competing
  51. # for the serial file.
  52. # delegated_serial_command is a custom module in lib_utils
  53. - name: Sign and create the server crt
  54. delegated_serial_command:
  55. command: >
  56. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  57. -out {{ etcd_cert_prefix }}server.crt
  58. -in {{ etcd_cert_prefix }}server.csr
  59. -extensions {{ etcd_ca_exts_server }} -batch
  60. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  61. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  62. ~ etcd_cert_prefix ~ 'server.crt' }}"
  63. environment:
  64. SAN: "IP:{{ etcd_ip }}"
  65. when: etcd_server_certs_missing | bool
  66. delegate_to: "{{ etcd_ca_host }}"
  67. - name: Create the peer csr
  68. command: >
  69. openssl req -new -keyout {{ etcd_cert_prefix }}peer.key
  70. -config {{ etcd_openssl_conf }}
  71. -out {{ etcd_cert_prefix }}peer.csr
  72. -reqexts {{ etcd_req_ext }} -batch -nodes
  73. -subj /CN={{ etcd_hostname }}
  74. args:
  75. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  76. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  77. ~ etcd_cert_prefix ~ 'peer.csr' }}"
  78. environment:
  79. SAN: "IP:{{ etcd_ip }},DNS:{{ etcd_hostname }}"
  80. when: etcd_server_certs_missing | bool
  81. delegate_to: "{{ etcd_ca_host }}"
  82. # Certificates must be signed serially in order to avoid competing
  83. # for the serial file.
  84. # delegated_serial_command is a custom module in lib_utils
  85. - name: Sign and create the peer crt
  86. delegated_serial_command:
  87. command: >
  88. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  89. -out {{ etcd_cert_prefix }}peer.crt
  90. -in {{ etcd_cert_prefix }}peer.csr
  91. -extensions {{ etcd_ca_exts_peer }} -batch
  92. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  93. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  94. ~ etcd_cert_prefix ~ 'peer.crt' }}"
  95. environment:
  96. SAN: "IP:{{ etcd_ip }}"
  97. when: etcd_server_certs_missing | bool
  98. delegate_to: "{{ etcd_ca_host }}"
  99. - file:
  100. src: "{{ etcd_ca_cert }}"
  101. dest: "{{ etcd_generated_certs_dir}}/{{ etcd_cert_subdir }}/{{ etcd_cert_prefix }}ca.crt"
  102. state: hard
  103. force: yes
  104. when: etcd_server_certs_missing | bool
  105. delegate_to: "{{ etcd_ca_host }}"
  106. - name: Create a tarball of the etcd certs
  107. command: >
  108. tar -czvf {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz
  109. -C {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }} .
  110. args:
  111. creates: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  112. # Disables the following warning:
  113. # Consider using unarchive module rather than running tar
  114. warn: no
  115. when: etcd_server_certs_missing | bool
  116. delegate_to: "{{ etcd_ca_host }}"
  117. - name: Retrieve etcd cert tarball
  118. fetch:
  119. src: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  120. dest: "/tmp"
  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: "/tmp/{{ inventory_hostname }}/{{ etcd_generated_certs_dir }}/{{ 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: "/tmp"
  150. fail_on_missing: yes
  151. validate_checksum: yes
  152. when: etcd_server_certs_missing | bool
  153. delegate_to: "{{ etcd_ca_host }}"
  154. - name: Ensure ca directory exists
  155. file:
  156. path: "{{ item }}"
  157. state: directory
  158. with_items:
  159. - "{{ etcd_ca_dir }}"
  160. when: etcd_server_certs_missing | bool
  161. - name: Delete temporary directory
  162. local_action: file path="/tmp/{{ inventory_hostname }}" state=absent
  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. when: etcd_url_scheme == 'https'
  170. with_items:
  171. - "{{ etcd_ca_file }}"
  172. - "{{ etcd_cert_file }}"
  173. - "{{ etcd_key_file }}"
  174. - name: Validate permissions on peer certificate files
  175. file:
  176. path: "{{ item }}"
  177. mode: 0600
  178. when: etcd_peer_url_scheme == 'https'
  179. with_items:
  180. - "{{ etcd_peer_ca_file }}"
  181. - "{{ etcd_peer_cert_file }}"
  182. - "{{ etcd_peer_key_file }}"
  183. - name: Validate permissions on the config dir
  184. file:
  185. path: "{{ etcd_conf_dir }}"
  186. state: directory
  187. mode: 0700