fetch_server_certificates_from_ca.yml 6.8 KB

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