main.yml 6.3 KB

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