From d151db8ebf352282736ec30b6edcfae1b6a7787f Mon Sep 17 00:00:00 2001 From: NaeiKinDus Date: Sun, 7 Jul 2024 00:00:00 +0000 Subject: [PATCH] feat(role): add helm role --- .../infrastructure/roles/helm/README.md | 29 +++++++++ .../roles/helm/defaults/main.yml | 3 + .../roles/helm/handlers/main.yml | 2 + .../infrastructure/roles/helm/meta/main.yml | 15 +++++ .../infrastructure/roles/helm/tasks/main.yml | 64 +++++++++++++++++++ .../infrastructure/roles/helm/tests/inventory | 2 + .../infrastructure/roles/helm/tests/test.yml | 5 ++ .../infrastructure/roles/helm/vars/main.yml | 2 + 8 files changed, 122 insertions(+) create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/helm/README.md create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/helm/defaults/main.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/helm/handlers/main.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/helm/meta/main.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/helm/tasks/main.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/helm/tests/inventory create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/helm/tests/test.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/helm/vars/main.yml diff --git a/collections/ansible_collections/nullified/infrastructure/roles/helm/README.md b/collections/ansible_collections/nullified/infrastructure/roles/helm/README.md new file mode 100644 index 0000000..324ad42 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/helm/README.md @@ -0,0 +1,29 @@ +Helm +========= + +This role handles the installation of the Helm binary and optionally supports version pining or unconditional upgrade. + +Requirements +------------ + +Only tested and used on Debian (uses the `ansible.builtin.apt` module). + +Role Variables +-------------- +### `helm_binary_path` +Path where the Helm binary is installed (no discovery is performed so pre-existing binaries will remain untouched). + +**Default value**: `helm_binary_path: /usr/local/bin/helm` + +### `helm_install_version` +Version of Helm to install. It allows multiple values: +- empty: will install the binary if not already present, else nothing is changed, +- 'vX.Y.Z': will install the specified version (e.g. `v3.15.2`), upgrading / downgrading the local binary if required, +- latest: will install the latest available version unless the local binary is already up-to-date. + +**Default value**: `helm_install_version: latest` + +License +------- + +MIT diff --git a/collections/ansible_collections/nullified/infrastructure/roles/helm/defaults/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/helm/defaults/main.yml new file mode 100644 index 0000000..82ec848 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/helm/defaults/main.yml @@ -0,0 +1,3 @@ +--- +helm_binary_path: '/usr/local/bin/helm' +helm_install_version: latest diff --git a/collections/ansible_collections/nullified/infrastructure/roles/helm/handlers/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/helm/handlers/main.yml new file mode 100644 index 0000000..fdfead9 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/helm/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for helm diff --git a/collections/ansible_collections/nullified/infrastructure/roles/helm/meta/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/helm/meta/main.yml new file mode 100644 index 0000000..5529462 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/helm/meta/main.yml @@ -0,0 +1,15 @@ +galaxy_info: + author: Florian L. + description: Install helm binary + issue_tracker_url: https://gitlab.0x2a.ninja/infrastructure/configuration + license: MIT + min_ansible_version: 2.15 + platforms: + - name: Debian + versions: + - 11 + - 12 + galaxy_tags: + - helm + - kubernetes +dependencies: [] diff --git a/collections/ansible_collections/nullified/infrastructure/roles/helm/tasks/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/helm/tasks/main.yml new file mode 100644 index 0000000..12b7ab2 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/helm/tasks/main.yml @@ -0,0 +1,64 @@ +--- +- name: install required packages + become: true + ansible.builtin.apt: + update_cache: true + force_apt_get: true + cache_valid_time: 3600 + pkg: + - curl + - jq + +- name: find if binary is already installed + ansible.builtin.file: + path: '{{ helm_binary_path }}' + register: helm_stat + changed_when: false + failed_when: false + +- name: find current installed version + when: helm_stat.state != "absent" + ansible.builtin.command: "{{ helm_binary_path }} version --template='{% raw %}{{.Version}}{% endraw %}'" + changed_when: false + register: helm_local_version_exec + +- name: find latest available version + connection: local + ansible.builtin.shell: |- + curl -sSL https://api.github.com/repos/helm/helm/releases/latest | jq -r '.tag_name' + register: latest_helm_version_exec + when: helm_install_version is falsy or helm_install_version == "latest" + changed_when: false + +- name: set helm facts + ansible.builtin.set_fact: + helm_latest_version_available: '{{ latest_helm_version_exec.get("stdout", "") if latest_helm_version_exec is defined }}' + helm_local_version: '{{ helm_local_version_exec.get("stdout", "") if helm_local_version_exec is defined }}' + helm_target_install_version: '{{ helm_install_version if helm_install_version != "latest" else latest_helm_version_exec.get("stdout", "") }}' + +- name: install binary + become: true + when: helm_stat.state == "absent" or (helm_local_version != helm_target_install_version and helm_install_version is not falsy) + block: + - name: create temporary directory + ansible.builtin.tempfile: + state: directory + register: tmp_dir + changed_when: false + - name: retrieve archive + ansible.builtin.unarchive: + remote_src: true + src: "https://get.helm.sh/helm-{{ helm_install_version if helm_install_version is not match('^$|^latest$') else helm_latest_version_available }}-linux-amd64.tar.gz" + dest: '{{ tmp_dir.path }}' + - name: install binary + ansible.builtin.copy: + remote_src: true + src: '{{ tmp_dir.path }}/linux-amd64/helm' + dest: '{{ helm_binary_path }}' + owner: root + group: root + mode: '0755' + - name: cleanup + ansible.builtin.file: + path: '{{ tmp_dir.path }}' + state: absent diff --git a/collections/ansible_collections/nullified/infrastructure/roles/helm/tests/inventory b/collections/ansible_collections/nullified/infrastructure/roles/helm/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/helm/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/collections/ansible_collections/nullified/infrastructure/roles/helm/tests/test.yml b/collections/ansible_collections/nullified/infrastructure/roles/helm/tests/test.yml new file mode 100644 index 0000000..f03690b --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/helm/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - helm diff --git a/collections/ansible_collections/nullified/infrastructure/roles/helm/vars/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/helm/vars/main.yml new file mode 100644 index 0000000..e42b129 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/helm/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for helm