main.yml 6.1 KB

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