Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate token #375

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions roles/k3s_agent/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
server_group: server # noqa var-naming[no-role-prefix]
k3s_server_location: "/var/lib/rancher/k3s" # noqa var-naming[no-role-prefix]
systemd_dir: "/etc/systemd/system" # noqa var-naming[no-role-prefix]
api_port: 6443 # noqa var-naming[no-role-prefix]
4 changes: 4 additions & 0 deletions roles/k3s_agent/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
INSTALL_K3S_EXEC: "agent"
changed_when: true

- name: Get the token from the first server

Check failure on line 38 in roles/k3s_agent/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

fqcn[action-core]

Use FQCN for builtin module actions (set_fact).
set_fact:
token: "{{ hostvars[groups[server_group][0]].token }}"

- name: Delete any existing token from the environment if different from the new one
ansible.builtin.lineinfile:
state: absent
Expand Down
26 changes: 24 additions & 2 deletions roles/k3s_server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@
ansible.builtin.lineinfile:
state: absent
path: "{{ systemd_dir }}/k3s.service.env"
regexp: "^K3S_TOKEN=\\s*(?!{{ token }}\\s*$)"
regexp: "^K3S_TOKEN=\\s*(?!{{ token | default('') }}\\s*$)"

# Add the token to the environment.
# Add the token to the environment if it has been provided.
# Otherwise, let the first server create one on the first run.
- name: Add token as an environment variable
no_log: true # avoid logging the server token
ansible.builtin.lineinfile:
path: "{{ systemd_dir }}/k3s.service.env"
line: "K3S_TOKEN={{ token }}"
when: token is defined

- name: Restart K3s service
when:
Expand Down Expand Up @@ -182,11 +184,31 @@
changed_when:
- mv_result.rc == 0

- name: Get the token if randomly generated
when: token is not defined
block:
- name: Wait for token

Check failure on line 190 in roles/k3s_server/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

fqcn[action-core]

Use FQCN for builtin module actions (wait_for).
wait_for:
path: /var/lib/rancher/k3s/server/token

- name: Read node-token from master

Check failure on line 194 in roles/k3s_server/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

fqcn[action-core]

Use FQCN for builtin module actions (slurp).
slurp:
src: /var/lib/rancher/k3s/server/token
register: node_token

- name: Store Master node-token

Check failure on line 199 in roles/k3s_server/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

fqcn[action-core]

Use FQCN for builtin module actions (set_fact).
set_fact:
token: "{{ node_token.content | b64decode | regex_replace('\n', '') }}"

- name: Start other server if any and verify status
when:
- (groups[server_group] | length) > 1
- inventory_hostname != groups[server_group][0]
block:
- name: Get the token from the first server

Check failure on line 208 in roles/k3s_server/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

fqcn[action-core]

Use FQCN for builtin module actions (set_fact).
set_fact:
token: "{{ hostvars[groups[server_group][0]].token }}"

- name: Delete any existing token from the environment if different from the new one
ansible.builtin.lineinfile:
state: absent
Expand Down
Loading