diff --git a/README.md b/README.md index 33a4e02..6710372 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ consistency. | Variable | Description | Default Value | |--------------------------------|--------------------------------------------------------------------------|--------------------------------| +| `k3s_cluster_state` | State of cluster, options: installed, started, stopped, restarted. | installed | | `k3s_release_version` | Use a specific version of k3s, eg. `v0.2.0`. Specify `false` for latest. | `false` | | `k3s_github_url` | Set the GitHub URL to install k3s from. | https://github.com/rancher/k3s | | `k3s_install_dir` | Installation directory for k3s. | `/usr/local/bin` | diff --git a/defaults/main.yml b/defaults/main.yml index 8cb7556..031c193 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,9 @@ --- +# k3s cluster state, options: installed, started, stopped, restarted +# (default: installed) +k3s_cluster_state: installed + # Use a specific k3s version, if set to "false" we will get the latest # k3s_release_version: v0.1.0 k3s_release_version: false diff --git a/molecule/default/playbook-restart-cluster.yml b/molecule/default/playbook-restart-cluster.yml new file mode 100644 index 0000000..d89782b --- /dev/null +++ b/molecule/default/playbook-restart-cluster.yml @@ -0,0 +1,9 @@ +--- +- name: Converge + hosts: all + become: true + vars: + molecule_is_test: true + k3s_cluster_state: restarted + roles: + - role: xanmanning.k3s diff --git a/molecule/default/playbook-start-cluster.yml b/molecule/default/playbook-start-cluster.yml new file mode 100644 index 0000000..e206333 --- /dev/null +++ b/molecule/default/playbook-start-cluster.yml @@ -0,0 +1,9 @@ +--- +- name: Converge + hosts: all + become: true + vars: + molecule_is_test: true + k3s_cluster_state: started + roles: + - role: xanmanning.k3s diff --git a/molecule/default/playbook-stop-cluster.yml b/molecule/default/playbook-stop-cluster.yml new file mode 100644 index 0000000..477b431 --- /dev/null +++ b/molecule/default/playbook-stop-cluster.yml @@ -0,0 +1,9 @@ +--- +- name: Converge + hosts: all + become: true + vars: + molecule_is_test: true + k3s_cluster_state: stopped + roles: + - role: xanmanning.k3s diff --git a/tasks/configure-k3s-cluster.yml b/tasks/build/configure-k3s-cluster.yml similarity index 100% rename from tasks/configure-k3s-cluster.yml rename to tasks/build/configure-k3s-cluster.yml diff --git a/tasks/download-k3s.yml b/tasks/build/download-k3s.yml similarity index 100% rename from tasks/download-k3s.yml rename to tasks/build/download-k3s.yml diff --git a/tasks/get-version.yml b/tasks/build/get-version.yml similarity index 100% rename from tasks/get-version.yml rename to tasks/build/get-version.yml diff --git a/tasks/install-docker-amazon.yml b/tasks/build/install-docker-amazon.yml similarity index 100% rename from tasks/install-docker-amazon.yml rename to tasks/build/install-docker-amazon.yml diff --git a/tasks/install-docker-opensuse-leap.yml b/tasks/build/install-docker-opensuse-leap.yml similarity index 100% rename from tasks/install-docker-opensuse-leap.yml rename to tasks/build/install-docker-opensuse-leap.yml diff --git a/tasks/install-docker-prerequisites-debian.yml b/tasks/build/install-docker-prerequisites-debian.yml similarity index 100% rename from tasks/install-docker-prerequisites-debian.yml rename to tasks/build/install-docker-prerequisites-debian.yml diff --git a/tasks/install-docker-prerequisites-redhat.yml b/tasks/build/install-docker-prerequisites-redhat.yml similarity index 100% rename from tasks/install-docker-prerequisites-redhat.yml rename to tasks/build/install-docker-prerequisites-redhat.yml diff --git a/tasks/install-docker-prerequisites-suse.yml b/tasks/build/install-docker-prerequisites-suse.yml similarity index 100% rename from tasks/install-docker-prerequisites-suse.yml rename to tasks/build/install-docker-prerequisites-suse.yml diff --git a/tasks/install-docker-suse.yml b/tasks/build/install-docker-suse.yml similarity index 100% rename from tasks/install-docker-suse.yml rename to tasks/build/install-docker-suse.yml diff --git a/tasks/install-docker.yml b/tasks/build/install-docker.yml similarity index 100% rename from tasks/install-docker.yml rename to tasks/build/install-docker.yml diff --git a/tasks/install-k3s.yml b/tasks/build/install-k3s.yml similarity index 100% rename from tasks/install-k3s.yml rename to tasks/build/install-k3s.yml diff --git a/tasks/main.yml b/tasks/main.yml index 4135545..9a81157 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,27 +1,11 @@ --- -- import_tasks: preconfigure-k3s.yml +- name: Check to see if k3s_cluster_state is a supported value + assert: + that: + - k3s_cluster_state in ['installed', 'started', 'stopped', 'restarted'] + fail_msg: "k3s_cluster_state not valid. Check README.md for details." + success_msg: "k3s_cluster_state is valid." + when: k3s_cluster_state is defined -- include_tasks: install-docker-prerequisites-{{ ansible_os_family | lower }}.yml - when: k3s_use_docker - and ((k3s_control_workers) - or (not k3s_control_workers and not k3s_control_node)) - -- import_tasks: install-docker.yml - when: k3s_use_docker - and ((k3s_control_workers) - or (not k3s_control_workers and not k3s_control_node)) - and ansible_distribution | replace(" ", "-") | lower not in ['amazon', 'suse', 'opensuse-leap'] - -- include_tasks: install-docker-{{ ansible_distribution | replace(" ", "-") | lower }}.yml - when: ansible_distribution | replace(" ", "-") | lower in ['amazon', 'suse', 'opensuse-leap'] - -- import_tasks: get-version.yml - when: k3s_release_version is not defined or not k3s_release_version - -- import_tasks: download-k3s.yml - -- import_tasks: install-k3s.yml - -- import_tasks: configure-k3s-cluster.yml - when: play_hosts | length > 1 +- import_tasks: state-{{ (k3s_cluster_state | lower) | default('installed') }}.yml diff --git a/tasks/operate/start-k3s.yml b/tasks/operate/start-k3s.yml new file mode 100644 index 0000000..2a7d9d1 --- /dev/null +++ b/tasks/operate/start-k3s.yml @@ -0,0 +1,7 @@ +--- + +- name: Ensure k3s service is started + service: + name: k3s + state: started + enabled: true diff --git a/tasks/operate/stop-k3s.yml b/tasks/operate/stop-k3s.yml new file mode 100644 index 0000000..8da7c32 --- /dev/null +++ b/tasks/operate/stop-k3s.yml @@ -0,0 +1,7 @@ +--- + +- name: Ensure k3s service is stopped + service: + name: k3s + state: stopped + enabled: false diff --git a/tasks/state-installed.yml b/tasks/state-installed.yml new file mode 100644 index 0000000..e81ee11 --- /dev/null +++ b/tasks/state-installed.yml @@ -0,0 +1,27 @@ +--- + +- import_tasks: preconfigure-k3s.yml + +- include_tasks: build/install-docker-prerequisites-{{ ansible_os_family | lower }}.yml + when: k3s_use_docker + and ((k3s_control_workers) + or (not k3s_control_workers and not k3s_control_node)) + +- import_tasks: build/install-docker.yml + when: k3s_use_docker + and ((k3s_control_workers) + or (not k3s_control_workers and not k3s_control_node)) + and ansible_distribution | replace(" ", "-") | lower not in ['amazon', 'suse', 'opensuse-leap'] + +- include_tasks: build/install-docker-{{ ansible_distribution | replace(" ", "-") | lower }}.yml + when: ansible_distribution | replace(" ", "-") | lower in ['amazon', 'suse', 'opensuse-leap'] + +- import_tasks: build/get-version.yml + when: k3s_release_version is not defined or not k3s_release_version + +- import_tasks: build/download-k3s.yml + +- import_tasks: build/install-k3s.yml + +- import_tasks: build/configure-k3s-cluster.yml + when: play_hosts | length > 1 diff --git a/tasks/state-restarted.yml b/tasks/state-restarted.yml new file mode 100644 index 0000000..aaa5d76 --- /dev/null +++ b/tasks/state-restarted.yml @@ -0,0 +1,4 @@ +--- + +- import_tasks: operate/stop-k3s.yml +- import_tasks: operate/start-k3s.yml diff --git a/tasks/state-started.yml b/tasks/state-started.yml new file mode 100644 index 0000000..5441988 --- /dev/null +++ b/tasks/state-started.yml @@ -0,0 +1,3 @@ +--- + +- import_tasks: operate/start-k3s.yml diff --git a/tasks/state-stopped.yml b/tasks/state-stopped.yml new file mode 100644 index 0000000..af1e1bd --- /dev/null +++ b/tasks/state-stopped.yml @@ -0,0 +1,3 @@ +--- + +- import_tasks: operate/stop-k3s.yml