Skip to content

Instantly share code, notes, and snippets.

@gtirloni
Last active July 28, 2022 00:01
Show Gist options
  • Save gtirloni/0725c985e53e3515332ac43ad1ee3b0b to your computer and use it in GitHub Desktop.
Save gtirloni/0725c985e53e3515332ac43ad1ee3b0b to your computer and use it in GitHub Desktop.
ansible_version.py

ansible_version.py

This is a plugin that verifies if the Ansible version meets the version requirements.

You can use the same range identifiers from Ansible Galaxy, separate by commas:

*: The most recent version. This is the default.
!=: Not equal to the version specified.
==: Exactly the version specified.
>=: Greater than or equal to the version specified.
>: Greater than the version specified.
<=: Less than or equal to the version specified.
<: Less than the version specified.

The plugin has been tested only with ansible-core 2.12 and certainly will not work with ansible 2.9.

Getting started

Download the ansible_version.py file into your callbacks_plugins folder.

You can either configure the version_requirements parameter in ansible.cfg defaults section or set the ANSIBLE_VERSION_REQUIREMENTS environment variable.

[defaults]
version_requirements = ">=2.12,<2.13"
callbacks_enabled = ansible_version
bin_ansible_callbacks = True # optional

or

$ ANSIBLE_VERSION_REQUIREMENTS=">=2.12,<2.13" ansible-playbook playbook.yml

The bin_ansible_callbacks is only necessary if you want it to work with adhoc ansible commands (besides ansible-playbook).

# Copyright (c) Giovanni Tirloni
# GNU General Public License v3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
callback_name: ansible_version
callback_type: notification
short_description: Allow specification of Ansible version requirements
options:
version_requirements:
description: abc
type: string
required: False
env:
- name: ANSIBLE_VERSION_REQUIREMENTS
ini:
- section: defaults
key: version_requirements
"""
import sys
from ansible import constants as C
from ansible.errors import AnsibleError
from ansible.galaxy.collection.galaxy_api_proxy import MultiGalaxyAPIProxy
from ansible.galaxy.dependency_resolution import build_collection_dependency_resolver
from ansible.galaxy.dependency_resolution.versioning import meets_requirements
from ansible.plugins.callback import CallbackBase
from ansible.release import __version__ as ansible_version
from ansible.utils.display import Display
display = Display()
class CallbackModule(CallbackBase):
"""
Verify if Ansible version meets version requirements
"""
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = "notification"
CALLBACK_NAME = "ansible_version"
CALLBACK_NEEDS_ENABLED = True
def __init__(self):
super(CallbackModule, self).__init__()
self.set_options()
current_version = ".".join(ansible_version.split(".")[:3])
version_requirements = self.get_option("version_requirements")
if version_requirements:
if not meets_requirements(current_version, version_requirements):
display.error(
f"Ansible version {current_version} does not satisfy version requirements ({version_requirements}), check your configuration."
)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment