diff --git a/roles/cluster_control_plane_init/tasks/kubeconfig.yaml b/roles/cluster_control_plane_init/tasks/kubeconfig.yaml new file mode 100644 index 0000000..01ad069 --- /dev/null +++ b/roles/cluster_control_plane_init/tasks/kubeconfig.yaml @@ -0,0 +1,30 @@ +--- +- name: Fetch cluster config + become: true + register: slurpfile + ansible.builtin.slurp: + src: /etc/kubernetes/admin.conf + +- name: Ensure .kube directory exists + delegate_to: kvm_host_local + connection: local + ansible.builtin.file: + path: ~/.kube + state: directory + mode: "0700" + +- name: Create local kubernetes config + delegate_to: kvm_host_local + connection: local + ansible.builtin.copy: + content: "{{ slurpfile.content | b64decode }}" + dest: ~/.kube/config + mode: "0700" + +- name: Copy kubeconfig to nodes + block: + + - name: Include task node_kubeconfig + with_items: "{{ nodes }}" + ansible.builtin.include_tasks: + file: node_kubeconfig.yaml diff --git a/roles/cluster_control_plane_init/tasks/main.yaml b/roles/cluster_control_plane_init/tasks/main.yaml index 025e3b4..c8ff3c9 100644 --- a/roles/cluster_control_plane_init/tasks/main.yaml +++ b/roles/cluster_control_plane_init/tasks/main.yaml @@ -18,27 +18,9 @@ --kubernetes-version=v"{{ kubernetes_minor_version }}" --pod-network-cidr=10.244.0.0/16 -- name: Fetch cluster config - become: true - register: slurpfile - ansible.builtin.slurp: - src: /etc/kubernetes/admin.conf - -- name: Ensure .kube directory exists - delegate_to: kvm_host_local - connection: local - ansible.builtin.file: - path: ~/.kube - state: directory - mode: "0700" - -- name: Create local kubernetes config - delegate_to: kvm_host_local - connection: local - ansible.builtin.copy: - content: "{{ slurpfile.content | b64decode }}" - dest: ~/.kube/config - mode: "0700" +- name: Include task kubeconfig + ansible.builtin.include_tasks: + file: kubeconfig.yaml - name: Apply CNI plugin delegate_to: kvm_host_local diff --git a/roles/cluster_control_plane_init/tasks/node_kubeconfig.yaml b/roles/cluster_control_plane_init/tasks/node_kubeconfig.yaml new file mode 100644 index 0000000..b3086c4 --- /dev/null +++ b/roles/cluster_control_plane_init/tasks/node_kubeconfig.yaml @@ -0,0 +1,33 @@ +--- +- name: Ensure .kube directory exists + delegate_to: "{{ item.name }}" + ansible.builtin.file: + path: ~/.kube + state: directory + mode: "0700" + +- name: Create local kubernetes config + delegate_to: "{{ item.name }}" + ansible.builtin.copy: + content: "{{ slurpfile.content | b64decode }}" + dest: ~/.kube/config + mode: "0700" + +- name: Set up kubectl autocompletion + ansible.builtin.lineinfile: + dest: ~/.bashrc + line: "source <(kubectl completion bash)" + state: present + +- name: Set up k alias + ansible.builtin.lineinfile: + dest: ~/.bashrc + line: "alias k=kubectl" + state: present + +- name: Set up k alias autocompletion + ansible.builtin.lineinfile: + dest: ~/.bashrc + insertafter: "alias k=kubectl" + line: "complete -o default -F __start_kubectl k" + state: present diff --git a/roles/kvm_host_set_up/tasks/main.yaml b/roles/kvm_host_set_up/tasks/main.yaml index feb72d5..c1d9216 100644 --- a/roles/kvm_host_set_up/tasks/main.yaml +++ b/roles/kvm_host_set_up/tasks/main.yaml @@ -91,5 +91,17 @@ ansible.builtin.lineinfile: path: ~/.bashrc state: present - regexp: "^source <(kubectl completion bash)" - line: source <(kubectl completion bash) + line: "source <(kubectl completion bash)" + +- name: Set up k alias + ansible.builtin.lineinfile: + dest: ~/.bashrc + line: "alias k=kubectl" + state: present + +- name: Set up k alias autocompletion + ansible.builtin.lineinfile: + dest: ~/.bashrc + insertafter: "alias k=kubectl" + line: "complete -o default -F __start_kubectl k" + state: present