You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
8.1 KiB

  1. ---
  2. - name: "Check_certs | Register certs that have already been generated on first etcd node"
  3. find:
  4. paths: "{{ etcd_cert_dir }}"
  5. patterns: "ca.pem,node*.pem,member*.pem,admin*.pem"
  6. get_checksum: true
  7. delegate_to: "{{ groups['etcd'][0] }}"
  8. register: etcdcert_master
  9. run_once: true
  10. - name: "Check_certs | Set default value for 'sync_certs', 'gen_certs' and 'etcd_secret_changed' to false"
  11. set_fact:
  12. sync_certs: false
  13. gen_certs: false
  14. etcd_secret_changed: false
  15. - name: "Check certs | Register ca and etcd admin/member certs on etcd hosts"
  16. stat:
  17. path: "{{ etcd_cert_dir }}/{{ item }}"
  18. get_attributes: no
  19. get_checksum: yes
  20. get_mime: no
  21. register: etcd_member_certs
  22. when: inventory_hostname in groups['etcd']
  23. with_items:
  24. - ca.pem
  25. - member-{{ inventory_hostname }}.pem
  26. - member-{{ inventory_hostname }}-key.pem
  27. - admin-{{ inventory_hostname }}.pem
  28. - admin-{{ inventory_hostname }}-key.pem
  29. - name: "Check certs | Register ca and etcd node certs on kubernetes hosts"
  30. stat:
  31. path: "{{ etcd_cert_dir }}/{{ item }}"
  32. register: etcd_node_certs
  33. when: inventory_hostname in groups['k8s_cluster']
  34. with_items:
  35. - ca.pem
  36. - node-{{ inventory_hostname }}.pem
  37. - node-{{ inventory_hostname }}-key.pem
  38. - name: "Check_certs | Set 'gen_certs' to true if expected certificates are not on the first etcd node(1/2)"
  39. set_fact:
  40. gen_certs: true
  41. when: force_etcd_cert_refresh or not item in etcdcert_master.files|map(attribute='path') | list
  42. run_once: true
  43. with_items: "{{ expected_files }}"
  44. vars:
  45. expected_files: >-
  46. ['{{ etcd_cert_dir }}/ca.pem',
  47. {% set etcd_members = groups['etcd'] %}
  48. {% for host in etcd_members %}
  49. '{{ etcd_cert_dir }}/admin-{{ host }}.pem',
  50. '{{ etcd_cert_dir }}/admin-{{ host }}-key.pem',
  51. '{{ etcd_cert_dir }}/member-{{ host }}.pem',
  52. '{{ etcd_cert_dir }}/member-{{ host }}-key.pem',
  53. {% endfor %}
  54. {% set k8s_nodes = groups['kube_control_plane'] %}
  55. {% for host in k8s_nodes %}
  56. '{{ etcd_cert_dir }}/node-{{ host }}.pem',
  57. '{{ etcd_cert_dir }}/node-{{ host }}-key.pem'
  58. {% if not loop.last %}{{','}}{% endif %}
  59. {% endfor %}]
  60. - name: "Check_certs | Set 'gen_certs' to true if expected certificates are not on the first etcd node(2/2)"
  61. set_fact:
  62. gen_certs: true
  63. run_once: true
  64. with_items: "{{ expected_files }}"
  65. vars:
  66. expected_files: >-
  67. ['{{ etcd_cert_dir }}/ca.pem',
  68. {% set etcd_members = groups['etcd'] %}
  69. {% for host in etcd_members %}
  70. '{{ etcd_cert_dir }}/admin-{{ host }}.pem',
  71. '{{ etcd_cert_dir }}/admin-{{ host }}-key.pem',
  72. '{{ etcd_cert_dir }}/member-{{ host }}.pem',
  73. '{{ etcd_cert_dir }}/member-{{ host }}-key.pem',
  74. {% endfor %}
  75. {% set k8s_nodes = groups['k8s_cluster']|unique|sort %}
  76. {% for host in k8s_nodes %}
  77. '{{ etcd_cert_dir }}/node-{{ host }}.pem',
  78. '{{ etcd_cert_dir }}/node-{{ host }}-key.pem'
  79. {% if not loop.last %}{{','}}{% endif %}
  80. {% endfor %}]
  81. when:
  82. - kube_network_plugin in ["calico", "flannel", "canal", "cilium"] or cilium_deploy_additionally | default(false) | bool
  83. - kube_network_plugin != "calico" or calico_datastore == "etcd"
  84. - force_etcd_cert_refresh or not item in etcdcert_master.files|map(attribute='path') | list
  85. - name: "Check_certs | Set 'gen_master_certs' object to track whether member and admin certs exist on first etcd node"
  86. set_fact:
  87. gen_master_certs: |-
  88. {
  89. {% set etcd_members = groups['etcd'] -%}
  90. {% set existing_certs = etcdcert_master.files|map(attribute='path')|list|sort %}
  91. {% for host in etcd_members -%}
  92. {% set member_cert = "%s/member-%s.pem"|format(etcd_cert_dir, host) %}
  93. {% set member_key = "%s/member-%s-key.pem"|format(etcd_cert_dir, host) %}
  94. {% set admin_cert = "%s/admin-%s.pem"|format(etcd_cert_dir, host) %}
  95. {% set admin_key = "%s/admin-%s-key.pem"|format(etcd_cert_dir, host) %}
  96. {% if force_etcd_cert_refresh -%}
  97. "{{ host }}": True,
  98. {% elif member_cert in existing_certs and member_key in existing_certs and admin_cert in existing_certs and admin_key in existing_certs -%}
  99. "{{ host }}": False,
  100. {% else -%}
  101. "{{ host }}": True,
  102. {% endif -%}
  103. {% endfor %}
  104. }
  105. run_once: true
  106. - name: "Check_certs | Set 'gen_node_certs' object to track whether node certs exist on first etcd node"
  107. set_fact:
  108. gen_node_certs: |-
  109. {
  110. {% set k8s_nodes = groups['k8s_cluster'] -%}
  111. {% set existing_certs = etcdcert_master.files|map(attribute='path')|list|sort %}
  112. {% for host in k8s_nodes -%}
  113. {% set host_cert = "%s/node-%s.pem"|format(etcd_cert_dir, host) %}
  114. {% set host_key = "%s/node-%s-key.pem"|format(etcd_cert_dir, host) %}
  115. {% if force_etcd_cert_refresh -%}
  116. "{{ host }}": True,
  117. {% elif host_cert in existing_certs and host_key in existing_certs -%}
  118. "{{ host }}": False,
  119. {% else -%}
  120. "{{ host }}": True,
  121. {% endif -%}
  122. {% endfor %}
  123. }
  124. run_once: true
  125. - name: "Check_certs | Set 'etcd_member_requires_sync' to true if ca or member/admin cert and key don't exist on etcd member or checksum doesn't match"
  126. set_fact:
  127. etcd_member_requires_sync: true
  128. when:
  129. - inventory_hostname in groups['etcd']
  130. - (not etcd_member_certs.results[0].stat.exists|default(false)) or
  131. (not etcd_member_certs.results[1].stat.exists|default(false)) or
  132. (not etcd_member_certs.results[2].stat.exists|default(false)) or
  133. (not etcd_member_certs.results[3].stat.exists|default(false)) or
  134. (not etcd_member_certs.results[4].stat.exists|default(false)) or
  135. (etcd_member_certs.results[0].stat.checksum|default('') != etcdcert_master.files|selectattr("path", "equalto", etcd_member_certs.results[0].stat.path)|map(attribute="checksum")|first|default('')) or
  136. (etcd_member_certs.results[1].stat.checksum|default('') != etcdcert_master.files|selectattr("path", "equalto", etcd_member_certs.results[1].stat.path)|map(attribute="checksum")|first|default('')) or
  137. (etcd_member_certs.results[2].stat.checksum|default('') != etcdcert_master.files|selectattr("path", "equalto", etcd_member_certs.results[2].stat.path)|map(attribute="checksum")|first|default('')) or
  138. (etcd_member_certs.results[3].stat.checksum|default('') != etcdcert_master.files|selectattr("path", "equalto", etcd_member_certs.results[3].stat.path)|map(attribute="checksum")|first|default('')) or
  139. (etcd_member_certs.results[4].stat.checksum|default('') != etcdcert_master.files|selectattr("path", "equalto", etcd_member_certs.results[4].stat.path)|map(attribute="checksum")|first|default(''))
  140. - name: "Check_certs | Set 'kubernetes_host_requires_sync' to true if ca or node cert and key don't exist on kubernetes host or checksum doesn't match"
  141. set_fact:
  142. kubernetes_host_requires_sync: true
  143. when:
  144. - inventory_hostname in groups['k8s_cluster'] and
  145. inventory_hostname not in groups['etcd']
  146. - (not etcd_node_certs.results[0].stat.exists|default(false)) or
  147. (not etcd_node_certs.results[1].stat.exists|default(false)) or
  148. (not etcd_node_certs.results[2].stat.exists|default(false)) or
  149. (etcd_node_certs.results[0].stat.checksum|default('') != etcdcert_master.files|selectattr("path", "equalto", etcd_node_certs.results[0].stat.path)|map(attribute="checksum")|first|default('')) or
  150. (etcd_node_certs.results[1].stat.checksum|default('') != etcdcert_master.files|selectattr("path", "equalto", etcd_node_certs.results[1].stat.path)|map(attribute="checksum")|first|default('')) or
  151. (etcd_node_certs.results[2].stat.checksum|default('') != etcdcert_master.files|selectattr("path", "equalto", etcd_node_certs.results[2].stat.path)|map(attribute="checksum")|first|default(''))
  152. - name: "Check_certs | Set 'sync_certs' to true"
  153. set_fact:
  154. sync_certs: true
  155. when:
  156. - etcd_member_requires_sync|default(false) or
  157. kubernetes_host_requires_sync|default(false) or
  158. (inventory_hostname in gen_master_certs and gen_master_certs[inventory_hostname]) or
  159. (inventory_hostname in gen_node_certs and gen_node_certs[inventory_hostname])