PyratLabs / ansible-role-k3s

Ansible role for installing k3s as either a standalone server or HA cluster.
BSD 3-Clause "New" or "Revised" License
640 stars 135 forks source link

Role is not re-entrant for HA mode #216

Closed weakcamel closed 3 weeks ago

weakcamel commented 1 year ago

Summary

This role can be run in several modes, including validation (installed, started, stopped, restarted, uninstalled, validated)

If you want to run it first in 'installed' and then 'validated' mode in single playbook, there is a problem with k3s_controller_list variable: on second run it contains duplicate entries and consequently validation fails. That's because Ansible - sadly - doesn't scope the variables properly and they're left behind as-is after a role finishes.

It's not a very common use case, however it would be nice if the role was re-entrant. It's also very easy to achieve - e.g. by setting the k3s_controller_list to an empty list

- name: Reset the value of k3s_controller_list
  ansible.builtin.set_fact:
    k3s_controller_list: []

just before it's being appended to in a loop:

https://github.com/PyratLabs/ansible-role-k3s/blob/37fda0a953e83ae802b0997f881a7c8533b77e92/tasks/ensure_pre_configuration.yml#L31

Issue Type

Controller Environment and Configuration

Role version:

v3.4.2

Steps to Reproduce

---
- name: Set up K3s multinode cluster
  hosts: k3s
  become: true

  vars:
    ansible_become: true
  tasks:
    - name: Include K3s role
      ansible.builtin.import_role:
        name: xanmanning.k3s
      tags:
        - k3s

    - name: Switch k3s role to validation mode
      ansible.builtin.set_fact:
        k3s_state: validated
      tags:
        - k3s

    - name: Validate the k3s installation
      ansible.builtin.import_role:
        name: xanmanning.k3s
      tags:
        - k3s

Expected Result

Actual Result

TASK [xanmanning.k3s : Check the conditions when embedded etcd is defined] **********************************************************************************************************
skipping: [k3s-agent-1.lab.example.com]
skipping: [k3s-agent-2.lab.example.com]
skipping: [k3s-agent-3.lab.example.com]
fatal: [k3s-server-1.lab.example.com]: FAILED! => changed=false
  assertion: (((k3s_controller_list | length) % 2) == 1)
  evaluated_to: false
  msg: Etcd should have a minimum of 3 defined members and the number of members should be odd. Please see notes about HA in README.md
fatal: [k3s-server-2.lab.example.com]: FAILED! => changed=false
  assertion: (((k3s_controller_list | length) % 2) == 1)
  evaluated_to: false
  msg: Etcd should have a minimum of 3 defined members and the number of members should be odd. Please see notes about HA in README.md
fatal: [k3s-server-3.lab.example.com]: FAILED! => changed=false
  assertion: (((k3s_controller_list | length) % 2) == 1)
  evaluated_to: false
  msg: Etcd should have a minimum of 3 defined members and the number of members should be odd. Please see notes about HA in README.md

After adding an extra debug task to the role just before this one, I discovered:


ok: [k3s-agent-2.lab.example.com] =>
  k3s_controller_list:
  - k3s-server-1.lab.example.com
  - k3s-server-2.lab.example.com
  - k3s-server-3.lab.example.com
  - k3s-server-1.lab.example.com
  - k3s-server-2.lab.example.com
  - k3s-server-3.lab.example.com
  - ```
stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

weakcamel commented 10 months ago

Still a problem

stale[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.