Skip to content

Commit f2aed3b

Browse files
authored
Automatically inject tls-san when api_endpoint differs from hostname (#434)
* Auto-add --tls-san={{ api_endpoint }} when it differs from ansible_hostname * Ensures first server generates certificate with all required SANs * Add .ansible/ and PR_DESCRIPTION.md to gitignore Signed-off-by: Guillaume Andre <[email protected]>
1 parent cb640b8 commit f2aed3b

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ venv
33
.vscode
44
.vagrant
55
inventory.yml
6-
playbook/debug.yml
6+
playbook/debug.yml
7+
.ansible/
8+
PR_DESCRIPTION.md

roles/k3s_agent/tasks/main.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@
4141
}) }}
4242
changed_when: true
4343

44+
- name: Compute final agent arguments
45+
ansible.builtin.set_fact:
46+
_api_endpoint_in_agent_config: >-
47+
{% if agent_config_yaml is defined and api_endpoint is defined and agent_config_yaml | regex_search('tls-san:.*' + api_endpoint | regex_escape(), ignorecase=True) %}
48+
true
49+
{% else %}
50+
false
51+
{% endif %}
52+
_api_endpoint_in_agent_args: >-
53+
{% if api_endpoint is defined and extra_agent_args | regex_search('--tls-san[=\s]+' + api_endpoint | regex_escape(), ignorecase=True) %}
54+
true
55+
{% else %}
56+
false
57+
{% endif %}
58+
59+
- name: Add TLS SAN to agent arguments if needed
60+
ansible.builtin.set_fact:
61+
opt_tls_san: >-
62+
{% if api_endpoint is defined and api_endpoint != ansible_hostname and _api_endpoint_in_agent_config | bool == false and _api_endpoint_in_agent_args | bool == false %}
63+
--tls-san={{ api_endpoint }}
64+
{% endif %}
65+
4466
- name: Setup optional config file
4567
when: agent_config_yaml is defined
4668
block:

roles/k3s_agent/templates/k3s-agent.service.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ RestartSec=5s
2626
ExecStartPre=/bin/sh -xc '! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service'
2727
ExecStartPre=-/sbin/modprobe br_netfilter
2828
ExecStartPre=-/sbin/modprobe overlay
29-
ExecStart=/usr/local/bin/k3s agent --data-dir {{ k3s_server_location }} --server https://{{ api_endpoint }}:{{ api_port }} {{ extra_agent_args }}
29+
ExecStart=/usr/local/bin/k3s agent --data-dir {{ k3s_server_location }} --server https://{{ api_endpoint }}:{{ api_port }} {{ opt_tls_san }} {{ extra_agent_args }}

roles/k3s_server/tasks/main.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@
4646
regexp: '\.\s+<\(k3s completion bash\)'
4747
line: ". <(k3s completion bash) # Added by k3s-ansible"
4848

49+
- name: Compute final server arguments
50+
ansible.builtin.set_fact:
51+
_api_endpoint_in_config: >-
52+
{% if server_config_yaml is defined and api_endpoint is defined and server_config_yaml | regex_search('tls-san:.*' + api_endpoint | regex_escape(), ignorecase=True) %}
53+
true
54+
{% else %}
55+
false
56+
{% endif %}
57+
_api_endpoint_in_args: >-
58+
{% if api_endpoint is defined and extra_server_args | regex_search('--tls-san[=\s]+' + api_endpoint | regex_escape(), ignorecase=True) %}
59+
true
60+
{% else %}
61+
false
62+
{% endif %}
63+
64+
- name: Add TLS SAN to server arguments if needed
65+
ansible.builtin.set_fact:
66+
final_server_args: >-
67+
{{ extra_server_args }}
68+
{% if api_endpoint is defined and api_endpoint != ansible_hostname and _api_endpoint_in_config | bool == false and _api_endpoint_in_args | bool == false %}
69+
--tls-san={{ api_endpoint }}
70+
{% endif %}
71+
4972
- name: Setup optional config file
5073
when: server_config_yaml is defined
5174
block:

roles/k3s_server/templates/k3s-cluster-init.service.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ Restart=always
2525
RestartSec=5s
2626
ExecStartPre=-/sbin/modprobe br_netfilter
2727
ExecStartPre=-/sbin/modprobe overlay
28-
ExecStart=/usr/local/bin/k3s server --cluster-init --data-dir {{ k3s_server_location }} {{ extra_server_args }}
28+
ExecStart=/usr/local/bin/k3s server --cluster-init --data-dir {{ k3s_server_location }} {{ final_server_args }}

roles/k3s_server/templates/k3s-ha.service.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ Restart=always
2525
RestartSec=5s
2626
ExecStartPre=-/sbin/modprobe br_netfilter
2727
ExecStartPre=-/sbin/modprobe overlay
28-
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} --server https://{{ api_endpoint }}:{{ api_port }} {{ extra_server_args }}
28+
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} --server https://{{ api_endpoint }}:{{ api_port }} {{ final_server_args }}

roles/k3s_server/templates/k3s-single.service.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ Restart=always
2525
RestartSec=5s
2626
ExecStartPre=-/sbin/modprobe br_netfilter
2727
ExecStartPre=-/sbin/modprobe overlay
28-
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} {{ extra_server_args }}
28+
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} {{ final_server_args }}

0 commit comments

Comments
 (0)