2016-11-09 18:44:41 +08:00
|
|
|
---
|
2021-02-09 17:53:22 +08:00
|
|
|
- name: "Check_certs | Register certs that have already been generated on first etcd node"
|
2017-03-03 21:21:01 +08:00
|
|
|
find:
|
|
|
|
paths: "{{ etcd_cert_dir }}"
|
2021-02-09 17:53:22 +08:00
|
|
|
patterns: "ca.pem,node*.pem,member*.pem,admin*.pem"
|
2017-03-15 19:00:42 +08:00
|
|
|
get_checksum: true
|
2019-05-03 05:24:21 +08:00
|
|
|
delegate_to: "{{ groups['etcd'][0] }}"
|
2016-11-09 18:44:41 +08:00
|
|
|
register: etcdcert_master
|
|
|
|
run_once: true
|
|
|
|
|
2016-12-13 17:03:35 +08:00
|
|
|
- name: "Check_certs | Set default value for 'sync_certs', 'gen_certs' and 'etcd_secret_changed' to false"
|
2016-11-09 18:44:41 +08:00
|
|
|
set_fact:
|
|
|
|
sync_certs: false
|
|
|
|
gen_certs: false
|
2016-12-13 17:03:35 +08:00
|
|
|
etcd_secret_changed: false
|
2016-11-09 18:44:41 +08:00
|
|
|
|
2021-02-09 17:53:22 +08:00
|
|
|
- name: "Check certs | Register ca and etcd admin/member certs on etcd hosts"
|
2016-11-09 18:44:41 +08:00
|
|
|
stat:
|
2016-12-27 23:26:22 +08:00
|
|
|
path: "{{ etcd_cert_dir }}/{{ item }}"
|
2024-08-28 13:30:56 +08:00
|
|
|
get_attributes: false
|
|
|
|
get_checksum: true
|
|
|
|
get_mime: false
|
2021-02-09 17:53:22 +08:00
|
|
|
register: etcd_member_certs
|
2024-09-21 20:09:09 +08:00
|
|
|
when: ('etcd' in group_names)
|
2016-12-13 17:03:35 +08:00
|
|
|
with_items:
|
|
|
|
- ca.pem
|
2021-02-09 17:53:22 +08:00
|
|
|
- member-{{ inventory_hostname }}.pem
|
|
|
|
- member-{{ inventory_hostname }}-key.pem
|
|
|
|
- admin-{{ inventory_hostname }}.pem
|
|
|
|
- admin-{{ inventory_hostname }}-key.pem
|
|
|
|
|
|
|
|
- name: "Check certs | Register ca and etcd node certs on kubernetes hosts"
|
|
|
|
stat:
|
|
|
|
path: "{{ etcd_cert_dir }}/{{ item }}"
|
|
|
|
register: etcd_node_certs
|
2024-09-21 20:09:09 +08:00
|
|
|
when: ('k8s_cluster' in group_names)
|
2021-02-09 17:53:22 +08:00
|
|
|
with_items:
|
|
|
|
- ca.pem
|
|
|
|
- node-{{ inventory_hostname }}.pem
|
2016-12-13 17:03:35 +08:00
|
|
|
- node-{{ inventory_hostname }}-key.pem
|
2016-11-09 18:44:41 +08:00
|
|
|
|
2022-09-09 16:29:22 +08:00
|
|
|
- name: "Check_certs | Set 'gen_certs' to true if expected certificates are not on the first etcd node(1/2)"
|
2017-03-03 21:21:01 +08:00
|
|
|
set_fact:
|
|
|
|
gen_certs: true
|
2023-07-05 11:36:54 +08:00
|
|
|
when: force_etcd_cert_refresh or not item in etcdcert_master.files | map(attribute='path') | list
|
2017-03-03 21:21:01 +08:00
|
|
|
run_once: true
|
2019-04-24 13:20:00 +08:00
|
|
|
with_items: "{{ expected_files }}"
|
|
|
|
vars:
|
|
|
|
expected_files: >-
|
2020-06-30 03:39:59 +08:00
|
|
|
['{{ etcd_cert_dir }}/ca.pem',
|
2021-02-09 17:53:22 +08:00
|
|
|
{% set etcd_members = groups['etcd'] %}
|
|
|
|
{% for host in etcd_members %}
|
|
|
|
'{{ etcd_cert_dir }}/admin-{{ host }}.pem',
|
2020-06-30 03:39:59 +08:00
|
|
|
'{{ etcd_cert_dir }}/admin-{{ host }}-key.pem',
|
2021-02-09 17:53:22 +08:00
|
|
|
'{{ etcd_cert_dir }}/member-{{ host }}.pem',
|
|
|
|
'{{ etcd_cert_dir }}/member-{{ host }}-key.pem',
|
|
|
|
{% endfor %}
|
2022-09-09 16:29:22 +08:00
|
|
|
{% set k8s_nodes = groups['kube_control_plane'] %}
|
2021-02-09 17:53:22 +08:00
|
|
|
{% for host in k8s_nodes %}
|
|
|
|
'{{ etcd_cert_dir }}/node-{{ host }}.pem',
|
|
|
|
'{{ etcd_cert_dir }}/node-{{ host }}-key.pem'
|
2023-07-05 11:36:54 +08:00
|
|
|
{% if not loop.last %}{{ ',' }}{% endif %}
|
2020-06-30 03:39:59 +08:00
|
|
|
{% endfor %}]
|
2017-03-03 21:21:01 +08:00
|
|
|
|
2022-09-09 16:29:22 +08:00
|
|
|
- name: "Check_certs | Set 'gen_certs' to true if expected certificates are not on the first etcd node(2/2)"
|
|
|
|
set_fact:
|
|
|
|
gen_certs: true
|
|
|
|
run_once: true
|
|
|
|
with_items: "{{ expected_files }}"
|
|
|
|
vars:
|
|
|
|
expected_files: >-
|
|
|
|
['{{ etcd_cert_dir }}/ca.pem',
|
|
|
|
{% set etcd_members = groups['etcd'] %}
|
|
|
|
{% for host in etcd_members %}
|
|
|
|
'{{ etcd_cert_dir }}/admin-{{ host }}.pem',
|
|
|
|
'{{ etcd_cert_dir }}/admin-{{ host }}-key.pem',
|
|
|
|
'{{ etcd_cert_dir }}/member-{{ host }}.pem',
|
|
|
|
'{{ etcd_cert_dir }}/member-{{ host }}-key.pem',
|
|
|
|
{% endfor %}
|
2023-07-05 11:36:54 +08:00
|
|
|
{% set k8s_nodes = groups['k8s_cluster'] | unique | sort %}
|
2022-09-09 16:29:22 +08:00
|
|
|
{% for host in k8s_nodes %}
|
|
|
|
'{{ etcd_cert_dir }}/node-{{ host }}.pem',
|
|
|
|
'{{ etcd_cert_dir }}/node-{{ host }}-key.pem'
|
2023-07-05 11:36:54 +08:00
|
|
|
{% if not loop.last %}{{ ',' }}{% endif %}
|
2022-09-09 16:29:22 +08:00
|
|
|
{% endfor %}]
|
|
|
|
when:
|
2023-05-18 18:40:33 +08:00
|
|
|
- kube_network_plugin in ["calico", "flannel", "cilium"] or cilium_deploy_additionally | default(false) | bool
|
2022-09-09 16:29:22 +08:00
|
|
|
- kube_network_plugin != "calico" or calico_datastore == "etcd"
|
2023-07-05 11:36:54 +08:00
|
|
|
- force_etcd_cert_refresh or not item in etcdcert_master.files | map(attribute='path') | list
|
2022-09-09 16:29:22 +08:00
|
|
|
|
2023-12-12 18:22:29 +08:00
|
|
|
- name: "Check_certs | Set 'gen_*_certs' groups to track which nodes needs to have certs generated on first etcd node"
|
|
|
|
vars:
|
|
|
|
existing_certs: etcdcert_master.files | map(attribute='path')
|
|
|
|
ansible.builtin.group_by:
|
|
|
|
key: "gen_{{ item.node_type }}_certs_{{ force_etcd_cert_refresh or item.certs is not subset(existing_certs) }}"
|
|
|
|
loop: "{{ cert_files | dict2items(key_name='node_type', value_name='certs') }}"
|
2017-03-03 21:21:01 +08:00
|
|
|
|
2021-02-09 17:53:22 +08:00
|
|
|
- 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"
|
|
|
|
set_fact:
|
|
|
|
etcd_member_requires_sync: true
|
|
|
|
when:
|
2024-09-21 20:09:09 +08:00
|
|
|
- ('etcd' in group_names)
|
2023-07-05 11:36:54 +08:00
|
|
|
- (not etcd_member_certs.results[0].stat.exists | default(false)) or
|
|
|
|
(not etcd_member_certs.results[1].stat.exists | default(false)) or
|
|
|
|
(not etcd_member_certs.results[2].stat.exists | default(false)) or
|
|
|
|
(not etcd_member_certs.results[3].stat.exists | default(false)) or
|
|
|
|
(not etcd_member_certs.results[4].stat.exists | default(false)) or
|
|
|
|
(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
|
|
|
|
(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
|
|
|
|
(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
|
|
|
|
(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
|
|
|
|
(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(''))
|
2021-02-09 17:53:22 +08:00
|
|
|
|
|
|
|
- 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"
|
|
|
|
set_fact:
|
|
|
|
kubernetes_host_requires_sync: true
|
|
|
|
when:
|
2024-09-21 20:09:09 +08:00
|
|
|
- ('k8s_cluster' in group_names) and
|
2021-02-09 17:53:22 +08:00
|
|
|
inventory_hostname not in groups['etcd']
|
2023-07-05 11:36:54 +08:00
|
|
|
- (not etcd_node_certs.results[0].stat.exists | default(false)) or
|
|
|
|
(not etcd_node_certs.results[1].stat.exists | default(false)) or
|
|
|
|
(not etcd_node_certs.results[2].stat.exists | default(false)) or
|
|
|
|
(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
|
|
|
|
(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
|
|
|
|
(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(''))
|
2021-02-09 17:53:22 +08:00
|
|
|
|
2016-11-09 18:44:41 +08:00
|
|
|
- name: "Check_certs | Set 'sync_certs' to true"
|
|
|
|
set_fact:
|
|
|
|
sync_certs: true
|
2018-10-16 14:47:04 +08:00
|
|
|
when:
|
2023-07-05 11:36:54 +08:00
|
|
|
- etcd_member_requires_sync | default(false) or
|
|
|
|
kubernetes_host_requires_sync | default(false) or
|
2023-12-12 18:22:29 +08:00
|
|
|
'gen_master_certs_True' in group_names or
|
|
|
|
'gen_node_certs_True' in group_names
|