Added custom context name (#315)

* Added custom context name

Signed-off-by: Vivek Sarin <vivek@sarin.info>
Signed-off-by: Derek Nola <derek.nola@suse.com>
Co-authored-by: Vivek Sarin <vivek@sarin.info>
Co-authored-by: Derek Nola <derek.nola@suse.com>
This commit is contained in:
Vivek Sarin 2024-04-01 12:35:55 -04:00 committed by GitHub
parent 6c14e5d923
commit c84c1ce5b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 5 additions and 3 deletions

View File

@ -20,6 +20,7 @@ k3s_cluster:
extra_agent_args: ""
# Optional vars
# cluster_context: k3s-ansible
# api_port: 6443
# k3s_server_location: /var/lib/rancher/k3s
# systemd_dir: /etc/systemd/system

View File

@ -4,3 +4,4 @@ systemd_dir: "/etc/systemd/system"
api_port: 6443
kubeconfig: ~/.kube/config.new
user_kubectl: true
cluster_context: k3s-ansible

View File

@ -120,12 +120,12 @@
changed_when:
- csa_result.rc == 0
- name: Setup kubeconfig k3s-ansible context on control node
- name: Setup kubeconfig context on control node - {{ cluster_context }}
when: kubeconfig != "~/.kube/config"
ansible.builtin.replace:
path: "{{ kubeconfig }}"
regexp: 'name: default'
replace: 'name: k3s-ansible'
replace: 'name: {{ cluster_context }}'
delegate_to: 127.0.0.1
become: false
@ -133,7 +133,7 @@
when: kubeconfig != "~/.kube/config"
ansible.builtin.shell: |
TFILE=$(mktemp)
KUBECONFIG={{ kubeconfig }} kubectl config set-context k3s-ansible --user=k3s-ansible --cluster=k3s-ansible
KUBECONFIG={{ kubeconfig }} kubectl config set-context {{ cluster_context }} --user={{ cluster_context }} --cluster={{ cluster_context }}
KUBECONFIG={{ kubeconfig }} kubectl config view --flatten > ${TFILE}
mv ${TFILE} {{ kubeconfig }}
delegate_to: 127.0.0.1