Skip to content

Commit

Permalink
feat: Add Ansible/Jinja2/Collections validation (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessfg committed Jul 29, 2024
1 parent 935472f commit 6e1a074
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
45 changes: 45 additions & 0 deletions tasks/validate/validate.yml
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
---
# Set the minimum version required for Ansible and Jinja2
nginx_config_ansible_version: 2.16
nginx_config_jinja2_version: 3.1

0 comments on commit 6e1a074

Please sign in to comment.