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
This commit is contained in:
Xan Manning 2020-12-21 19:14:52 +00:00 committed by GitHub
parent ef6c579336
commit e7c714424c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
61 changed files with 330 additions and 170 deletions

View File

@ -14,6 +14,37 @@
---
-->
## 2020-12-21, v2.4.0
### Notable changes
- `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` and `command` tasks now use FQCN.
- Check of `ansible_version` in environment check.
- Introduction of target environment checks for #72.
- Fixed bug with non-default listening port not being passed to workers.
- Added ability to put documentation links into validation checks #76.
- Removed the requirement for `jmespath` on the Ansible controller.
- Fixed bug with issue data collection tasks.
### Breaking changes
- Ansible minimum version is hard set to v2.10.4
- `k3s_token_location` has moved to be in `k3s_config_dir` so re-running the
role will create a duplicate file here.
---
## 2020-12-19, v2.3.0
### Notable changes

View File

@ -14,8 +14,7 @@ and [CHANGELOG.md](CHANGELOG.md).
The host you're running Ansible from requires the following Python dependencies:
- `ansible >= 2.10`
- `jmespath >= 0.10.0`
- `ansible >= 2.10.4`
You can install dependencies using the requirements.txt file in this repository:
`pip3 install -r requirements.txt`.
@ -70,7 +69,6 @@ consistency. These are generally cluster-level configuration.
| `k3s_build_cluster` | When multiple play hosts are available, attempt to cluster. Read notes below. | `true` |
| `k3s_control_node_address` | Use a specific control node address. IP or FQDN. | NULL |
| `k3s_github_url` | Set the GitHub URL to install k3s from. | https://github.com/k3s-io/k3s |
| `k3s_skip_validation` | Skip all tasks that validate configuration. | `false` |
| `k3s_install_dir` | Installation directory for k3s. | `/usr/local/bin` |
| `k3s_install_hard_links` | Install using hard links rather than symbolic links. | `false` |
| `k3s_server_manifests_templates` | A list of Auto-Deploying Manifests Templates. | [] |
@ -154,17 +152,18 @@ configuration.
The below variables are used to change the way the role executes in Ansible,
particularly with regards to privilege escalation.
| Variable | Description | Default Value |
|----------------------------------|---------------------------------------------------------------------|---------------|
| `k3s_become_for_all` | Escalate user privileges for all tasks. Overrides all of the below. | `false` |
| `k3s_become_for_systemd` | Escalate user privileges for systemd tasks. | NULL |
| `k3s_become_for_install_dir` | Escalate user privileges for creating installation directories. | NULL |
| `k3s_become_for_manifests_dir` | Escalate user privileges for creating manifests directory. | NULL |
| `k3s_become_for_usr_local_bin` | Escalate user privileges for writing to `/usr/local/bin`. | NULL |
| `k3s_become_for_data_dir` | Escalate user privileges for creating data directory. | NULL |
| `k3s_become_for_package_install` | Escalate user privileges for installing k3s. | NULL |
| `k3s_become_for_kubectl` | Escalate user privileges for running `kubectl`. | NULL |
| `k3s_become_for_uninstall` | Escalate user privileges for uninstalling k3s. | NULL |
| Variable | Description | Default Value |
|-------------------------------------|---------------------------------------------------------------------|---------------|
| `k3s_skip_validation` | Skip all tasks that validate configuration. | `false` |
| `k3s_skip_env_checks` | Skill all tasks that check environment configuration. | `false` |
| `k3s_become_for_all` | Escalate user privileges for all tasks. Overrides all of the below. | `false` |
| `k3s_become_for_systemd` | Escalate user privileges for systemd tasks. | NULL |
| `k3s_become_for_install_dir` | Escalate user privileges for creating installation directories. | NULL |
| `k3s_become_for_directory_creation` | Escalate user privileges for creating application directories. | NULL |
| `k3s_become_for_usr_local_bin` | Escalate user privileges for writing to `/usr/local/bin`. | NULL |
| `k3s_become_for_package_install` | Escalate user privileges for installing k3s. | NULL |
| `k3s_become_for_kubectl` | Escalate user privileges for running `kubectl`. | NULL |
| `k3s_become_for_uninstall` | Escalate user privileges for uninstalling k3s. | NULL |
#### Important note about `k3s_release_version`
@ -303,7 +302,7 @@ stable release:
datastore-endpoint: "postgres://postgres:verybadpass@database:5432/postgres?sslmode=disable"
pre_tasks:
- name: Set each node to be a control node
set_fact:
ansible.builtin.set_fact:
k3s_control_node: true
when: inventory_hostname in ['node2', 'node3']
roles:

View File

@ -26,6 +26,9 @@ k3s_github_url: https://github.com/k3s-io/k3s
# Skip all tasks that validate configuration
k3s_skip_validation: false
# Skip all tasks that check environment configuration
k3s_skip_env_checks: false
# Installation directory for k3s
k3s_install_dir: /usr/local/bin
@ -71,9 +74,8 @@ k3s_agent: {}
k3s_become_for_all: false
k3s_become_for_systemd: null
k3s_become_for_install_dir: null
k3s_become_for_manifests_dir: null
k3s_become_for_directory_creation: null
k3s_become_for_usr_local_bin: null
k3s_become_for_data_dir: null
k3s_become_for_package_install: null
k3s_become_for_kubectl: null
k3s_become_for_uninstall: null

View File

@ -5,6 +5,11 @@
vars:
molecule_is_test: true
k3s_build_cluster: false
k3s_install_dir: /opt/k3s/bin
k3s_config_file: /opt/k3s/etc/k3s.yaml
k3s_server:
data-dir: /var/lib/k3s-io
default-local-storage-path: /var/lib/k3s-io/local-storage
k3s_server_manifests_templates:
- "molecule/autodeploy/templates/00-ns-monitoring.yml.j2"
roles:

View File

@ -0,0 +1,9 @@
---
- name: Prepare
hosts: node*
become: true
tasks:
- name: Ensure apt cache is updated
ansible.builtin.apt:
update_cache: true
when: ansible_pkg_mgr == 'apt'

View File

@ -6,7 +6,7 @@
pyratlabs_issue_controller_dump: true
pre_tasks:
- name: Ensure k3s_debug is set
set_fact:
ansible.builtin.set_fact:
k3s_debug: true
roles:
- xanmanning.k3s

View File

@ -0,0 +1,8 @@
---
- name: Prepare
hosts: all
tasks:
- name: Ensure apt cache is updated
ansible.builtin.apt:
update_cache: true
when: ansible_pkg_mgr == 'apt'

View File

@ -10,7 +10,7 @@
datastore-endpoint: "postgres://postgres:verybadpass@database:5432/postgres?sslmode=disable"
pre_tasks:
- name: Set each node to be a control node
set_fact:
ansible.builtin.set_fact:
k3s_control_node: true
when: inventory_hostname in ['node2', 'node3']
roles:

View File

@ -1,5 +1,5 @@
---
- name: Prepare
- name: Prepare Load Balancer
hosts: loadbalancer
tasks:
- name: Ensure apt cache is updated
@ -28,3 +28,11 @@
command: haproxy -D -f /usr/local/etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
args:
creates: /var/run/haproxy.pid
- name: Prepare nodes
hosts: node*
tasks:
- name: Ensure apt cache is updated
ansible.builtin.apt:
update_cache: true
when: ansible_pkg_mgr == 'apt'

View File

@ -11,7 +11,7 @@
secrets-encryption: true
pre_tasks:
- name: Set each node to be a control node
set_fact:
ansible.builtin.set_fact:
k3s_control_node: true
roles:
- role: xanmanning.k3s

View File

@ -1,5 +1,5 @@
---
- name: Prepare
- name: Prepare Load Balancer
hosts: loadbalancer
tasks:
- name: Ensure apt cache is updated
@ -28,3 +28,11 @@
command: haproxy -D -f /usr/local/etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
args:
creates: /var/run/haproxy.pid
- name: Prepare nodes
hosts: node*
tasks:
- name: Ensure apt cache is updated
ansible.builtin.apt:
update_cache: true
when: ansible_pkg_mgr == 'apt'

View File

@ -0,0 +1,8 @@
---
- name: Prepare
hosts: all
tasks:
- name: Ensure apt cache is updated
ansible.builtin.apt:
update_cache: true
when: ansible_pkg_mgr == 'apt'

View File

@ -1,6 +1,6 @@
-r ../requirements.txt
molecule[docker]<3.2.0
molecule[docker]<3.2
docker>=4.3.1
yamllint>=1.25.0
ansible-lint>=4.3.5

View File

@ -1,2 +1 @@
jmespath>=0.10.0
ansible>=2.10.3
ansible>=2.10.4

View File

@ -1,25 +1,25 @@
---
- name: Ensure NODE_TOKEN is captured from control node
- name: Ensure cluster token is captured from control node
ansible.builtin.slurp:
path: "{{ k3s_runtime_config['data-dir'] | default(k3s_data_dir) }}/server/node-token"
register: k3s_slurped_control_token
path: "{{ k3s_runtime_config['data-dir'] | default(k3s_data_dir) }}/server/token"
register: k3s_slurped_cluster_token
delegate_to: "{{ k3s_control_delegate }}"
when: k3s_control_token is not defined and not ansible_check_mode
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure NODE_TOKEN is formatted correctly for use in templates
set_fact:
k3s_control_token: "{{ k3s_slurped_control_token.content | b64decode }}"
- name: Ensure cluster token is formatted correctly for use in templates
ansible.builtin.set_fact:
k3s_control_token: "{{ k3s_slurped_cluster_token.content | b64decode }}"
when: k3s_control_token is not defined and not ansible_check_mode
- name: Ensure dummy NODE_TOKEN is defined for ansible_check_mode
set_fact:
- name: Ensure dummy cluster token is defined for ansible_check_mode
ansible.builtin.set_fact:
k3s_control_token: "{{ k3s_control_delegate | to_uuid }}"
check_mode: false
when: k3s_control_token is not defined and ansible_check_mode
- name: Ensure the cluster NODE_TOKEN file location exists
- name: Ensure the cluster token file location exists
ansible.builtin.file:
path: "{{ k3s_token_location | dirname }}"
state: directory
@ -69,10 +69,10 @@
when: k3s_control_node and not k3s_primary_control_node
become: "{{ k3s_become_for_systemd | ternary(true, false, k3s_become_for_all) }}"
- import_tasks: ../validate/check-control-plane.yml
- import_tasks: ../validate/state/control-plane.yml
when: not k3s_skip_validation
- meta: flush_handlers
- import_tasks: ../validate/check-cluster-nodes-ready.yml
- import_tasks: ../validate/state/nodes.yml
when: not k3s_skip_validation

View File

@ -1,7 +1,7 @@
---
- name: Ensure docker is installed using amazon-linux-extras
command: amazon-linux-extras install docker
ansible.builtin.command: amazon-linux-extras install docker
args:
creates: /etc/docker
notify:

View File

@ -45,7 +45,7 @@
become: "{{ k3s_become_for_package_install | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure Docker repository is installed and configured from file
command: yum-config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
ansible.builtin.command: yum-config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
args:
creates: /etc/yum.repos.d/docker-ce.repo
when: ansible_distribution | lower not in ['amazon']

View File

@ -1,19 +1,19 @@
---
- name: Ensure target host architecture information is set as a fact
set_fact:
ansible.builtin.set_fact:
k3s_arch: "{{ k3s_arch_lookup[ansible_architecture].arch }}"
k3s_arch_suffix: "{{ k3s_arch_lookup[ansible_architecture].suffix }}"
check_mode: false
- name: Ensure URLs are set as facts for downloading binaries
set_fact:
ansible.builtin.set_fact:
k3s_binary_url: "{{ k3s_github_download_url }}/{{ k3s_release_version }}/k3s{{ k3s_arch_suffix }}"
k3s_hash_url: "{{ k3s_github_download_url }}/{{ k3s_release_version }}/sha256sum-{{ k3s_arch }}.txt"
check_mode: false
- name: Override k3s_binary_url and k3s_hash_url facts for testing specific commit
set_fact:
ansible.builtin.set_fact:
k3s_binary_url: "https://storage.googleapis.com/k3s-ci-builds/k3s{{ k3s_arch_suffix }}-{{ k3s_release_version }}"
k3s_hash_url: "https://storage.googleapis.com/k3s-ci-builds/k3s{{ k3s_arch_suffix }}-{{ k3s_release_version }}.sha256sum"
when:
@ -28,7 +28,7 @@
check_mode: false
- name: Ensure sha256sum is set from hashsum variable
set_fact:
ansible.builtin.set_fact:
k3s_hash_sum: "{{ (k3s_hash_sum_raw.content.split('\n') |
select('search', 'k3s' + k3s_arch_suffix) |
reject('search', 'images') |

View File

@ -1,7 +1,7 @@
---
- name: Ensure systemd context is correct if we are running k3s rootless
set_fact:
ansible.builtin.set_fact:
k3s_systemd_context: user
k3s_systemd_unit_dir: "{{ ansible_user_dir }}/.config/systemd/user"
when: k3s_runtime_config is defined

View File

@ -1,13 +1,13 @@
---
- name: Ensure k3s_release_version is set to default if false
set_fact:
ansible.builtin.set_fact:
k3s_release_version: "{{ k3s_release_channel }}"
check_mode: false
when: k3s_release_version is defined and not k3s_release_version
- name: Ensure the default release channel is set
set_fact:
ansible.builtin.set_fact:
k3s_release_channel: "{{ k3s_release_version | default('stable') }}"
check_mode: false
@ -20,7 +20,7 @@
check_mode: false
- name: Ensure the release version is set as a fact
set_fact:
ansible.builtin.set_fact:
k3s_release_version: "{{ item.latest }}"
loop: "{{ k3s_latest_release.json.data }}"
check_mode: false

View File

@ -0,0 +1,10 @@
---
- name: Ensure {{ directory.name }} exists
ansible.builtin.file:
path: "{{ directory.path }}"
state: directory
mode: "{{ directory.mode | default(0755) }}"
become: "{{ k3s_become_for_directory_creation | ternary(true, false, k3s_become_for_all) }}"
when: directory.path is defined
and directory.path != omit

View File

@ -1,32 +1,13 @@
---
- name: Ensure config directory exists
ansible.builtin.file:
path: "{{ k3s_config_file | dirname }}"
state: directory
mode: 0755
become: "{{ k3s_become_for_install_dir | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure systemd unit file directory exists
ansible.builtin.file:
path: "{{ k3s_systemd_unit_dir }}"
state: directory
mode: 0755
become: "{{ k3s_become_for_systemd | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure data directory exists when not using the default
ansible.builtin.file:
path: "{{ k3s_runtime_config['data-dir'] | default(k3s_data_dir) }}"
state: directory
mode: 0755
become: "{{ k3s_become_for_data_dir | ternary(true, false, k3s_become_for_all) }}"
when: k3s_runtime_config is defined
and "data-dir" in k3s_runtime_config
and k3s_runtime_config['data-dir'] != k3s_data_dir
- include_tasks: install-k3s-directories.yml
loop: "{{ k3s_ensure_directories_exist }}"
loop_control:
loop_var: directory
- include_tasks: install-k3s-node.yml
when: ((k3s_control_node and k3s_controller_count | length == 1)
or (k3s_primary_control_node and k3s_controller_count | length > 1)) and not ansible_check_mode
when: ((k3s_control_node and k3s_controller_list | length == 1)
or (k3s_primary_control_node and k3s_controller_list | length > 1)) and not ansible_check_mode
- meta: flush_handlers
@ -39,6 +20,6 @@
state: started
enabled: true
scope: "{{ k3s_systemd_context }}"
when: (k3s_control_node and k3s_controller_count | length == 1)
or (k3s_primary_control_node and k3s_controller_count | length > 1)
when: (k3s_control_node and k3s_controller_list | length == 1)
or (k3s_primary_control_node and k3s_controller_list | length > 1)
become: "{{ k3s_become_for_systemd | ternary(true, false, k3s_become_for_all) }}"

View File

@ -6,7 +6,7 @@
path: "{{ k3s_server_manifests_dir }}"
mode: 0755
when: k3s_server_manifests_templates | length > 0
become: "{{ k3s_become_for_manifests_dir | ternary(true, false, k3s_become_for_all) }}"
become: "{{ k3s_become_for_directory_creation | ternary(true, false, k3s_become_for_all) }}"
# https://rancher.com/docs/k3s/latest/en/advanced/#auto-deploying-manifests
- name: Ensure Auto-Deploying Manifests are copied to controllers
@ -15,4 +15,4 @@
dest: "{{ k3s_server_manifests_dir }}/{{ item | basename | replace('.j2','') }}"
mode: 0644
loop: "{{ k3s_server_manifests_templates }}"
become: "{{ k3s_become_for_manifests_dir | ternary(true, false, k3s_become_for_all) }}"
become: "{{ k3s_become_for_directory_creation | ternary(true, false, k3s_become_for_all) }}"

View File

@ -1,43 +1,57 @@
---
- name: Ensure k3s_build_cluster is false if running against a single node.
set_fact:
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
set_fact:
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
set_fact:
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 a k3s control node is defined if none are found in ansible_play_hosts_all
block:
- name: Set the control host
set_fact:
k3s_control_node: true
when: inventory_hostname == ansible_play_hosts_all[0]
when: true not in (hostvars | json_query('*.k3s_control_node'))
and k3s_build_cluster is defined and k3s_build_cluster
- 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
set_fact:
k3s_controller_count: "{{ k3s_controller_count + [ item ] }}"
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
set_fact:
ansible.builtin.set_fact:
k3s_primary_control_node: true
when: k3s_controller_count is defined
and k3s_controller_count | length > 1
and inventory_hostname == k3s_controller_count[0]
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
@ -61,19 +75,19 @@
- name: Delegate a control plane node
block:
- name: Lookup control node from file
command: "grep '{{ 'P_True' if (k3s_controller_count | length > 1) else 'C_True' }}' /tmp/inventory.txt"
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
set_fact:
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
set_fact:
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
@ -82,13 +96,13 @@
or k3s_control_delegate is not defined
- name: Ensure k3s_runtime_config is set for control plane
set_fact:
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
set_fact:
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)

View File

@ -17,12 +17,12 @@
- name: Ensure docker installation tasks are run
block:
- include_tasks: build/install-docker-prerequisites-{{ ansible_os_family | lower }}.yml
- include_tasks: build/docker/{{ ansible_os_family | lower }}/install-prerequisites.yml
- import_tasks: build/install-docker.yml
- import_tasks: build/docker/install.yml
when: ansible_distribution | replace(" ", "-") | lower not in ['amazon', 'suse', 'opensuse-leap', 'archlinux']
- include_tasks: build/install-docker-{{ ansible_distribution | replace(" ", "-") | lower }}.yml
- include_tasks: build/docker/{{ ansible_distribution | replace(" ", "-") | lower }}/install.yml
when: ansible_distribution | replace(" ", "-") | lower in ['amazon', 'suse', 'opensuse-leap', 'archlinux']
when: ('docker' in k3s_runtime_config and k3s_runtime_config.docker)

View File

@ -9,16 +9,16 @@
- name: Ensure docker uninstall tasks are run
block:
- import_tasks: teardown/uninstall-docker.yml
- import_tasks: teardown/docker/uninstall.yml
when: ansible_distribution | replace(" ", "-") | lower not in ['amazon', 'suse', 'opensuse-leap', 'archlinux']
- include_tasks: teardown/uninstall-docker-{{ ansible_distribution | replace(" ", "-") | lower }}.yml
- include_tasks: teardown/docker/{{ ansible_distribution | replace(" ", "-") | lower }}/uninstall.yml
when: ansible_distribution | replace(" ", "-") | lower in ['amazon', 'suse', 'opensuse-leap', 'archlinux']
- include_tasks: teardown/uninstall-docker-prerequisites-{{ ansible_os_family | lower }}.yml
- include_tasks: teardown/docker/{{ ansible_os_family | lower }}/uninstall-prerequisites.yml
when: ('docker' in k3s_runtime_config and k3s_runtime_config.docker)
and ('rootless' not in k3s_runtime_config or not k3s_runtime_config.rootless)
- import_tasks: validate/check-uninstalled.yml
- import_tasks: validate/state/uninstalled.yml
when: not k3s_skip_validation

View File

@ -1,7 +1,7 @@
---
- name: Ensure docker is uninstalled using amazon-linux-extras
command: amazon-linux-extras uninstall docker
ansible.builtin.command: amazon-linux-extras uninstall docker
register: uninstall_docker_from_amazon_linux
changed_when: uninstall_docker_from_amazon_linux.rc == 0
become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}"

View File

@ -10,7 +10,7 @@
block:
- name: Gather a list of nodes
command: "{{ k3s_install_dir }}/kubectl get nodes"
ansible.builtin.command: "{{ k3s_install_dir }}/kubectl get nodes"
changed_when: false
failed_when: false
delegate_to: "{{ k3s_control_delegate }}"
@ -19,7 +19,7 @@
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure uninstalled nodes are drained
command: "{{ k3s_install_dir }}/kubectl drain {{ item }} --ignore-daemonsets --delete-local-data"
ansible.builtin.command: "{{ k3s_install_dir }}/kubectl drain {{ item }} --ignore-daemonsets --delete-local-data"
delegate_to: "{{ k3s_control_delegate }}"
run_once: true
when: item in kubectl_get_nodes_result.stdout
@ -29,7 +29,7 @@
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure uninstalled nodes are removed
command: "{{ k3s_install_dir }}/kubectl delete node {{ item }}"
ansible.builtin.command: "{{ k3s_install_dir }}/kubectl delete node {{ item }}"
delegate_to: "{{ k3s_control_delegate }}"
run_once: true
when: item in kubectl_get_nodes_result.stdout

View File

@ -11,20 +11,20 @@
register: check_k3s_uninstall_script
- name: Check to see if docker is present
command: which docker
ansible.builtin.command: which docker
failed_when: false
changed_when: false
register: check_k3s_docker_path
- name: Run k3s-killall.sh
command: /usr/local/bin/k3s-killall.sh
ansible.builtin.command: /usr/local/bin/k3s-killall.sh
register: k3s_killall
changed_when: k3s_killall.rc == 0
when: check_k3s_killall_script.stat.exists
become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}"
- name: Run k3s-uninstall.sh
command: /usr/local/bin/k3s-uninstall.sh
ansible.builtin.command: /usr/local/bin/k3s-uninstall.sh
args:
removes: /usr/local/bin/k3s-uninstall.sh
register: k3s_uninstall
@ -33,6 +33,6 @@
become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}"
- name: Clean up Docker
command: docker system prune -a --force
ansible.builtin.command: docker system prune -a --force
when: ("docker" in k3s_runtime_config and k3s_runtime_config.docker)
and check_k3s_docker_path.rc == 0

View File

@ -1,6 +0,0 @@
---
- name: Check to see if jmespath is installed
command: echo "{{ (hostvars | json_query('*.k3s_control_node')) }}"
register: k3s_check_jmespath
changed_when: false

View File

@ -3,31 +3,31 @@
- name: Check the conditions when a single controller is defined
ansible.builtin.assert:
that:
- (k3s_controller_count | length == 1)
- (k3s_controller_list | length == 1)
and ("datastore-endpoint" not in k3s_runtime_config or not k3s_runtime_config['datastore-endpoint'])
and (k3s_etcd_datastore is not defined or not k3s_etcd_datastore)
success_msg: "Control plane configuration is valid."
fail_msg: "Control plane configuration is invalid. Please see notes about k3s_control_node and HA in README.md."
when: k3s_controller_count | length == 1
when: k3s_controller_list | length == 1
and not k3s_use_unsupported_config
and k3s_control_node
- name: Check the conditions when multiple controllers are defined
ansible.builtin.assert:
that:
- (k3s_controller_count | length >= 2)
- (k3s_controller_list | length >= 2)
and (("datastore-endpoint" in k3s_runtime_config and k3s_runtime_config['datastore-endpoint'])
or (k3s_etcd_datastore is defined and k3s_etcd_datastore))
success_msg: "Control plane configuration is valid."
fail_msg: "Control plane configuration is invalid. Please see notes about k3s_control_node and HA in README.md."
when: k3s_controller_count | length >= 2
when: k3s_controller_list | length >= 2
and k3s_control_node
- name: Check the conditions when embedded etcd is defined
ansible.builtin.assert:
that:
- (k3s_controller_count | length >= 3)
and (((k3s_controller_count | length) % 2) == 1)
- (k3s_controller_list | length >= 3)
and (((k3s_controller_list | length) % 2) == 1)
success_msg: "Control plane configuration is valid."
fail_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"
when: k3s_etcd_datastore is defined

View File

@ -1,11 +1,13 @@
---
- name: Check if any experimental variables are configure and if they are enabled with k3s_use_experimental
- name: Check if embedded etcd datastore is enabled and marked as experimental
ansible.builtin.assert:
that:
- k3s_use_experimental is defined and k3s_use_experimental
success_msg: "Experimental variables are defined and enabled."
fail_msg: "Experimental variable k3s_etcd_datastore has been configured. If you want to use this ensure you set k3s_use_experimental"
fail_msg: >-
Experimental variable k3s_etcd_datastore has been configured.
If you want to use this ensure you set k3s_use_experimental,
when: k3s_etcd_datastore is defined and k3s_etcd_datastore
and (k3s_release_version | replace('v', '')) is version_compare("1.19.5", '<')
@ -14,7 +16,12 @@
that:
- k3s_use_experimental is defined and k3s_use_experimental
success_msg: "Experimental variables are defined and enabled."
fail_msg: "Experimental variable {{ item.setting }} has been configured. If you want to use this ensure you set k3s_use_experimental"
fail_msg: >-
Experimental variable {{ item.setting }} has been configured.
If you want to use this ensure you set k3s_use_experimental.
{% if item.document is defined %}
Documentation: {{ item.documentation }}
{% endif %}
loop: "{{ k3s_experimental_config }}"
when: (item.setting in k3s_runtime_config and k3s_runtime_config[item.setting])
and ((item.until is not defined) or

View File

@ -1,7 +1,7 @@
---
- name: Check if newuidmap is available
command: which newuidmap
ansible.builtin.command: which newuidmap
failed_when: false
changed_when: false
register: k3s_check_newuidmap_installed
@ -18,7 +18,7 @@
when: k3s_check_unprivileged_userns_exists.stat.exists
- name: Set the value of k3s_get_unprivileged_userns_clone
set_fact:
ansible.builtin.set_fact:
k3s_get_unprivileged_userns_clone:
content: "MQo="
when: not k3s_check_unprivileged_userns_exists.stat.exists
@ -39,7 +39,7 @@
register: k3s_get_subgid
- name: Get current user subuid and subgid values
set_fact:
ansible.builtin.set_fact:
k3s_current_user_subuid: "{{ (k3s_get_subuid['content'] | b64decode).split('\n')
| select('search', ansible_user_id) | first | default('UserNotFound:0:0') }}"
k3s_current_user_subgid: "{{ (k3s_get_subgid['content'] | b64decode).split('\n')

View File

@ -12,7 +12,12 @@
that:
- (item.setting not in k3s_runtime_config)
success_msg: "{{ item.setting }} not found in server/agent config"
fail_msg: "{{ item.setting }} found in server/agent config. Please set {{ item.correction }} to use this option."
fail_msg: >-
{{ item.setting }} found in server/agent config.
Please set {{ item.correction }} to use this option.
{% if item.document is defined %}
Documentation: {{ item.documentation }}
{% endif %}
loop: "{{ k3s_config_exclude }}"
- name: Check configuration in k3s_server and k3s_agent for deprecated configuration
@ -20,7 +25,12 @@
that:
- (item.setting not in k3s_runtime_config)
success_msg: "{{ item.setting }} not found in server/agent config"
fail_msg: "{{ item.setting }} found in server/agent config. Please set {{ item.correction }} to use this option."
fail_msg: >-
{{ item.setting }} found in server/agent config.
Please set {{ item.correction }} to use this option.
{% if item.document is defined %}
Documentation: {{ item.documentation }}
{% endif %}
loop: "{{ k3s_deprecated_config }}"
when: (item.when is not defined
or (item.when is defined and (k3s_release_version | replace('v', '')) is version_compare(item.when, '>=')))
@ -31,7 +41,12 @@
that:
- (k3s_release_version | replace('v', '')) is version_compare(item.version, '>=')
success_msg: "{{ item.setting }} is supported by {{ k3s_release_version }}"
fail_msg: "{{ item.setting }} is not supported in {{ k3s_release_version }}. Please update to v{{ item.version }} to use this option."
fail_msg: >-
{{ item.setting }} is not supported in {{ k3s_release_version }}.
Please update to v{{ item.version }} to use this option.
{% if item.document is defined %}
Documentation: {{ item.documentation }}
{% endif %}
loop: "{{ k3s_config_version_check }}"
when: k3s_config_version_check is defined
and item.setting in k3s_runtime_config

View File

@ -4,7 +4,7 @@
ansible.builtin.setup:
- name: Ensure Ansible version is captured
command: ansible --version
ansible.builtin.command: ansible --version
failed_when: false
changed_when: false
register: check_ansible_version
@ -13,7 +13,7 @@
become: false
- name: Ensure Ansible config is captured
command: ansible-config dump --only-changed
ansible.builtin.command: ansible-config dump --only-changed
failed_when: false
changed_when: false
register: check_ansible_config
@ -22,7 +22,7 @@
become: false
- name: Ensure a list of roles is captured
command: ansible-galaxy role list
ansible.builtin.command: ansible-galaxy role list
failed_when: false
changed_when: false
register: check_ansible_roles
@ -32,7 +32,7 @@
- name: Ensure facts are written to disk
ansible.builtin.copy:
dest: pyratlabs-issue-dump.txt
dest: "{{ playbook_dir }}/pyratlabs-issue-dump.txt"
content: |
# Begin ANSIBLE VERSION
{{ check_ansible_version.stdout }}
@ -53,8 +53,10 @@
# Begin K3S ROLE CONFIG
{% for host in ansible_play_hosts_all %}
## {{ host }}
{% for config_key in hostvars[host] | to_json | from_json | json_query('keys(@)[?starts_with(@, `k3s_`)]') %}
{% for config_key in hostvars[host] %}
{% if config_key | regex_search('^k3s_') %}
{{ config_key }}: {{ hostvars[host][config_key] | to_json }}
{% endif %}
{% endfor %}
{% endfor %}
@ -63,7 +65,9 @@
# Begin K3S RUNTIME CONFIG
{% for host in ansible_play_hosts_all %}
## {{ host }}
{% if hostvars[host].k3s_runtime_config is defined %}
{{ hostvars[host].k3s_runtime_config }}
{% endif %}
{% endfor %}
# End K3S RUNTIME CONFIG
mode: 0600

View File

@ -0,0 +1,11 @@
---
- name: Check that Ansible v{{ ansible_version.string }} is supported by this role
ansible.builtin.assert:
that:
- ansible_version.string is version_compare(k3s_ansible_min_version, '>=')
fail_msg: "Ansible v{{ ansible_version.string }} is not supported by this role. Please install >= v{{ k3s_ansible_min_version }}"
success_msg: "Ansible v{{ ansible_version.string }} is supported."
become: false
delegate_to: localhost
run_once: true

View File

@ -0,0 +1,22 @@
---
- name: Check that {{ package.name }} is installed
ansible.builtin.command: "which {{ package.name }}"
changed_when: false
failed_when: false
register: check_k3s_required_package
- name: Test that checks for {{ package.name }} passed
ansible.builtin.assert:
that:
- check_k3s_required_package.rc == 0
success_msg: "Found required package: {{ package.name }}."
fail_msg: >-
Could not find package: {{ package.name }}.
{% if package.documentation is defined %}
Documentation: {{ package.documentation }}
{% endif %}
when: (package.until is not defined
or k3s_release_version is version_compare(package.until, '>='))
and (package.from is not defined
or k3s_release_version is version_compare(package.from, '>='))

View File

@ -1,5 +0,0 @@
---
- name: Ensure k3s_runtime_config is set for validation
set_fact:
k3s_runtime_config: "{{ (k3s_server | default({})) | combine (k3s_agent | default({})) }}"

View File

@ -1,12 +1,12 @@
---
- import_tasks: check-variables.yml
- import_tasks: configuration/variables.yml
- import_tasks: check-experimental-variables.yml
- import_tasks: configuration/experimental-variables.yml
- import_tasks: check-unsupported-rootless.yml
- import_tasks: configuration/unsupported-rootless.yml
when: ("rootless" in k3s_runtime_config)
and k3s_runtime_config.rootless
- import_tasks: check-control-count.yml
- import_tasks: configuration/control-node-count.yml
when: k3s_build_cluster is defined and k3s_build_cluster

View File

@ -1,5 +1,5 @@
---
- import_tasks: check-control-plane.yml
- import_tasks: state/control-plane.yml
- import_tasks: check-cluster-nodes-ready.yml
- import_tasks: state/nodes.yml

View File

@ -1,6 +1,6 @@
---
- name: Check to see if k3s_state is a supported value
- name: Check that k3s_state is a supported value
ansible.builtin.assert:
that:
- k3s_state in k3s_valid_states
@ -8,10 +8,16 @@
success_msg: "k3s_state is valid."
when: k3s_state is defined
- import_tasks: generate-check-config.yml
- import_tasks: check-environment.yml
- import_tasks: environment/local/packages.yml
when: not k3s_skip_validation
and not k3s_skip_env_checks
- include_tasks: issue-data.yml
- include_tasks: environment/remote/packages.yml
loop: "{{ k3s_check_packages }}"
loop_control:
loop_var: package
when: not k3s_skip_validation
and not k3s_skip_env_checks
- include_tasks: environment/local/issue-data.yml
when: pyratlabs_issue_controller_dump is defined and pyratlabs_issue_controller_dump

View File

@ -1,7 +1,7 @@
---
- name: Check that all nodes to be ready
command: "{{ k3s_install_dir }}/kubectl get nodes"
ansible.builtin.command: "{{ k3s_install_dir }}/kubectl get nodes"
changed_when: false
failed_when: kubectl_get_nodes_result.stdout.find("was refused") != -1 or
kubectl_get_nodes_result.stdout.find("ServiceUnavailable") != -1

View File

@ -1,7 +1,7 @@
---
- name: Check that k3s is not running
command: pgrep k3s
ansible.builtin.command: pgrep k3s
ignore_errors: true
changed_when: false
register: check_k3s_process
@ -12,7 +12,7 @@
when: check_k3s_process.rc == 0
- name: Check that docker is not running
command: pgrep docker
ansible.builtin.command: pgrep docker
ignore_errors: true
changed_when: false
register: check_k3s_docker_process

View File

@ -37,7 +37,7 @@ for bin in {{ k3s_install_dir }}/k3s*; do
fi
done
rm -rf {{ k3s_config_file | dirname }}
rm -rf {{ k3s_config_dir }}
rm -rf /run/k3s
rm -rf /run/flannel
rm -rf {{ k3s_runtime_config['data-dir'] | default(k3s_data_dir) }}

View File

@ -17,7 +17,7 @@ ExecStart={{ k3s_install_dir }}/k3s
{% if k3s_control_node %}
server
{% if (k3s_etcd_datastore is defined and k3s_etcd_datastore) and (k3s_primary_control_node is not defined or not k3s_primary_control_node) %}
--server https://{{ k3s_control_node_address }}:{{ k3s_runtime_config['https-listen-port'] | default(6443) }}
--server https://{{ k3s_control_node_address }}:{{ k3s_control_plane_port | default(6443) }}
--token-file {{ k3s_token_location }}
{% endif %}
{% if k3s_server is defined %}
@ -25,7 +25,7 @@ ExecStart={{ k3s_install_dir }}/k3s
{% endif %}
{% else %}
agent
--server https://{{ k3s_control_node_address }}:{{ k3s_runtime_config['https-listen-port'] | default(6443) }}
--server https://{{ k3s_control_node_address }}:{{ k3s_control_plane_port | default(6443) }}
--token-file {{ k3s_token_location }}
{% if k3s_agent is defined %}
--config {{ k3s_config_file }}

View File

@ -1,7 +1,8 @@
---
# Minimum supported version
# Minimum supported versions
k3s_min_version: 1.19.3
k3s_ansible_min_version: 2.10.4
# Valid states for this role
k3s_valid_states:
@ -43,17 +44,19 @@ k3s_arch_lookup:
# Always default to stable channel, this will change with k3s_release_version
k3s_release_channel: stable
# Create the API address for the k3s_github_url that has been specified
k3s_github_api: "{{ k3s_github_url | replace('github.com', 'api.github.com') }}"
# Create the latest release API link for github. Possibly deprecated with k3s.io updates API
k3s_github_api_releases: "{{ k3s_github_api | replace('.com', '.com/repos') }}/releases/latest"
# K3s updates API
k3s_api_releases: https://update.k3s.io/v1-release/channels
# Download location for releases
k3s_github_download_url: "{{ k3s_github_url }}/releases/download"
# Generate a runtime config dictionary for validation
k3s_runtime_config: "{{ (k3s_server | default({})) | combine (k3s_agent | default({})) }}"
# Empty array for counting the number of control plane nodes
k3s_controller_count: []
k3s_controller_list: []
# Control plane port default
k3s_control_plane_port: 6443
# Default to the "system" systemd context, this will be "user" when running rootless
k3s_systemd_context: system
@ -62,16 +65,37 @@ k3s_systemd_context: system
# management, this should live in /etc/systemd, not /lib/systemd
k3s_systemd_unit_dir: "/etc/systemd/{{ k3s_systemd_context }}"
# Directory for gathering the k3s token for clustering. I don't see this changing.
k3s_token_location: "/etc/rancher/cluster-token"
# Data directory location for k3s
k3s_data_dir: /var/lib/rancher/k3s
k3s_data_dir: "{{ k3s_runtime_config['data-dir'] | default('/var/lib/rancher/k3s') }}"
# Config directroy location for k3s
k3s_config_dir: "{{ k3s_config_file | dirname }}"
# Directory for gathering the k3s token for clustering. I don't see this changing.
k3s_token_location: "{{ k3s_config_dir }}/cluster-token"
# Path for additional Kubernetes Manifests
# https://rancher.com/docs/k3s/latest/en/advanced/#auto-deploying-manifests
k3s_server_manifests_dir: "{{ k3s_data_dir }}/server/manifests"
# Packages that we need to check are installed
k3s_check_packages: []
# - name: dummy
# from: 1.19.2
# until: 1.21.0
# documentation: https://example.com
# Directories that we need to ensure exist
k3s_ensure_directories_exist:
- name: Config directory
path: "{{ k3s_config_dir }}"
- name: Systemd unit file directory
path: "{{ k3s_systemd_unit_dir }}"
- name: Data directory
path: "{{ k3s_data_dir }}"
- name: Default local storage path
path: "{{ k3s_runtime_config['default-local-storage-path'] | default(omit) }}"
# Config items that should not appear in k3s_server or k3s_agent
k3s_config_exclude:
- setting: server