From 8bc60e42a87212ca5a253d480a91e2341f887c86 Mon Sep 17 00:00:00 2001 From: julian Date: Sat, 8 Jul 2023 19:58:02 +0200 Subject: [PATCH] Extend distribution_check role to account for Ansible changes reg. facts Somewhere between ansible [core 2.14.4] and ansible [core 2.15.0] the logic for the distribution_version Ansible fact got changed. With the newer Ansible version Debians distribution_version gets reported as 11.7 as opposed to getting reported as 11 with the old Ansible version. To still allow for useful distribution checks, extend the distribution_check role by allowing the specification of distribution_major_versions and distribution_releases as well. This way you can check for an Ubuntu version by using distribution_version (which for example resolves to 18.04, while distribution_major_version would resolve to 18 in that case) and check for a Debian version by using distribution_major_version (which for example resolves to 11, while distribution_version would resolve to 11.7 in that case). --- .../roles/bluetooth_audio_sink/meta/main.yaml | 2 +- playbooks/roles/cert/meta/main.yaml | 2 +- playbooks/roles/distribution_check/README.md | 13 ++++ .../meta/argument_specs.yaml | 14 ++++- .../roles/distribution_check/tasks/main.yaml | 59 +++++++++++++++---- playbooks/roles/docker/meta/main.yaml | 2 +- playbooks/roles/docker_compose/meta/main.yaml | 2 +- playbooks/roles/esphome/meta/main.yaml | 2 +- playbooks/roles/foobazdmx/meta/main.yaml | 2 +- playbooks/roles/hifiberry/meta/main.yaml | 2 +- playbooks/roles/mosquitto/meta/main.yaml | 2 +- playbooks/roles/nginx/meta/main.yaml | 2 +- playbooks/roles/nodejs/meta/main.yaml | 2 +- playbooks/roles/ola/meta/main.yaml | 2 +- .../send_only_mail_server/meta/main.yaml | 2 +- playbooks/roles/shairport_sync/meta/main.yaml | 2 +- playbooks/roles/zigbee2mqtt/meta/main.yaml | 2 +- 17 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 playbooks/roles/distribution_check/README.md diff --git a/playbooks/roles/bluetooth_audio_sink/meta/main.yaml b/playbooks/roles/bluetooth_audio_sink/meta/main.yaml index 2c3bd99..146b112 100644 --- a/playbooks/roles/bluetooth_audio_sink/meta/main.yaml +++ b/playbooks/roles/bluetooth_audio_sink/meta/main.yaml @@ -4,5 +4,5 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/cert/meta/main.yaml b/playbooks/roles/cert/meta/main.yaml index f730c82..99e9559 100644 --- a/playbooks/roles/cert/meta/main.yaml +++ b/playbooks/roles/cert/meta/main.yaml @@ -3,5 +3,5 @@ dependencies: # noqa meta-no-info vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/distribution_check/README.md b/playbooks/roles/distribution_check/README.md new file mode 100644 index 0000000..38e06fc --- /dev/null +++ b/playbooks/roles/distribution_check/README.md @@ -0,0 +1,13 @@ +# Role `distribution_check` + +This role checks if the distribution of the hosts is supported (part of the provided distribution support spec.) and fails if it's not. + +If a hosts distribution and either an accompanying distribution version, major version or release is supported, the role doesn't fail for the host in question. + +## Required Arguments + +For the required arguments look at the [`argument_specs.yaml`](./meta/argument_specs.yaml). + +## `hosts` + +The `hosts` for this role need to be the machines for which you want to make sure their distribution is supported. diff --git a/playbooks/roles/distribution_check/meta/argument_specs.yaml b/playbooks/roles/distribution_check/meta/argument_specs.yaml index 2332b34..1d8bab9 100644 --- a/playbooks/roles/distribution_check/meta/argument_specs.yaml +++ b/playbooks/roles/distribution_check/meta/argument_specs.yaml @@ -1,7 +1,7 @@ argument_specs: main: options: - distribution_check__supported_distributions: + distribution_check__distribution_support_spec: description: A spec specifying the supported distribution. type: list elements: dict @@ -15,4 +15,14 @@ argument_specs: description: The supported versions of the supported distribution. type: list elements: str - required: true + required: false + major_versions: + description: The supported major versions of the supported distribution. + type: list + elements: str + required: false + releases: + description: The supported releases of the supported distribution. + type: list + elements: str + required: false diff --git a/playbooks/roles/distribution_check/tasks/main.yaml b/playbooks/roles/distribution_check/tasks/main.yaml index 5ddda9a..6522fc9 100644 --- a/playbooks/roles/distribution_check/tasks/main.yaml +++ b/playbooks/roles/distribution_check/tasks/main.yaml @@ -1,20 +1,53 @@ -- name: Set fact holding list of supported distribution names +- name: set fact holding list of supported distribution names ansible.builtin.set_fact: - distribution_check__supported_distribution_names: "{{ distribution_check__supported_distributions | community.general.json_query('[].name') }}" + distribution_check__supported_distribution_names: "{{ distribution_check__distribution_support_spec + | community.general.json_query('[].name') }}" -- name: Fail on unsupported distribution (name) +- name: fail on unsupported distribution (name) ansible.builtin.fail: msg: The hosts distribution (name) isn't supported. - when: ansible_facts.distribution not in distribution_check__supported_distribution_names + when: ansible_facts['distribution'] not in distribution_check__supported_distribution_names -- name: Set fact holding list of supported distribution versions - ansible.builtin.set_fact: - distribution_check__supported_distribution_versions: "{{ distribution_check__supported_distributions - | community.general.json_query(distribution_check__supported_distribution_versions_query) }}" - vars: - distribution_check__supported_distribution_versions_query: "[?name=='{{ ansible_facts.distribution }}'].versions | [].to_string(@)" +- name: set facts for holding lists of supported distribution versions, major versions and releases + block: + - name: set fact holding list of supported distribution versions + ansible.builtin.set_fact: + distribution_check__supported_distribution_versions: "{{ distribution_check__distribution_support_spec + | community.general.json_query(distribution_check__supported_distribution_versions_query) }}" + vars: + distribution_check__supported_distribution_versions_query: "[?name=='{{ ansible_facts['distribution'] }}'].versions | [].to_string(@)" -- name: Fail on unsupported distribution version + - name: set fact holding list of supported distribution major versions + ansible.builtin.set_fact: + distribution_check__supported_distribution_major_versions: "{{ distribution_check__distribution_support_spec + | community.general.json_query(distribution_check__supported_distribution_major_versions_query) }}" + vars: + distribution_check__supported_distribution_major_versions_query: "[?name=='{{ ansible_facts['distribution'] }}'].major_versions | [].to_string(@)" + + - name: set fact holding list of supported distribution releases + ansible.builtin.set_fact: + distribution_check__supported_distribution_releases: "{{ distribution_check__distribution_support_spec + | community.general.json_query(distribution_check__supported_distribution_releases_query) }}" + vars: + distribution_check__supported_distribution_releases_query: "[?name=='{{ ansible_facts['distribution'] }}'].releases | [].to_string(@)" + +- name: check for distribution version, major version and release support + block: + - name: set fact on whether the distribution version is supported + ansible.builtin.set_fact: + distribution_check__distribution_version_supported: "{{ ansible_facts['distribution_version'] in distribution_check__supported_distribution_versions }}" + + - name: set fact on whether the distribution major version is supported + ansible.builtin.set_fact: + distribution_check__distribution_major_version_supported: "{{ ansible_facts['distribution_major_version'] in distribution_check__supported_distribution_major_versions }}" # noqa: yaml[line-length] + + - name: set fact on whether the distribution release is supported + ansible.builtin.set_fact: + distribution_check__distribution_release_supported: "{{ ansible_facts['distribution_release'] in distribution_check__supported_distribution_releases }}" + +- name: fail, if neither the distributions version, major version or release is supported ansible.builtin.fail: - msg: The hosts distribution version isn't supported. - when: ansible_facts.distribution_version not in distribution_check__supported_distribution_versions + msg: Neither the hosts distribution version, major version or release is supported. + when: not (distribution_check__distribution_version_supported + or distribution_check__distribution_major_version_supported + or distribution_check__distribution_release_supported) diff --git a/playbooks/roles/docker/meta/main.yaml b/playbooks/roles/docker/meta/main.yaml index 633049c..cc40197 100644 --- a/playbooks/roles/docker/meta/main.yaml +++ b/playbooks/roles/docker/meta/main.yaml @@ -4,5 +4,5 @@ dependencies: vars: distribution_check__distribution_support_spec: - name: Debian - versions: + major_versions: - 11 diff --git a/playbooks/roles/docker_compose/meta/main.yaml b/playbooks/roles/docker_compose/meta/main.yaml index 8bd04af..f88ae79 100644 --- a/playbooks/roles/docker_compose/meta/main.yaml +++ b/playbooks/roles/docker_compose/meta/main.yaml @@ -4,6 +4,6 @@ dependencies: vars: distribution_check__distribution_support_spec: - name: Debian - versions: + major_versions: - 11 - role: docker diff --git a/playbooks/roles/esphome/meta/main.yaml b/playbooks/roles/esphome/meta/main.yaml index 2c3bd99..146b112 100644 --- a/playbooks/roles/esphome/meta/main.yaml +++ b/playbooks/roles/esphome/meta/main.yaml @@ -4,5 +4,5 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/foobazdmx/meta/main.yaml b/playbooks/roles/foobazdmx/meta/main.yaml index 2c3bd99..146b112 100644 --- a/playbooks/roles/foobazdmx/meta/main.yaml +++ b/playbooks/roles/foobazdmx/meta/main.yaml @@ -4,5 +4,5 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/hifiberry/meta/main.yaml b/playbooks/roles/hifiberry/meta/main.yaml index 6995b78..df87434 100644 --- a/playbooks/roles/hifiberry/meta/main.yaml +++ b/playbooks/roles/hifiberry/meta/main.yaml @@ -4,6 +4,6 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" - role: raspberry_pi_check diff --git a/playbooks/roles/mosquitto/meta/main.yaml b/playbooks/roles/mosquitto/meta/main.yaml index 2c3bd99..146b112 100644 --- a/playbooks/roles/mosquitto/meta/main.yaml +++ b/playbooks/roles/mosquitto/meta/main.yaml @@ -4,5 +4,5 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/nginx/meta/main.yaml b/playbooks/roles/nginx/meta/main.yaml index 2c3bd99..146b112 100644 --- a/playbooks/roles/nginx/meta/main.yaml +++ b/playbooks/roles/nginx/meta/main.yaml @@ -4,5 +4,5 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/nodejs/meta/main.yaml b/playbooks/roles/nodejs/meta/main.yaml index cf4a717..1b86cae 100644 --- a/playbooks/roles/nodejs/meta/main.yaml +++ b/playbooks/roles/nodejs/meta/main.yaml @@ -4,7 +4,7 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" - role: add_apt_repository vars: diff --git a/playbooks/roles/ola/meta/main.yaml b/playbooks/roles/ola/meta/main.yaml index 2c3bd99..146b112 100644 --- a/playbooks/roles/ola/meta/main.yaml +++ b/playbooks/roles/ola/meta/main.yaml @@ -4,5 +4,5 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/send_only_mail_server/meta/main.yaml b/playbooks/roles/send_only_mail_server/meta/main.yaml index f730c82..99e9559 100644 --- a/playbooks/roles/send_only_mail_server/meta/main.yaml +++ b/playbooks/roles/send_only_mail_server/meta/main.yaml @@ -3,5 +3,5 @@ dependencies: # noqa meta-no-info vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/shairport_sync/meta/main.yaml b/playbooks/roles/shairport_sync/meta/main.yaml index 2c3bd99..146b112 100644 --- a/playbooks/roles/shairport_sync/meta/main.yaml +++ b/playbooks/roles/shairport_sync/meta/main.yaml @@ -4,5 +4,5 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" diff --git a/playbooks/roles/zigbee2mqtt/meta/main.yaml b/playbooks/roles/zigbee2mqtt/meta/main.yaml index 0bb7752..49711da 100644 --- a/playbooks/roles/zigbee2mqtt/meta/main.yaml +++ b/playbooks/roles/zigbee2mqtt/meta/main.yaml @@ -4,7 +4,7 @@ dependencies: vars: distribution_check__supported_distributions: - name: Debian - versions: + major_versions: - "11" - role: nodejs vars: