diff --git a/ansible/upgrade.yaml b/ansible/upgrade.yaml index 78f08f1..c009c45 100644 --- a/ansible/upgrade.yaml +++ b/ansible/upgrade.yaml @@ -6,14 +6,14 @@ gather_facts: yes tasks: - - name: Get list of k0s workers hosts + - name: get hosts local_action: module: shell _raw_params: "kubectl --kubeconfig $KUBECONFIG get nodes" become: false register: k0s_workers - - name: Descriptions + - name: verbose ansible.builtin.debug: msg: - "The following playbook allows you to upgrade the k0s version on nodes safely and one at a time" @@ -21,13 +21,13 @@ - "If you are upgrading a k0s controller node check inventory for the desired hostname." - "The k0s worker nodes in your current cluster are:" - - name: List nodes + - name: nodes ansible.builtin.debug: msg: - "{{ item }}" with_items: "{{ k0s_workers.stdout_lines }}" - - name: Prompt for target_host + - name: target_host pause: prompt: "Specify which node to upgrade:" echo: yes @@ -41,21 +41,32 @@ target_host: "{{ target_host.user_input }}" # End of inventory on the fly block + - ansible.builtin.debug: + msg: + - "{{ target_host }}" - - name: Drain/cordon node + - ansible.builtin.debug: + msg: "{{ groups }}" + + - ansible.builtin.fail: + msg: "Failing play as specified host does not exist in inventory. + Check that you are specifying a valid node and rerun" + when: "'{{ target_host }}' not in groups.k0s" + + - name: drain node local_action: module: shell - _raw_params: "kubectl --kubeconfig $KUBECONFIG drain {{ target_host }} --force --ignore-daemonsets --delete-emptydir-data" + _raw_params: "kubectl --kubeconfig $KUBECONFIG drain {{ target_host }} --disable-eviction=true --force --ignore-daemonsets --delete-emptydir-data" become: false register: node_drain_out - - name: Set k8s_version_upgrade_eviction_seconds fact + - name: set k8s_version_upgrade_eviction_seconds fact set_fact: k8s_version_upgrade_eviction_seconds: 45 run_once: true when: k8s_version_upgrade_eviction_seconds is undefined - - name: Allow some time for the evictions + - name: allow some time for the evictions pause: seconds: "{{ k8s_version_upgrade_eviction_seconds }}" @@ -73,15 +84,30 @@ tags: download - hosts: dynamically_created_hosts - name: Start the k0s service + name: start the k0s service become: yes tasks: - shell: k0s start + - name: "Wait for k8s apiserver" + ansible.builtin.wait_for: + host: localhost + port: 6443 + timeout: 100 + when: inventory_hostname not in groups.worker + + - name: Get k0s status on newly upgraded node + command: k0s status + register: k0s_status + + - ansible.builtin.debug: + msg: "k0s status: {{ k0s_status.stdout_lines }}" + failed_when: k0s_status.rc != 0 + - hosts: localhost gather_facts: yes tasks: - - name: Uncordon node + - name: uncordon node local_action: module: shell _raw_params: "kubectl --kubeconfig $KUBECONFIG uncordon {{ target_host }}" diff --git a/taskfiles/hosts.yml b/taskfiles/hosts.yml index b2191ef..6871f6a 100644 --- a/taskfiles/hosts.yml +++ b/taskfiles/hosts.yml @@ -19,10 +19,11 @@ tasks: cmds: - ansible-playbook -vv launchpad-core/ansible/install_k0s_cluster.yaml --diff -i {{.INVENTORY_PATH}} {{.CLI_ARGS}} - upgrade-k0s-node: + k0s-node-upgrade: interactive: true # for interactive scripts - desc: Playbook to upgrade version of k0s on one cluster node at a time + desc: Safely upgrade the version of a k0s node in a cluster cmds: - ansible-playbook -vv launchpad-core/ansible/upgrade.yaml --diff -i {{.INVENTORY_PATH}} {{.CLI_ARGS}} # k0s-sysinfo: # TODO add task for running k0s sysinfo on a host/hosts +