ansible-role-k3s/tasks/ensure_pre_configuration.yml
Daniel Brennand de1bd094e5
Fix(tests): Resolve Ansible Lint warnings and fix Molecule tests on GitHub Actions (#202)
* fix(ansible-lint): FQDN and `name`

* fix(ansible-lint): add `name` and FQDN for module call

* fix(ansible-lint): add `name` to tasks and FQDN for module

* fix(ansible-lint): add task `name` and FQDN for module calls

* fix(ansible-lint): last `include_tasks`

* fix(ansible-lint): add task names and FQDN

* refactor: `Ensure` to `Run`

* [skip ci]refactor: add exist and seperate ensure installed node task, mention build cluster

* [skip ci]refactor: Pipe seperator

* [skip ci]refactor: run

* refactor: remove quotes as other files don't use them

For templated vars in task name

* [skip ci]refactor: task names, use `Run`

* [skip ci]refactor: use variable name in task name

* [skip ci]refactor: task names

* [skip ci]refactor: add service mgr in task name

* [skip ci]refactor: add task names and module FQDNs

* [skip ci]refactor: fix task name

* [skip ci]refactor: add -

* [skip ci]refactor: include task names and FQDNs

* [skip ci]refactor: add task names and FQDNs

* [skip ci]: ignore `name[template]`

* refactor: `when` clause for `block` should be before `block`

* fix: https://github.com/ansible-community/molecule/issues/3883

* refactor: molecule lint command was removed in version `5.0.0`

Use separate CI job step to run linting instead.

* [skip ci]refactor: noqa for command tasks

Subject to change

* refactor: use Ubuntu 22.04

Suspect issues with Molecule tests are related to cgroups v2.
2023-05-13 09:49:39 -04:00

126 lines
4.5 KiB
YAML

---
- name: Ensure k3s_build_cluster is false if running against a single node.
ansible.builtin.set_fact:
k3s_build_cluster: false
when:
- ansible_play_hosts | length < 2
- k3s_registration_address is not defined
- name: Ensure k3s control node fact is set
ansible.builtin.set_fact:
k3s_control_node: "{{ not k3s_build_cluster }}"
when: k3s_control_node is not defined
- name: Ensure k3s primary control node fact is set
ansible.builtin.set_fact:
k3s_primary_control_node: "{{ not k3s_build_cluster }}"
when: k3s_primary_control_node is not defined
- name: Ensure k3s control plane port is captured
ansible.builtin.set_fact:
k3s_control_plane_port: "{{ k3s_runtime_config['https-listen-port'] | default(6443) }}"
delegate_to: k3s_primary_control_node
- name: Ensure k3s node IP is configured when node-ip is defined
ansible.builtin.set_fact:
k3s_node_ip: "{{ k3s_runtime_config['node-ip'] }}"
when:
- k3s_runtime_config['node-ip'] is defined
- name: Ensure a count of control nodes is generated from ansible_play_hosts
ansible.builtin.set_fact:
k3s_controller_list: "{{ k3s_controller_list + [item] }}"
when:
- hostvars[item].k3s_control_node is defined
- hostvars[item].k3s_control_node
loop: "{{ ansible_play_hosts }}"
- name: Ensure a k3s control node is defined if none are found in ansible_play_hosts
when:
- k3s_controller_list | length < 1
- k3s_build_cluster is defined
- k3s_build_cluster
block:
- name: Set the control host
ansible.builtin.set_fact:
k3s_control_node: true
when: inventory_hostname == ansible_play_hosts[0]
- name: Ensure a count of control nodes is generated
ansible.builtin.set_fact:
k3s_controller_list: "{{ k3s_controller_list + [item] }}"
when:
- hostvars[item].k3s_control_node is defined
- hostvars[item].k3s_control_node
loop: "{{ ansible_play_hosts }}"
- name: Ensure a primary k3s control node is defined if multiple are found in ansible_play_hosts
ansible.builtin.set_fact:
k3s_primary_control_node: true
when:
- k3s_controller_list is defined
- inventory_hostname == k3s_controller_list[0]
- k3s_build_cluster is defined
- k3s_build_cluster
- name: Ensure ansible_host is mapped to inventory_hostname
ansible.builtin.blockinfile:
path: /tmp/inventory.txt
block: |
{% for host in ansible_play_hosts %}
{% filter replace('\n', ' ') %}
{{ host }}
@@@
{{ hostvars[host].ansible_host | default(hostvars[host].ansible_fqdn) | string }}
@@@
C_{{ hostvars[host].k3s_control_node | string }}
@@@
P_{{ hostvars[host].k3s_primary_control_node | default(False) | string }}
{% endfilter %}
@@@ END:{{ host }}
{% endfor %}
create: true
mode: 0600
check_mode: false
when: k3s_control_node is defined
- name: Delegate an initializing control plane node
when: k3s_registration_address is not defined
or k3s_control_delegate is not defined
block:
- name: Lookup control node from file
ansible.builtin.command:
cmd: "grep -i '{{ 'P_True' if (k3s_controller_list | length > 1) else 'C_True' }}' /tmp/inventory.txt"
changed_when: false
check_mode: false
register: k3s_control_delegate_raw
- name: Ensure control node is delegated for obtaining a cluster token
ansible.builtin.set_fact:
k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split(' @@@ ')[0] }}"
check_mode: false
when: k3s_control_delegate is not defined
- name: Ensure the node registration address is defined from k3s_control_node_address
ansible.builtin.set_fact:
k3s_registration_address: "{{ k3s_control_node_address }}"
check_mode: false
when: k3s_control_node_address is defined
- name: Ensure the node registration address is defined from node-ip
ansible.builtin.set_fact:
k3s_registration_address: "{{ hostvars[k3s_control_delegate].k3s_node_ip }}"
check_mode: false
when:
- k3s_registration_address is not defined
- k3s_control_node_address is not defined
- hostvars[k3s_control_delegate].k3s_node_ip is defined
- name: Ensure the node registration address is defined
ansible.builtin.set_fact:
k3s_registration_address: "{{ hostvars[k3s_control_delegate].ansible_host | default(hostvars[k3s_control_delegate].ansible_fqdn) }}"
check_mode: false
when:
- k3s_registration_address is not defined
- k3s_control_node_address is not defined