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

fix control node tasks logic to properly change server address on control node #6

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Changes from all 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
80 changes: 45 additions & 35 deletions roles/k3s_server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,11 @@
ansible.builtin.pause:
seconds: 10

- name: Change server to API endpoint instead of localhost
ansible.builtin.command: >-
/usr/local/bin/k3s kubectl config set-cluster default
--server=https://{{ api_endpoint }}:{{ api_port }}
--kubeconfig ~{{ ansible_user }}/.kube/config
changed_when: true

- name: Copy kubectl config to local machine
ansible.builtin.fetch:
src: /etc/rancher/k3s/k3s.yaml
dest: "{{ kubeconfig }}"
flat: true
- name: Add K3s autocomplete to user bashrc
ansible.builtin.lineinfile:
path: "~{{ ansible_user }}/.bashrc"
regexp: '\.\s+<\(k3s completion bash\)'
line: ". <(k3s completion bash) # Added by k3s-ansible"

- name: Check whether kubectl is installed on control node
ansible.builtin.command: 'kubectl'
Expand All @@ -100,29 +93,46 @@
become: false
changed_when: false

- name: Setup kubeconfig k3s-ansible context
when: kubeconfig == "~/.kube/config.new" and kubectl_installed.rc == 0
ansible.builtin.replace:
path: "{{ kubeconfig }}"
regexp: 'name: default'
replace: 'name: k3s-ansible'
delegate_to: 127.0.0.1
become: false

- name: Merge with any existing kube config
when: kubeconfig == "~/.kube/config.new" and kubectl_installed.rc == 0
ansible.builtin.shell: |
TFILE=$(mktemp)
KUBECONFIG=~/.kube/config.new kubectl rename-context default k3s-ansible
KUBECONFIG=~/.kube/config.new kubectl config set-context k3s-ansible --user=k3s-ansible --cluster=k3s-ansible
KUBECONFIG=~/.kube/config.new:~/.kube/config kubectl config view --flatten > ${TFILE}
mv ${TFILE} ~/.kube/config
rm ~/.kube/config.new
delegate_to: 127.0.0.1
become: false
register: mv_result
changed_when:
- mv_result.rc == 0
- name: Apply K3S kubeconfig to control node
when: kubectl_installed.rc == 0
block:
- name: Copy kubeconfig to control node
ansible.builtin.fetch:
src: /etc/rancher/k3s/k3s.yaml
dest: "{{ kubeconfig }}"
flat: true

- name: Change server address in kubeconfig on control node
ansible.builtin.shell: |
KUBECONFIG={{ kubeconfig }} kubectl config set-cluster default --server=https://{{ api_endpoint }}:{{ api_port }}

Check warning on line 107 in roles/k3s_server/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

107:121 [line-length] line too long (125 > 120 characters)
delegate_to: 127.0.0.1
become: false
register: csa_result
changed_when:
- csa_result.rc == 0

- name: Setup kubeconfig k3s-ansible context on control node
when: kubeconfig != "~/.kube/config"
ansible.builtin.replace:
path: "{{ kubeconfig }}"
regexp: 'name: default'
replace: 'name: k3s-ansible'
delegate_to: 127.0.0.1
become: false

- name: Merge with any existing kubeconfig on control node
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 }}:~/.kube/config kubectl config view --flatten > ${TFILE}
mv ${TFILE} ~/.kube/config
rm {{ kubeconfig }}
delegate_to: 127.0.0.1
become: false
register: mv_result
changed_when:
- mv_result.rc == 0

- name: Start other server if any and verify status
when:
Expand Down
Loading