From 6e1a074632be262c0e95acee1a5f079fa2ffb84d Mon Sep 17 00:00:00 2001 From: Alessandro Fael Garcia Date: Mon, 29 Jul 2024 03:32:04 -0800 Subject: [PATCH] feat: Add Ansible/Jinja2/Collections validation (#456) --- CHANGELOG.md | 1 + tasks/main.yml | 4 ++++ tasks/validate/validate.yml | 45 +++++++++++++++++++++++++++++++++++++ vars/main.yml | 3 +++ 4 files changed, 53 insertions(+) create mode 100644 tasks/validate/validate.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 378bfa2c..6dacfba3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ FEATURES: +- Add validation tasks to check the Ansible version, the Jinja2 version, and whether the required Ansible collections for this role are installed. - Bump the Ansible `community.general` collection to `9.2.0`, `community.crypto` collection to `2.21.1` and `community.docker` collection to `3.11.0`. BUG FIXES: diff --git a/tasks/main.yml b/tasks/main.yml index 8682ccb6..3edbf1bd 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,4 +1,8 @@ --- +- name: Validate Ansible/Jinja2 version and Ansible collections + ansible.builtin.include_tasks: "{{ role_path }}/tasks/validate/validate.yml" + tags: nginx_config_validate + - name: Set up SELinux ansible.builtin.include_tasks: "{{ role_path }}/tasks/prerequisites/setup-selinux.yml" when: diff --git a/tasks/validate/validate.yml b/tasks/validate/validate.yml new file mode 100644 index 00000000..b9694f06 --- /dev/null +++ b/tasks/validate/validate.yml @@ -0,0 +1,45 @@ +--- +- name: Verify you are using a supported Ansible version on your Ansible host + ansible.builtin.assert: + that: ansible_version['full'] is version(nginx_config_ansible_version, '>=') + success_msg: Ansible {{ ansible_version['full'] }} is supported. + fail_msg: Ansible {{ ansible_version['full'] }} has reached End of Life (EoL). Please upgrade to a supported Ansible release. Check the README for more details. + delegate_to: localhost + ignore_errors: true # noqa ignore-errors + +- name: Extract the version of Jinja2 installed on your Ansible host + ansible.builtin.command: ansible --version + register: jinja2_version + changed_when: false + delegate_to: localhost + become: false + +- name: Verify that you are using a supported Jinja2 version on your Ansible host + ansible.builtin.assert: + that: (jinja2_version['stdout'] | regex_search('jinja version = ([\\d.]+)', '\\1') | first) is version(nginx_config_jinja2_version, '>=') + success_msg: Jinja2 {{ jinja2_version['stdout'] | regex_search('jinja version = ([\d.]+)', '\1') | first }} is supported. + fail_msg: Jinja2 {{ jinja2_version['stdout'] | regex_search('jinja version = ([\d.]+)', '\1') | first }} is not supported. Please upgrade to Jinja2 3.1. Check the README for more details. + delegate_to: localhost + become: false + +- name: Verify that the 'community.general' and 'ansible.posix' Ansible collections are installed on your Ansible host + when: nginx_config_selinux | bool + delegate_to: localhost + become: false + block: + - name: Extract the list of Ansible collections installed on your Ansible host + ansible.builtin.command: ansible-galaxy collection list + register: collection_list + changed_when: false + + - name: Verify that the 'community.general' Ansible collection is installed on your Ansible host + ansible.builtin.assert: + that: collection_list is search('community.general') + success_msg: The 'community.general' Ansible collection is installed. + fail_msg: The 'community.general' Ansible collection is not installed. Please install the 'community.general' Ansible collection. Check the README for more details. + + - name: Verify that the 'ansible.posix' Ansible collection is installed on your Ansible host + ansible.builtin.assert: + that: lookup('community.general.collection_version', 'ansible.posix') != 'none' + success_msg: The 'ansible.posix' Ansible collection is installed. + fail_msg: The 'ansible.posix' Ansible collection is not installed. Please install the 'ansible.posix' Ansible collection. Check the README for more details. diff --git a/vars/main.yml b/vars/main.yml index ed97d539..410ec43b 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1 +1,4 @@ --- +# Set the minimum version required for Ansible and Jinja2 +nginx_config_ansible_version: 2.16 +nginx_config_jinja2_version: 3.1