From 7f406b8d395c939b7883d80b2845325702c082bd Mon Sep 17 00:00:00 2001 From: Devin Buhl Date: Thu, 30 Sep 2021 14:52:00 -0400 Subject: [PATCH] feat: move kubeconfig to provision folder during ansible k3s-install (#91) * feat: move kubeconfig to provision folder during ansible k3s-install Signed-off-by: Devin Buhl * fix: update kubeconfig refereces Signed-off-by: Devin Buhl --- .envrc | 2 +- README.md | 6 ++---- Taskfile.yml | 2 +- provision/ansible/playbooks/k3s-install.yml | 13 ++++++++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.envrc b/.envrc index e57ccc2f..0b7a8512 100644 --- a/.envrc +++ b/.envrc @@ -1,4 +1,4 @@ #shellcheck disable=SC2148,SC2155 -export KUBECONFIG=$(expand_path ./kubeconfig) +export KUBECONFIG=$(expand_path ./provision/kubeconfig) export ANSIBLE_CONFIG=$(expand_path ./ansible.cfg) export GPG_TTY=$(tty) diff --git a/README.md b/README.md index 4eae6c40..c9a31c70 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ In order to use Terraform and `cert-manager` with the Cloudflare DNS challenge y ### :sailboat:  Installing k3s with Ansible -:round_pushpin: Here we will be running a Ansible Playbook to install [k3s](https://k3s.io/) with [this](https://galaxy.ansible.com/xanmanning/k3s) wonderful k3s Ansible galaxy role. After completion, Ansible will drop a `kubeconfig` in `/tmp/kubeconfig` for use with interacting with your cluster with `kubectl`. This file should be manually copied to the root of your repository. +:round_pushpin: Here we will be running a Ansible Playbook to install [k3s](https://k3s.io/) with [this](https://galaxy.ansible.com/xanmanning/k3s) wonderful k3s Ansible galaxy role. After completion, Ansible will drop a `kubeconfig` in `./provision/kubeconfig` for use with interacting with your cluster with `kubectl`. 1. Verify Ansible can view your config by running `task ansible:list` @@ -217,9 +217,7 @@ In order to use Terraform and `cert-manager` with the Cloudflare DNS challenge y 4. If everything goes as planned you should see Ansible running the k3s install Playbook against your nodes. -5. Copy the `kubeconfig` file from `/tmp` to your repository. - -6. Verify the nodes are online +5. Verify the nodes are online ```sh kubectl --kubeconfig=./provision/kubeconfig get nodes diff --git a/Taskfile.yml b/Taskfile.yml index 54f866cc..cdfcdd52 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -9,7 +9,7 @@ vars: TERRAFORM_DIR: "{{.PROJECT_DIR}}/provision/terraform" env: - KUBECONFIG: "{{.PROJECT_DIR}}/kubeconfig" + KUBECONFIG: "{{.PROJECT_DIR}}/provision/kubeconfig" includes: ansible: .taskfiles/ansible.yml diff --git a/provision/ansible/playbooks/k3s-install.yml b/provision/ansible/playbooks/k3s-install.yml index e407c97c..7d2b71d3 100644 --- a/provision/ansible/playbooks/k3s-install.yml +++ b/provision/ansible/playbooks/k3s-install.yml @@ -27,11 +27,18 @@ name: xanmanning.k3s public: true - - name: Copy kubeconfig locally to /tmp + - name: Get absolute path to this Git repository + delegate_to: localhost + become: false + run_once: true + ansible.builtin.command: "git rev-parse --show-toplevel" + register: repo_abs_path + + - name: Copy kubeconfig to provision folder run_once: true ansible.builtin.fetch: src: "/etc/rancher/k3s/k3s.yaml" - dest: "/tmp/kubeconfig" + dest: "{{ repo_abs_path.stdout }}/provision/kubeconfig" flat: true when: - k3s_control_node is defined @@ -42,7 +49,7 @@ become: false run_once: true ansible.builtin.replace: - path: "/tmp/kubeconfig" + path: "{{ repo_abs_path.stdout }}/provision/kubeconfig" regexp: "https://127.0.0.1:6443" replace: "https://{{ k3s_registration_address }}:6443"