fix(ansible): only run certain tasks on k3s_primary_control_node

Signed-off-by: Devin Buhl <devin@buhl.casa>
This commit is contained in:
Devin Buhl committed 2023-06-17 07:48:59 -04:00
1 parent de7a73f0ec
commit 746f49e028
1 file changed
+30 -36
+30 -36
View File
@@ -11,16 +11,16 @@
seconds: 5 seconds: 5
tasks: tasks:
- name: Check if cluster is installed - name: Check if cluster is installed
check_mode: false
ansible.builtin.stat: ansible.builtin.stat:
path: /etc/rancher/k3s/config.yaml path: /etc/rancher/k3s/config.yaml
register: k3s_check_installed register: k3s_check_installed
check_mode: false
- name: Ignore manifests templates and urls if the cluster is already installed - name: Ignore manifests templates and urls if the cluster is already installed
when: k3s_check_installed.stat.exists
ansible.builtin.set_fact: ansible.builtin.set_fact:
k3s_server_manifests_templates: [] k3s_server_manifests_templates: []
k3s_server_manifests_urls: [] k3s_server_manifests_urls: []
when: k3s_check_installed.stat.exists
- name: Install Kubernetes - name: Install Kubernetes
ansible.builtin.include_role: ansible.builtin.include_role:
@@ -34,39 +34,30 @@
become: false become: false
run_once: true run_once: true
check_mode: false check_mode: false
ansible.builtin.command: |- ansible.builtin.command: git rev-parse --show-toplevel
git rev-parse --show-toplevel
register: repository_path register: repository_path
- name: Copy kubeconfig project directory - name: Copy kubeconfig to the project directory
run_once: true when: k3s_primary_control_node
ansible.builtin.fetch: ansible.builtin.fetch:
src: "/etc/rancher/k3s/k3s.yaml" src: /etc/rancher/k3s/k3s.yaml
dest: "{{ repository_path.stdout }}/kubeconfig" dest: "{{ repository_path.stdout }}/kubeconfig"
flat: true flat: true
when:
- k3s_control_node is defined
- k3s_control_node
- name: Update kubeconfig with the correct IPv4 address - name: Update kubeconfig with the correct load balancer address
delegate_to: localhost delegate_to: localhost
become: false become: false
run_once: true run_once: true
ansible.builtin.replace: ansible.builtin.replace:
path: "{{ repository_path.stdout }}/kubeconfig" path: "{{ repository_path.stdout }}/kubeconfig"
regexp: "https://127.0.0.1:6443" regexp: https://127.0.0.1:6443
replace: "https://{{ k3s_registration_address }}:6443" replace: "https://{{ k3s_registration_address }}:6443"
# Unmanaging and removing the Cilium HelmChart is required for - name: Post installation of custom manifests
# flux to take over managing the lifecycle of Cilium
- name: Post installation of custom manifests tasks
run_once: true
when: when:
- k3s_server_manifests_templates | length > 0 - k3s_primary_control_node
or k3s_server_manifests_urls | length > 0 - (k3s_server_manifests_templates | length > 0
- k3s_control_node is defined or k3s_server_manifests_urls | length > 0)
- k3s_control_node
block: block:
- name: Wait for custom manifests to rollout - name: Wait for custom manifests to rollout
kubernetes.core.k8s_info: kubernetes.core.k8s_info:
@@ -98,6 +89,8 @@
type: Complete type: Complete
status: true status: true
wait_timeout: 360 wait_timeout: 360
# Unmanage and remove the Cilium HelmChart in-order for
# flux to take over managing the lifecycle of Cilium
- name: Patch the Cilium HelmChart to unmanage it - name: Patch the Cilium HelmChart to unmanage it
kubernetes.core.k8s_json_patch: kubernetes.core.k8s_json_patch:
kubeconfig: /etc/rancher/k3s/k3s.yaml kubeconfig: /etc/rancher/k3s/k3s.yaml
@@ -122,7 +115,7 @@
kind: HelmChart kind: HelmChart
namespace: kube-system namespace: kube-system
register: cilium_helmchart register: cilium_helmchart
- name: Remove finializers on the Cilium HelmChart - name: Force delete the Cilium HelmChart
when: cilium_helmchart.resources | count > 0 when: cilium_helmchart.resources | count > 0
kubernetes.core.k8s: kubernetes.core.k8s:
kubeconfig: /etc/rancher/k3s/k3s.yaml kubeconfig: /etc/rancher/k3s/k3s.yaml
@@ -137,17 +130,18 @@
# Cleaning up certain manifests from the /var/lib/rancher/k3s/server/manifests directory # Cleaning up certain manifests from the /var/lib/rancher/k3s/server/manifests directory
# is needed because k3s has an awesome "feature" to always re-deploy them when the k3s # is needed because k3s has an awesome "feature" to always re-deploy them when the k3s
# service is restarted. Removing them does not uninstall the manifests from your cluster. # service is restarted. Removing them does not uninstall the manifests from your cluster.
- name: Delete custom manifests
- name: Get a list of all custom manifest files when: k3s_primary_control_node
ansible.builtin.find: block:
paths: "{{ k3s_server_manifests_dir }}" - name: Get a list of all custom manifest files
file_type: file ansible.builtin.find:
use_regex: true paths: "{{ k3s_server_manifests_dir }}"
patterns: ["^custom-.*"] file_type: file
register: custom_manifest use_regex: true
patterns: ["^custom-.*"]
- name: Delete all custom manifest files register: custom_manifest
ansible.builtin.file: - name: Delete all custom manifest files
path: "{{ item.path }}" ansible.builtin.file:
state: absent path: "{{ item.path }}"
loop: "{{ custom_manifest.files }}" state: absent
loop: "{{ custom_manifest.files }}"