|
1 | 1 | ---
|
2 |
| -- name: 'Select candidate host to run init' |
| 2 | +- name: 'Find nodes to join' |
3 | 3 | set_fact:
|
4 |
| - kubeadm_host: '{{ groups.cp_running|default(ansible_play_hosts, true)|first }}' |
5 |
| - |
6 |
| -- name: 'Retrieve a valid bootstrap token' |
7 |
| - import_tasks: bootstrap_token.yml |
8 |
| - |
9 |
| -- name: 'Create bootstrap token if no valid found' |
10 |
| - command: kubeadm token create |
11 |
| - run_once: true |
12 |
| - delegate_to: '{{ kubeadm_host }}' |
13 |
| - when: valid_bootstrap_tokens|length == 0 |
14 |
| - |
15 |
| -- name: 'Retrieve a valid bootstrap token' |
16 |
| - import_tasks: bootstrap_token.yml |
17 |
| - when: valid_bootstrap_tokens|length == 0 |
18 |
| - |
19 |
| -# TODO: fix two following tasks to be more platform dependent |
20 |
| -- name: 'Install python-openssl' |
21 |
| - package: |
22 |
| - name: >- |
23 |
| - {%- if ansible_python.version.major > 2 -%} |
24 |
| - {{ python3_openssl }} |
25 |
| - {%- else -%} |
26 |
| - {{ python2_openssl }} |
27 |
| - {%- endif -%} |
28 |
| - state: present |
29 |
| - run_once: true |
30 |
| - delegate_to: '{{ kubeadm_host }}' |
31 |
| - |
32 |
| -- name: 'Get info from ca' |
33 |
| - openssl_certificate_info: |
34 |
| - path: /etc/kubernetes/pki/ca.crt |
35 |
| - run_once: true |
36 |
| - delegate_to: '{{ kubeadm_host }}' |
37 |
| - register: ca_info |
38 |
| - when: not(groups.cp_init is defined and ansible_check_mode) |
39 |
| - |
40 |
| -- name: 'Display Kubernetes CA(cert) properties' |
41 |
| - debug: |
42 |
| - var: ca_info |
43 |
| - verbosity: 1 |
44 |
| - run_once: true |
45 |
| - |
46 |
| -- name: 'List current nodes' |
47 |
| - kubectl: |
48 |
| - state: get |
49 |
| - resource_type: nodes |
50 |
| - kubeconfig: /etc/kubernetes/admin.conf |
51 |
| - run_once: true |
52 |
| - delegate_to: '{{ kubeadm_host }}' |
53 |
| - register: current_nodes |
54 |
| - when: |
55 |
| - - not(found_kubectl.rc == 1 and ansible_check_mode) |
56 |
| - |
57 |
| -- name: 'Compute list of "to-join" nodes' |
58 |
| - set_fact: |
59 |
| - # "items" cannot be defaulted easily as jinja fallback on using method instead |
60 |
| - to_join_cp: >- |
61 |
| - {{ ansible_play_hosts|difference( |
62 |
| - ({"items": []}|combine(current_nodes))["items"]|map(attribute="metadata.name")) }} |
63 |
| - cert_encryption_key: >- |
64 |
| - {{ lookup('password', '/dev/null length=64 chars=hexdigits') }} |
65 |
| - run_once: true |
66 |
| - |
67 |
| -- name: 'Display list of node that need to be joined' |
68 |
| - debug: |
69 |
| - var: to_join_cp |
70 |
| - verbosity: 1 |
71 |
| - run_once: true |
72 |
| - |
73 |
| -- name: 'Upload certificates if control-plane node need to be joined' |
74 |
| - command: >- |
75 |
| - kubeadm init phase upload-certs |
76 |
| - --upload-certs |
77 |
| - --certificate-key {{ cert_encryption_key }} |
78 |
| - no_log: '{{ sensitive_debug|bool }}' |
79 |
| - run_once: true |
80 |
| - delegate_to: '{{ kubeadm_host }}' |
81 |
| - when: to_join_cp|length > 0 |
| 4 | + nodes_to_join: >- |
| 5 | + {{ q('inventory_hostnames', kube_cp_group ~ ':' ~ kube_worker_group) |
| 6 | + |map('extract', hostvars) |
| 7 | + |rejectattr('_kubelet_config_stat.stat.exists') |
| 8 | + |map(attribute='inventory_hostname')|list }} |
| 9 | + run_once: true |
| 10 | + |
| 11 | +- name: 'Create bootstrap token' |
| 12 | + when: nodes_to_join|length > 0 |
| 13 | + block: |
| 14 | + - name: 'Retrieve a valid bootstrap token' |
| 15 | + import_role: |
| 16 | + name: bootstrap_token_get |
| 17 | + |
| 18 | + - name: 'Create bootstrap token if no valid found' |
| 19 | + command: kubeadm token create |
| 20 | + run_once: true |
| 21 | + delegate_to: '{{ cp_node }}' |
| 22 | + when: _valid_bootstrap_tokens|length == 0 |
| 23 | + |
| 24 | + - name: 'Retrieve a valid bootstrap token' |
| 25 | + import_role: |
| 26 | + name: bootstrap_token_get |
| 27 | + when: _valid_bootstrap_tokens|length == 0 |
0 commit comments