Skip to content

Commit f823042

Browse files
committed
now uses bootstrap tokens instead of cloud-init metadata
1 parent 25eea00 commit f823042

File tree

17 files changed

+88
-71
lines changed

17 files changed

+88
-71
lines changed

ansible/bootstrap.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,4 @@
312312
tasks:
313313
- ansible.builtin.include_role:
314314
name: k3s
315-
tasks_from: install.yml
315+
tasks_from: "{{ 'install.yml' if 'builder' in group_names else 'runtime.yml' }}"

ansible/roles/k3s/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ k3s_version: "v1.31.0+k3s1"
33
k3s_selinux_release: v1.6.latest.1
44
k3s_selinux_rpm_version: 1.6-1
55
k3s_helm_version: v3.11.0
6+
k3s_bootstrap_token_expiry: 5m

ansible/roles/k3s/files/start_k3s.yml

Lines changed: 0 additions & 44 deletions
This file was deleted.

ansible/roles/k3s/tasks/install.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,3 @@
7171
ansible.builtin.lineinfile:
7272
path: /etc/environment
7373
line: "KUBECONFIG=/etc/rancher/k3s/k3s.yaml"
74-
75-
- name: Install ansible-init playbook for k3s agent or server activation
76-
copy:
77-
src: start_k3s.yml
78-
dest: /etc/ansible-init/playbooks/0-start-k3s.yml
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
- name: Check if k3s agents are already connected
3+
service_facts:
4+
register: services_state
5+
6+
- name: Initialise and authenticate k3s server and agents
7+
vars:
8+
k3s_server_name: "{{ hostvars[groups['k3s_server'].0].ansible_host }}"
9+
access_ip: "{{ ansible_default_ipv4.address }}"
10+
services_states: > # getting list of all unique agent service states
11+
groups['k3s_agent']
12+
| map('extract', hostvars, ['services', 'k3s-agent.service', 'state'])
13+
| unique
14+
when: not (services_state | length == 1 and services_state[0] == 'running')
15+
block:
16+
- name: Initialise server and generate bootstrap tokens
17+
when: inventory_hostname in groups['k3s_server']
18+
block:
19+
- name: Template k3s env file
20+
ansible.builtin.template:
21+
dest: /etc/systemd/system/k3s.service.env
22+
src: k3s.service.env.j2
23+
24+
- name: Start k3s server
25+
ansible.builtin.systemd:
26+
name: k3s
27+
daemon_reload: true
28+
state: started
29+
enabled: true
30+
31+
- name: Generate bootstrap token
32+
no_log: true
33+
shell:
34+
cmd: "k3s token create --ttl {{ k3s_bootstrap_token_expiry }}"
35+
register: _token_output
36+
37+
- name: Initialise agents
38+
when: inventory_hostname in groups['k3s_agent']
39+
block:
40+
- name: Template k3s agent env file
41+
ansible.builtin.template:
42+
dest: /etc/systemd/system/k3s-agent.service.env
43+
src: k3s-agent.service.env.j2
44+
45+
- name: Ensure password directory exists
46+
ansible.builtin.file:
47+
path: "/etc/rancher/node"
48+
state: directory
49+
50+
- name: Write node password
51+
ansible.builtin.copy:
52+
dest: /etc/rancher/node/password
53+
content: "{{ vault_k3s_node_password }}"
54+
owner: root
55+
group: root
56+
mode: 640 # normal k3s install is 644 but that doesn't feel right
57+
58+
- name: Start k3s agent
59+
ansible.builtin.systemd:
60+
name: k3s-agent
61+
daemon_reload: true
62+
state: started
63+
enabled: true
64+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
K3S_NODE_IP={{ access_ip }}
2+
K3S_TOKEN={{ hostvars[groups['control'] | first]._token_output.stdout }}
3+
K3S_URL=https://{{ k3s_server_name }}:6443
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
K3S_NODE_IP={{ access_ip }}

ansible/roles/passwords/defaults/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ slurm_appliance_secrets:
88
vault_openhpc_mungekey: "{{ secrets_openhpc_mungekey | default(vault_openhpc_mungekey | default(secrets_openhpc_mungekey_default)) }}"
99
vault_freeipa_ds_password: "{{ vault_freeipa_ds_password | default(lookup('password', '/dev/null')) }}"
1010
vault_freeipa_admin_password: "{{ vault_freeipa_admin_password | default(lookup('password', '/dev/null')) }}"
11-
vault_k3s_token: "{{ vault_k3s_token | default(lookup('ansible.builtin.password', '/dev/null', length=64)) }}"
11+
vault_k3s_node_password: "{{ vault_k3s_node_password | default(lookup('ansible.builtin.password', '/dev/null', length=64)) }}"
1212
vault_pulp_admin_password: "{{ vault_pulp_admin_password | default(lookup('password', '/dev/null', chars=['ascii_letters', 'digits'])) }}"
1313
vault_demo_user_password: "{{ vault_demo_user_password | default(lookup('password', '/dev/null')) }}"
1414

ansible/roles/passwords/templates/k3s-token.auto.tfvars.json.j2

Lines changed: 0 additions & 3 deletions
This file was deleted.

environments/common/inventory/groups

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,16 @@ freeipa_client
145145
[compute_init]
146146
# EXPERIMENTAL: Compute hosts to enable joining cluster on boot on
147147

148-
[k3s]
148+
[k3s:children]
149149
# Hosts to run k3s server/agent
150+
k3s_server
151+
k3s_agent
152+
153+
[k3s_server]
154+
# Hosts to run k3s server (should only be single node i.e control node)
155+
156+
[k3s_agent]
157+
# Hosts to run k3s agent
150158

151159
[k9s]
152160
# Hosts to install k9s on

0 commit comments

Comments
 (0)