fetch_server_certificates_from_ca.yml 6.7 KB

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