ansible-role-k3s/tasks/build/preconfigure-k3s.yml
Xan Manning e7c714424c
Tiidy up and refactoring of tasks (#80)
* Tidy up and refactoring of tasks

  - `k3s_config_dir` derived from `k3s_config_file`, reused throughout the role
    to allow for easy removal of "Rancher" references #73.
  - `k3s_token_location` has moved to be in `k3s_config_dir`.
  - Tasks for creating directories now looped to caputure configuration from
    `k3s_server` and `k3s_agent` and ensure directories exist before k3s
    starts, see #75.
  - Server token collected directly from token file, not symlinked file
    (node-token).
  - `k3s_runtime_config` defined in `vars/` for validation and overwritten in
    tasks for control plane and workers.
  - Removed unused references to GitHub API.

* set_fact now uses FQCN

* re-pin molecule<3.2

* Command module now uses FQCN

* Added package checks for #72

* Reorder task files

  - Docker tasks moved into a separate directory for ease of removal #67
  - Bugfix: Control plane on alternate port didn't work.
  - Validation tasks grouped

* Fix Fedora tests

* Add optional documentation links to validations steps #76

* Removed jmespath requirement

* Fix issue with data collection

* Release candidate
2020-12-21 19:14:52 +00:00

108 lines
4.2 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_all | length < 2
and k3s_control_node_address is not defined
- name: Ensure k3s control node fact is set
ansible.builtin.set_fact:
k3s_control_node: "{{ 'false' if k3s_build_cluster else 'true' }}"
when: k3s_control_node is not defined
- name: Ensure k3s primary control node fact is set
ansible.builtin.set_fact:
k3s_primary_control_node: "{{ 'false' if k3s_build_cluster else 'true' }}"
when: k3s_primary_control_node is not defined
- name: Ensure k3s control plane port is captures
ansible.builtin.set_fact:
k3s_control_plane_port: "{{ k3s_runtime_config['https-listen-port'] | default(6443) }}"
delegate_to: k3s_primary_control_node
- 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
and hostvars[item].k3s_control_node
loop: "{{ ansible_play_hosts_all }}"
- name: Ensure a k3s control node is defined if none are found in ansible_play_hosts_all
block:
- name: Set the control host
ansible.builtin.set_fact:
k3s_control_node: true
when: inventory_hostname == ansible_play_hosts_all[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
and hostvars[item].k3s_control_node
loop: "{{ ansible_play_hosts_all }}"
when: k3s_controller_list | length < 1
and k3s_build_cluster is defined and k3s_build_cluster
- name: Ensure a primary k3s control node is defined if multiple are found in ansible_play_hosts_all
ansible.builtin.set_fact:
k3s_primary_control_node: true
when: k3s_controller_list is defined
and k3s_controller_list | length > 1
and inventory_hostname == k3s_controller_list[0]
and k3s_build_cluster is defined and k3s_build_cluster
- name: Ensure ansible_host is mapped to inventory_hostname
ansible.builtin.lineinfile:
path: /tmp/inventory.txt
line: >-
{{ item }}
@@@
{{ hostvars[item].ansible_host | default(hostvars[item].ansible_fqdn) }}
@@@
C_{{ hostvars[item].k3s_control_node }}
@@@
P_{{ hostvars[item].k3s_primary_control_node | default(False) }}
create: true
regexp: "^{{ item }} @@@ {{ hostvars[item].ansible_host | default(hostvars[item].ansible_fqdn) }}"
mode: 0600
loop: "{{ ansible_play_hosts_all }}"
check_mode: false
when: hostvars[item].k3s_control_node is defined
- name: Delegate a control plane node
block:
- name: Lookup control node from file
ansible.builtin.command: "grep '{{ '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 to for obtaining a 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 control node address is registered in Ansible
ansible.builtin.set_fact:
k3s_control_node_address: "{{ hostvars[k3s_control_delegate].ansible_host | default(hostvars[k3s_control_delegate].ansible_fqdn) }}"
check_mode: false
when: k3s_control_node_address is not defined
when: k3s_control_node_address is not defined
or k3s_control_delegate is not defined
- name: Ensure k3s_runtime_config is set for control plane
ansible.builtin.set_fact:
k3s_runtime_config: "{{ (k3s_server | default({})) | combine((k3s_agent | default({}))) }}"
when: (k3s_server is defined or k3s_agent is defined)
and (k3s_control_node is defined and k3s_control_node)
- name: Ensure k3s_runtime_config is set for agents
ansible.builtin.set_fact:
k3s_runtime_config: "{{ (k3s_agent | default({})) }}"
when: k3s_agent is defined
and (k3s_control_node is not defined or not k3s_control_node)