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 ## 2020-12-19, v2.3.0
### Notable changes ### 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: The host you're running Ansible from requires the following Python dependencies:
- `ansible >= 2.10` - `ansible >= 2.10.4`
- `jmespath >= 0.10.0`
You can install dependencies using the requirements.txt file in this repository: You can install dependencies using the requirements.txt file in this repository:
`pip3 install -r requirements.txt`. `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_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_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_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_dir` | Installation directory for k3s. | `/usr/local/bin` |
| `k3s_install_hard_links` | Install using hard links rather than symbolic links. | `false` | | `k3s_install_hard_links` | Install using hard links rather than symbolic links. | `false` |
| `k3s_server_manifests_templates` | A list of Auto-Deploying Manifests Templates. | [] | | `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, The below variables are used to change the way the role executes in Ansible,
particularly with regards to privilege escalation. particularly with regards to privilege escalation.
| Variable | Description | Default Value | | Variable | Description | Default Value |
|----------------------------------|---------------------------------------------------------------------|---------------| |-------------------------------------|---------------------------------------------------------------------|---------------|
| `k3s_become_for_all` | Escalate user privileges for all tasks. Overrides all of the below. | `false` | | `k3s_skip_validation` | Skip all tasks that validate configuration. | `false` |
| `k3s_become_for_systemd` | Escalate user privileges for systemd tasks. | NULL | | `k3s_skip_env_checks` | Skill all tasks that check environment configuration. | `false` |
| `k3s_become_for_install_dir` | Escalate user privileges for creating installation directories. | NULL | | `k3s_become_for_all` | Escalate user privileges for all tasks. Overrides all of the below. | `false` |
| `k3s_become_for_manifests_dir` | Escalate user privileges for creating manifests directory. | NULL | | `k3s_become_for_systemd` | Escalate user privileges for systemd tasks. | NULL |
| `k3s_become_for_usr_local_bin` | Escalate user privileges for writing to `/usr/local/bin`. | NULL | | `k3s_become_for_install_dir` | Escalate user privileges for creating installation directories. | NULL |
| `k3s_become_for_data_dir` | Escalate user privileges for creating data directory. | NULL | | `k3s_become_for_directory_creation` | Escalate user privileges for creating application directories. | NULL |
| `k3s_become_for_package_install` | Escalate user privileges for installing k3s. | NULL | | `k3s_become_for_usr_local_bin` | Escalate user privileges for writing to `/usr/local/bin`. | NULL |
| `k3s_become_for_kubectl` | Escalate user privileges for running `kubectl`. | NULL | | `k3s_become_for_package_install` | Escalate user privileges for installing k3s. | NULL |
| `k3s_become_for_uninstall` | Escalate user privileges for uninstalling 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` #### Important note about `k3s_release_version`
@ -303,7 +302,7 @@ stable release:
datastore-endpoint: "postgres://postgres:verybadpass@database:5432/postgres?sslmode=disable" datastore-endpoint: "postgres://postgres:verybadpass@database:5432/postgres?sslmode=disable"
pre_tasks: pre_tasks:
- name: Set each node to be a control node - name: Set each node to be a control node
set_fact: ansible.builtin.set_fact:
k3s_control_node: true k3s_control_node: true
when: inventory_hostname in ['node2', 'node3'] when: inventory_hostname in ['node2', 'node3']
roles: roles:

View File

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

View File

@ -5,6 +5,11 @@
vars: vars:
molecule_is_test: true molecule_is_test: true
k3s_build_cluster: false 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: k3s_server_manifests_templates:
- "molecule/autodeploy/templates/00-ns-monitoring.yml.j2" - "molecule/autodeploy/templates/00-ns-monitoring.yml.j2"
roles: 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 pyratlabs_issue_controller_dump: true
pre_tasks: pre_tasks:
- name: Ensure k3s_debug is set - name: Ensure k3s_debug is set
set_fact: ansible.builtin.set_fact:
k3s_debug: true k3s_debug: true
roles: roles:
- xanmanning.k3s - 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" datastore-endpoint: "postgres://postgres:verybadpass@database:5432/postgres?sslmode=disable"
pre_tasks: pre_tasks:
- name: Set each node to be a control node - name: Set each node to be a control node
set_fact: ansible.builtin.set_fact:
k3s_control_node: true k3s_control_node: true
when: inventory_hostname in ['node2', 'node3'] when: inventory_hostname in ['node2', 'node3']
roles: roles:

View File

@ -1,5 +1,5 @@
--- ---
- name: Prepare - name: Prepare Load Balancer
hosts: loadbalancer hosts: loadbalancer
tasks: tasks:
- name: Ensure apt cache is updated - 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 command: haproxy -D -f /usr/local/etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
args: args:
creates: /var/run/haproxy.pid 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 secrets-encryption: true
pre_tasks: pre_tasks:
- name: Set each node to be a control node - name: Set each node to be a control node
set_fact: ansible.builtin.set_fact:
k3s_control_node: true k3s_control_node: true
roles: roles:
- role: xanmanning.k3s - role: xanmanning.k3s

View File

@ -1,5 +1,5 @@
--- ---
- name: Prepare - name: Prepare Load Balancer
hosts: loadbalancer hosts: loadbalancer
tasks: tasks:
- name: Ensure apt cache is updated - 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 command: haproxy -D -f /usr/local/etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
args: args:
creates: /var/run/haproxy.pid 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 -r ../requirements.txt
molecule[docker]<3.2.0 molecule[docker]<3.2
docker>=4.3.1 docker>=4.3.1
yamllint>=1.25.0 yamllint>=1.25.0
ansible-lint>=4.3.5 ansible-lint>=4.3.5

View File

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

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

View File

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

View File

@ -45,7 +45,7 @@
become: "{{ k3s_become_for_package_install | ternary(true, false, k3s_become_for_all) }}" become: "{{ k3s_become_for_package_install | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure Docker repository is installed and configured from file - 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: args:
creates: /etc/yum.repos.d/docker-ce.repo creates: /etc/yum.repos.d/docker-ce.repo
when: ansible_distribution | lower not in ['amazon'] when: ansible_distribution | lower not in ['amazon']

View File

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

View File

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

View File

@ -1,13 +1,13 @@
--- ---
- name: Ensure k3s_release_version is set to default if false - name: Ensure k3s_release_version is set to default if false
set_fact: ansible.builtin.set_fact:
k3s_release_version: "{{ k3s_release_channel }}" k3s_release_version: "{{ k3s_release_channel }}"
check_mode: false check_mode: false
when: k3s_release_version is defined and not k3s_release_version when: k3s_release_version is defined and not k3s_release_version
- name: Ensure the default release channel is set - name: Ensure the default release channel is set
set_fact: ansible.builtin.set_fact:
k3s_release_channel: "{{ k3s_release_version | default('stable') }}" k3s_release_channel: "{{ k3s_release_version | default('stable') }}"
check_mode: false check_mode: false
@ -20,7 +20,7 @@
check_mode: false check_mode: false
- name: Ensure the release version is set as a fact - name: Ensure the release version is set as a fact
set_fact: ansible.builtin.set_fact:
k3s_release_version: "{{ item.latest }}" k3s_release_version: "{{ item.latest }}"
loop: "{{ k3s_latest_release.json.data }}" loop: "{{ k3s_latest_release.json.data }}"
check_mode: false 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 - include_tasks: install-k3s-directories.yml
ansible.builtin.file: loop: "{{ k3s_ensure_directories_exist }}"
path: "{{ k3s_config_file | dirname }}" loop_control:
state: directory loop_var: 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-node.yml - include_tasks: install-k3s-node.yml
when: ((k3s_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_count | length > 1)) and not ansible_check_mode or (k3s_primary_control_node and k3s_controller_list | length > 1)) and not ansible_check_mode
- meta: flush_handlers - meta: flush_handlers
@ -39,6 +20,6 @@
state: started state: started
enabled: true enabled: true
scope: "{{ k3s_systemd_context }}" scope: "{{ k3s_systemd_context }}"
when: (k3s_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_count | 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) }}" become: "{{ k3s_become_for_systemd | ternary(true, false, k3s_become_for_all) }}"

View File

@ -6,7 +6,7 @@
path: "{{ k3s_server_manifests_dir }}" path: "{{ k3s_server_manifests_dir }}"
mode: 0755 mode: 0755
when: k3s_server_manifests_templates | length > 0 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 # https://rancher.com/docs/k3s/latest/en/advanced/#auto-deploying-manifests
- name: Ensure Auto-Deploying Manifests are copied to controllers - name: Ensure Auto-Deploying Manifests are copied to controllers
@ -15,4 +15,4 @@
dest: "{{ k3s_server_manifests_dir }}/{{ item | basename | replace('.j2','') }}" dest: "{{ k3s_server_manifests_dir }}/{{ item | basename | replace('.j2','') }}"
mode: 0644 mode: 0644
loop: "{{ k3s_server_manifests_templates }}" 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. - name: Ensure k3s_build_cluster is false if running against a single node.
set_fact: ansible.builtin.set_fact:
k3s_build_cluster: false k3s_build_cluster: false
when: ansible_play_hosts_all | length < 2 when: ansible_play_hosts_all | length < 2
and k3s_control_node_address is not defined and k3s_control_node_address is not defined
- name: Ensure k3s control node fact is set - name: Ensure k3s control node fact is set
set_fact: ansible.builtin.set_fact:
k3s_control_node: "{{ 'false' if k3s_build_cluster else 'true' }}" k3s_control_node: "{{ 'false' if k3s_build_cluster else 'true' }}"
when: k3s_control_node is not defined when: k3s_control_node is not defined
- name: Ensure k3s primary control node fact is set - 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' }}" k3s_primary_control_node: "{{ 'false' if k3s_build_cluster else 'true' }}"
when: k3s_primary_control_node is not defined 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 - name: Ensure k3s control plane port is captures
block: ansible.builtin.set_fact:
- name: Set the control host k3s_control_plane_port: "{{ k3s_runtime_config['https-listen-port'] | default(6443) }}"
set_fact: delegate_to: k3s_primary_control_node
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 a count of control nodes is generated - name: Ensure a count of control nodes is generated
set_fact: ansible.builtin.set_fact:
k3s_controller_count: "{{ k3s_controller_count + [ item ] }}" k3s_controller_list: "{{ k3s_controller_list + [ item ] }}"
when: hostvars[item].k3s_control_node is defined when: hostvars[item].k3s_control_node is defined
and hostvars[item].k3s_control_node and hostvars[item].k3s_control_node
loop: "{{ ansible_play_hosts_all }}" 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 - 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 k3s_primary_control_node: true
when: k3s_controller_count is defined when: k3s_controller_list is defined
and k3s_controller_count | length > 1 and k3s_controller_list | length > 1
and inventory_hostname == k3s_controller_count[0] and inventory_hostname == k3s_controller_list[0]
and k3s_build_cluster is defined and k3s_build_cluster and k3s_build_cluster is defined and k3s_build_cluster
- name: Ensure ansible_host is mapped to inventory_hostname - name: Ensure ansible_host is mapped to inventory_hostname
@ -61,19 +75,19 @@
- name: Delegate a control plane node - name: Delegate a control plane node
block: block:
- name: Lookup control node from file - 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 changed_when: false
check_mode: false check_mode: false
register: k3s_control_delegate_raw register: k3s_control_delegate_raw
- name: Ensure control node is delegated to for obtaining a token - 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] }}" k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split(' @@@ ')[0] }}"
check_mode: false check_mode: false
when: k3s_control_delegate is not defined when: k3s_control_delegate is not defined
- name: Ensure the control node address is registered in Ansible - 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) }}" k3s_control_node_address: "{{ hostvars[k3s_control_delegate].ansible_host | default(hostvars[k3s_control_delegate].ansible_fqdn) }}"
check_mode: false check_mode: false
when: k3s_control_node_address is not defined when: k3s_control_node_address is not defined
@ -82,13 +96,13 @@
or k3s_control_delegate is not defined or k3s_control_delegate is not defined
- name: Ensure k3s_runtime_config is set for control plane - 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({}))) }}" k3s_runtime_config: "{{ (k3s_server | default({})) | combine((k3s_agent | default({}))) }}"
when: (k3s_server is defined or k3s_agent is defined) when: (k3s_server is defined or k3s_agent is defined)
and (k3s_control_node is defined and k3s_control_node) and (k3s_control_node is defined and k3s_control_node)
- name: Ensure k3s_runtime_config is set for agents - name: Ensure k3s_runtime_config is set for agents
set_fact: ansible.builtin.set_fact:
k3s_runtime_config: "{{ (k3s_agent | default({})) }}" k3s_runtime_config: "{{ (k3s_agent | default({})) }}"
when: k3s_agent is defined when: k3s_agent is defined
and (k3s_control_node is not defined or not k3s_control_node) 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 - name: Ensure docker installation tasks are run
block: 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'] 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: ansible_distribution | replace(" ", "-") | lower in ['amazon', 'suse', 'opensuse-leap', 'archlinux']
when: ('docker' in k3s_runtime_config and k3s_runtime_config.docker) when: ('docker' in k3s_runtime_config and k3s_runtime_config.docker)

View File

@ -9,16 +9,16 @@
- name: Ensure docker uninstall tasks are run - name: Ensure docker uninstall tasks are run
block: 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'] 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'] 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) when: ('docker' in k3s_runtime_config and k3s_runtime_config.docker)
and ('rootless' not in k3s_runtime_config or not k3s_runtime_config.rootless) 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 when: not k3s_skip_validation

View File

@ -1,7 +1,7 @@
--- ---
- name: Ensure docker is uninstalled using amazon-linux-extras - 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 register: uninstall_docker_from_amazon_linux
changed_when: uninstall_docker_from_amazon_linux.rc == 0 changed_when: uninstall_docker_from_amazon_linux.rc == 0
become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}" become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}"

View File

@ -10,7 +10,7 @@
block: block:
- name: Gather a list of nodes - 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 changed_when: false
failed_when: false failed_when: false
delegate_to: "{{ k3s_control_delegate }}" delegate_to: "{{ k3s_control_delegate }}"
@ -19,7 +19,7 @@
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}" become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure uninstalled nodes are drained - 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 }}" delegate_to: "{{ k3s_control_delegate }}"
run_once: true run_once: true
when: item in kubectl_get_nodes_result.stdout when: item in kubectl_get_nodes_result.stdout
@ -29,7 +29,7 @@
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}" become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure uninstalled nodes are removed - 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 }}" delegate_to: "{{ k3s_control_delegate }}"
run_once: true run_once: true
when: item in kubectl_get_nodes_result.stdout when: item in kubectl_get_nodes_result.stdout

View File

@ -11,20 +11,20 @@
register: check_k3s_uninstall_script register: check_k3s_uninstall_script
- name: Check to see if docker is present - name: Check to see if docker is present
command: which docker ansible.builtin.command: which docker
failed_when: false failed_when: false
changed_when: false changed_when: false
register: check_k3s_docker_path register: check_k3s_docker_path
- name: Run k3s-killall.sh - name: Run k3s-killall.sh
command: /usr/local/bin/k3s-killall.sh ansible.builtin.command: /usr/local/bin/k3s-killall.sh
register: k3s_killall register: k3s_killall
changed_when: k3s_killall.rc == 0 changed_when: k3s_killall.rc == 0
when: check_k3s_killall_script.stat.exists when: check_k3s_killall_script.stat.exists
become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}" become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}"
- name: Run k3s-uninstall.sh - name: Run k3s-uninstall.sh
command: /usr/local/bin/k3s-uninstall.sh ansible.builtin.command: /usr/local/bin/k3s-uninstall.sh
args: args:
removes: /usr/local/bin/k3s-uninstall.sh removes: /usr/local/bin/k3s-uninstall.sh
register: k3s_uninstall register: k3s_uninstall
@ -33,6 +33,6 @@
become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}" become: "{{ k3s_become_for_uninstall | ternary(true, false, k3s_become_for_all) }}"
- name: Clean up Docker - 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) when: ("docker" in k3s_runtime_config and k3s_runtime_config.docker)
and check_k3s_docker_path.rc == 0 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 - name: Check the conditions when a single controller is defined
ansible.builtin.assert: ansible.builtin.assert:
that: 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 ("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) and (k3s_etcd_datastore is not defined or not k3s_etcd_datastore)
success_msg: "Control plane configuration is valid." 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." 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 not k3s_use_unsupported_config
and k3s_control_node and k3s_control_node
- name: Check the conditions when multiple controllers are defined - name: Check the conditions when multiple controllers are defined
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- (k3s_controller_count | length >= 2) - (k3s_controller_list | length >= 2)
and (("datastore-endpoint" in k3s_runtime_config and k3s_runtime_config['datastore-endpoint']) and (("datastore-endpoint" in k3s_runtime_config and k3s_runtime_config['datastore-endpoint'])
or (k3s_etcd_datastore is defined and k3s_etcd_datastore)) or (k3s_etcd_datastore is defined and k3s_etcd_datastore))
success_msg: "Control plane configuration is valid." 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." 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 and k3s_control_node
- name: Check the conditions when embedded etcd is defined - name: Check the conditions when embedded etcd is defined
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- (k3s_controller_count | length >= 3) - (k3s_controller_list | length >= 3)
and (((k3s_controller_count | length) % 2) == 1) and (((k3s_controller_list | length) % 2) == 1)
success_msg: "Control plane configuration is valid." 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" 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 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: ansible.builtin.assert:
that: that:
- k3s_use_experimental is defined and k3s_use_experimental - k3s_use_experimental is defined and k3s_use_experimental
success_msg: "Experimental variables are defined and enabled." 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 when: k3s_etcd_datastore is defined and k3s_etcd_datastore
and (k3s_release_version | replace('v', '')) is version_compare("1.19.5", '<') and (k3s_release_version | replace('v', '')) is version_compare("1.19.5", '<')
@ -14,7 +16,12 @@
that: that:
- k3s_use_experimental is defined and k3s_use_experimental - k3s_use_experimental is defined and k3s_use_experimental
success_msg: "Experimental variables are defined and enabled." 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 }}" loop: "{{ k3s_experimental_config }}"
when: (item.setting in k3s_runtime_config and k3s_runtime_config[item.setting]) when: (item.setting in k3s_runtime_config and k3s_runtime_config[item.setting])
and ((item.until is not defined) or and ((item.until is not defined) or

View File

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

View File

@ -12,7 +12,12 @@
that: that:
- (item.setting not in k3s_runtime_config) - (item.setting not in k3s_runtime_config)
success_msg: "{{ item.setting }} not found in server/agent 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 }}" loop: "{{ k3s_config_exclude }}"
- name: Check configuration in k3s_server and k3s_agent for deprecated configuration - name: Check configuration in k3s_server and k3s_agent for deprecated configuration
@ -20,7 +25,12 @@
that: that:
- (item.setting not in k3s_runtime_config) - (item.setting not in k3s_runtime_config)
success_msg: "{{ item.setting }} not found in server/agent 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 }}" loop: "{{ k3s_deprecated_config }}"
when: (item.when is not defined when: (item.when is not defined
or (item.when is defined and (k3s_release_version | replace('v', '')) is version_compare(item.when, '>='))) or (item.when is defined and (k3s_release_version | replace('v', '')) is version_compare(item.when, '>=')))
@ -31,7 +41,12 @@
that: that:
- (k3s_release_version | replace('v', '')) is version_compare(item.version, '>=') - (k3s_release_version | replace('v', '')) is version_compare(item.version, '>=')
success_msg: "{{ item.setting }} is supported by {{ k3s_release_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 }}" loop: "{{ k3s_config_version_check }}"
when: k3s_config_version_check is defined when: k3s_config_version_check is defined
and item.setting in k3s_runtime_config and item.setting in k3s_runtime_config

View File

@ -4,7 +4,7 @@
ansible.builtin.setup: ansible.builtin.setup:
- name: Ensure Ansible version is captured - name: Ensure Ansible version is captured
command: ansible --version ansible.builtin.command: ansible --version
failed_when: false failed_when: false
changed_when: false changed_when: false
register: check_ansible_version register: check_ansible_version
@ -13,7 +13,7 @@
become: false become: false
- name: Ensure Ansible config is captured - name: Ensure Ansible config is captured
command: ansible-config dump --only-changed ansible.builtin.command: ansible-config dump --only-changed
failed_when: false failed_when: false
changed_when: false changed_when: false
register: check_ansible_config register: check_ansible_config
@ -22,7 +22,7 @@
become: false become: false
- name: Ensure a list of roles is captured - name: Ensure a list of roles is captured
command: ansible-galaxy role list ansible.builtin.command: ansible-galaxy role list
failed_when: false failed_when: false
changed_when: false changed_when: false
register: check_ansible_roles register: check_ansible_roles
@ -32,7 +32,7 @@
- name: Ensure facts are written to disk - name: Ensure facts are written to disk
ansible.builtin.copy: ansible.builtin.copy:
dest: pyratlabs-issue-dump.txt dest: "{{ playbook_dir }}/pyratlabs-issue-dump.txt"
content: | content: |
# Begin ANSIBLE VERSION # Begin ANSIBLE VERSION
{{ check_ansible_version.stdout }} {{ check_ansible_version.stdout }}
@ -53,8 +53,10 @@
# Begin K3S ROLE CONFIG # Begin K3S ROLE CONFIG
{% for host in ansible_play_hosts_all %} {% for host in ansible_play_hosts_all %}
## {{ host }} ## {{ 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 }} {{ config_key }}: {{ hostvars[host][config_key] | to_json }}
{% endif %}
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
@ -63,7 +65,9 @@
# Begin K3S RUNTIME CONFIG # Begin K3S RUNTIME CONFIG
{% for host in ansible_play_hosts_all %} {% for host in ansible_play_hosts_all %}
## {{ host }} ## {{ host }}
{% if hostvars[host].k3s_runtime_config is defined %}
{{ hostvars[host].k3s_runtime_config }} {{ hostvars[host].k3s_runtime_config }}
{% endif %}
{% endfor %} {% endfor %}
# End K3S RUNTIME CONFIG # End K3S RUNTIME CONFIG
mode: 0600 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) when: ("rootless" in k3s_runtime_config)
and k3s_runtime_config.rootless 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 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: ansible.builtin.assert:
that: that:
- k3s_state in k3s_valid_states - k3s_state in k3s_valid_states
@ -8,10 +8,16 @@
success_msg: "k3s_state is valid." success_msg: "k3s_state is valid."
when: k3s_state is defined when: k3s_state is defined
- import_tasks: generate-check-config.yml - import_tasks: environment/local/packages.yml
- import_tasks: check-environment.yml
when: not k3s_skip_validation 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 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 - 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 changed_when: false
failed_when: kubectl_get_nodes_result.stdout.find("was refused") != -1 or failed_when: kubectl_get_nodes_result.stdout.find("was refused") != -1 or
kubectl_get_nodes_result.stdout.find("ServiceUnavailable") != -1 kubectl_get_nodes_result.stdout.find("ServiceUnavailable") != -1

View File

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

View File

@ -37,7 +37,7 @@ for bin in {{ k3s_install_dir }}/k3s*; do
fi fi
done done
rm -rf {{ k3s_config_file | dirname }} rm -rf {{ k3s_config_dir }}
rm -rf /run/k3s rm -rf /run/k3s
rm -rf /run/flannel rm -rf /run/flannel
rm -rf {{ k3s_runtime_config['data-dir'] | default(k3s_data_dir) }} 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 %} {% if k3s_control_node %}
server 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) %} {% 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 }} --token-file {{ k3s_token_location }}
{% endif %} {% endif %}
{% if k3s_server is defined %} {% if k3s_server is defined %}
@ -25,7 +25,7 @@ ExecStart={{ k3s_install_dir }}/k3s
{% endif %} {% endif %}
{% else %} {% else %}
agent 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 }} --token-file {{ k3s_token_location }}
{% if k3s_agent is defined %} {% if k3s_agent is defined %}
--config {{ k3s_config_file }} --config {{ k3s_config_file }}

View File

@ -1,7 +1,8 @@
--- ---
# Minimum supported version # Minimum supported versions
k3s_min_version: 1.19.3 k3s_min_version: 1.19.3
k3s_ansible_min_version: 2.10.4
# Valid states for this role # Valid states for this role
k3s_valid_states: k3s_valid_states:
@ -43,17 +44,19 @@ k3s_arch_lookup:
# Always default to stable channel, this will change with k3s_release_version # Always default to stable channel, this will change with k3s_release_version
k3s_release_channel: stable 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 updates API
k3s_api_releases: https://update.k3s.io/v1-release/channels k3s_api_releases: https://update.k3s.io/v1-release/channels
# Download location for releases # Download location for releases
k3s_github_download_url: "{{ k3s_github_url }}/releases/download" 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 # 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 # Default to the "system" systemd context, this will be "user" when running rootless
k3s_systemd_context: system k3s_systemd_context: system
@ -62,16 +65,37 @@ k3s_systemd_context: system
# management, this should live in /etc/systemd, not /lib/systemd # management, this should live in /etc/systemd, not /lib/systemd
k3s_systemd_unit_dir: "/etc/systemd/{{ k3s_systemd_context }}" 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 # 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 # Path for additional Kubernetes Manifests
# https://rancher.com/docs/k3s/latest/en/advanced/#auto-deploying-manifests # https://rancher.com/docs/k3s/latest/en/advanced/#auto-deploying-manifests
k3s_server_manifests_dir: "{{ k3s_data_dir }}/server/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 # Config items that should not appear in k3s_server or k3s_agent
k3s_config_exclude: k3s_config_exclude:
- setting: server - setting: server