From d435bf1d26b8439954f3f7e9257adabd935fdeb3 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 6 Dec 2023 21:36:16 +0100 Subject: [PATCH] Use community.library_inventory_filtering_v1 collection. --- .github/workflows/ansible-test.yml | 15 ++-- .github/workflows/docs-pr.yml | 1 + .github/workflows/docs-push.yml | 1 + galaxy.yml | 2 + plugins/doc_fragments/inventory_filter.py | 36 --------- plugins/inventory/docker_containers.py | 4 +- plugins/inventory/docker_machine.py | 4 +- plugins/inventory/docker_swarm.py | 4 +- plugins/plugin_utils/inventory_filter.py | 91 ----------------------- tests/integration/requirements.yml | 3 +- tests/unit/requirements.yml | 1 + 11 files changed, 21 insertions(+), 141 deletions(-) delete mode 100644 plugins/doc_fragments/inventory_filter.py delete mode 100644 plugins/plugin_utils/inventory_filter.py diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index 366ccdac4..029c8b0ba 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -50,6 +50,8 @@ jobs: coverage: ${{ github.event_name == 'schedule' && 'always' || 'never' }} pull-request-change-detection: 'true' testing-type: sanity + pre-test-cmd: >- + git clone --depth=1 --single-branch --branch stable-1 https://github.com/ansible-collections/community.library_inventory_filtering.git ../../community/library_inventory_filtering_v1 units: # Ansible-test on various stable branches does not yet work well with cgroups v2. @@ -72,9 +74,7 @@ jobs: - '2.13' steps: - - name: >- - Perform unit testing against - Ansible version ${{ matrix.ansible }} + - name: Perform unit testing against Ansible version ${{ matrix.ansible }} uses: felixfontein/ansible-test-gh-action@main with: ansible-core-github-repository-slug: ${{ contains(fromJson('["2.10", "2.11"]'), matrix.ansible) && 'felixfontein/ansible' || 'ansible/ansible' }} @@ -82,6 +82,8 @@ jobs: coverage: ${{ github.event_name == 'schedule' && 'always' || 'never' }} pull-request-change-detection: 'true' testing-type: units + pre-test-cmd: >- + git clone --depth=1 --single-branch --branch stable-1 https://github.com/ansible-collections/community.library_inventory_filtering.git ../../community/library_inventory_filtering_v1 integration: # Ansible-test on various stable branches does not yet work well with cgroups v2. @@ -182,10 +184,7 @@ jobs: target: azp/5/ steps: - - name: >- - Perform integration testing against - Ansible version ${{ matrix.ansible }} - under Python ${{ matrix.python }} + - name: Perform integration testing against Ansible version ${{ matrix.ansible }} under Python ${{ matrix.python }} uses: felixfontein/ansible-test-gh-action@main with: ansible-core-github-repository-slug: ${{ contains(fromJson('["2.10", "2.11"]'), matrix.ansible) && 'felixfontein/ansible' || 'ansible/ansible' }} @@ -203,6 +202,8 @@ jobs: git clone --depth=1 --single-branch https://github.com/ansible-collections/community.crypto.git ../../community/crypto ; git clone --depth=1 --single-branch https://github.com/ansible-collections/community.general.git ../../community/general + ; + git clone --depth=1 --single-branch --branch stable-1 https://github.com/ansible-collections/community.library_inventory_filtering.git ../../community/library_inventory_filtering_v1 ${{ matrix.extra-constraints && format('; echo ''{0}'' >> tests/utils/constraints.txt', matrix.extra-constraints) || '' }} ; cat tests/utils/constraints.txt diff --git a/.github/workflows/docs-pr.yml b/.github/workflows/docs-pr.yml index c63bdbfc6..74aad8825 100644 --- a/.github/workflows/docs-pr.yml +++ b/.github/workflows/docs-pr.yml @@ -32,6 +32,7 @@ jobs: init-extra-html-theme-options: | documentation_home_url=https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/branch/main/ render-file-line: '> * `$` [$](https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr/${{ github.event.number }}/$)' + extra-collections: community.library_inventory_filtering_v1 publish-docs-gh-pages: # for now we won't run this on forks diff --git a/.github/workflows/docs-push.yml b/.github/workflows/docs-push.yml index ccc320850..8ed0d874b 100644 --- a/.github/workflows/docs-push.yml +++ b/.github/workflows/docs-push.yml @@ -37,6 +37,7 @@ jobs: init-html-short-title: Community.Docker Collection Docs init-extra-html-theme-options: | documentation_home_url=https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/branch/main/ + extra-collections: community.library_inventory_filtering_v1 publish-docs-gh-pages: # for now we won't run this on forks diff --git a/galaxy.yml b/galaxy.yml index b7c1b87fd..411588cd7 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -18,6 +18,8 @@ license: #license_file: COPYING tags: - docker +dependencies: + community.library_inventory_filtering_v1: '>=0.1.0' repository: https://github.com/ansible-collections/community.docker #documentation: https://github.com/ansible-collection-migration/community.REPO_NAME/tree/main/docs homepage: https://github.com/ansible-collections/community.docker diff --git a/plugins/doc_fragments/inventory_filter.py b/plugins/doc_fragments/inventory_filter.py deleted file mode 100644 index 64ce2fa5f..000000000 --- a/plugins/doc_fragments/inventory_filter.py +++ /dev/null @@ -1,36 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright (c) Ansible Project -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - - -class ModuleDocFragment(object): - - # Docker doc fragment - DOCUMENTATION = r''' -options: - filters: - description: - - A list of include/exclude filters that allows to select/deselect hosts for this inventory. - - Filters are processed sequentially until the first filter where O(filters[].exclude) or - O(filters[].include) matches is found. In case O(filters[].exclude) matches, the host is - excluded, and in case O(filters[].include) matches, the host is included. In case no filter - matches, the host is included. - type: list - elements: dict - suboptions: - exclude: - description: - - A Jinja2 condition. If it matches for a host, that host is B(excluded). - - Exactly one of O(filters[].exclude) and O(filters[].include) can be specified. - type: str - include: - description: - - A Jinja2 condition. If it matches for a host, that host is B(included). - - Exactly one of O(filters[].exclude) and O(filters[].include) can be specified. - type: str -''' diff --git a/plugins/inventory/docker_containers.py b/plugins/inventory/docker_containers.py index 023df62ed..e80534e2f 100644 --- a/plugins/inventory/docker_containers.py +++ b/plugins/inventory/docker_containers.py @@ -21,7 +21,7 @@ extends_documentation_fragment: - ansible.builtin.constructed - community.docker.docker.api_documentation - - community.docker.inventory_filter + - community.library_inventory_filtering_v1.inventory_filter description: - Reads inventories from the Docker API. - Uses a YAML configuration file that ends with C(docker.[yml|yaml]). @@ -179,7 +179,7 @@ ) from ansible_collections.community.docker.plugins.module_utils._api.errors import APIError, DockerException -from ansible_collections.community.docker.plugins.plugin_utils.inventory_filter import parse_filters, filter_host +from ansible_collections.community.library_inventory_filtering_v1.plugins.plugin_utils.inventory_filter import parse_filters, filter_host MIN_DOCKER_API = None diff --git a/plugins/inventory/docker_machine.py b/plugins/inventory/docker_machine.py index 1987faa88..82c9eb7b2 100644 --- a/plugins/inventory/docker_machine.py +++ b/plugins/inventory/docker_machine.py @@ -14,7 +14,7 @@ - L(Docker Machine,https://docs.docker.com/machine/) extends_documentation_fragment: - ansible.builtin.constructed - - community.docker.inventory_filter + - community.library_inventory_filtering_v1.inventory_filter description: - Get inventory hosts from Docker Machine. - Uses a YAML configuration file that ends with docker_machine.(yml|yaml). @@ -97,7 +97,7 @@ from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable from ansible.utils.display import Display -from ansible_collections.community.docker.plugins.plugin_utils.inventory_filter import parse_filters, filter_host +from ansible_collections.community.library_inventory_filtering_v1.plugins.plugin_utils.inventory_filter import parse_filters, filter_host import json import re diff --git a/plugins/inventory/docker_swarm.py b/plugins/inventory/docker_swarm.py index 28a5c09a8..6f94b2df6 100644 --- a/plugins/inventory/docker_swarm.py +++ b/plugins/inventory/docker_swarm.py @@ -18,7 +18,7 @@ - L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 1.10.0 extends_documentation_fragment: - ansible.builtin.constructed - - community.docker.inventory_filter + - community.library_inventory_filtering_v1.inventory_filter description: - Reads inventories from the Docker swarm API. - Uses a YAML configuration file docker_swarm.[yml|yaml]. @@ -158,7 +158,7 @@ from ansible.plugins.inventory import BaseInventoryPlugin, Constructable from ansible.parsing.utils.addresses import parse_address -from ansible_collections.community.docker.plugins.plugin_utils.inventory_filter import parse_filters, filter_host +from ansible_collections.community.library_inventory_filtering_v1.plugins.plugin_utils.inventory_filter import parse_filters, filter_host try: import docker diff --git a/plugins/plugin_utils/inventory_filter.py b/plugins/plugin_utils/inventory_filter.py deleted file mode 100644 index 460dc208e..000000000 --- a/plugins/plugin_utils/inventory_filter.py +++ /dev/null @@ -1,91 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright (c) Ansible Project -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -from ansible.errors import AnsibleError, AnsibleParserError -from ansible.module_utils.common._collections_compat import Mapping -from ansible.module_utils.common.text.converters import to_native -from ansible.module_utils.parsing.convert_bool import boolean -from ansible.module_utils.six import string_types - - -_ALLOWED_KEYS = ('include', 'exclude') - - -def parse_filters(filters): - """ - Parse get_option('filter') and return normalized version to be fed into filter_host(). - """ - result = [] - if filters is None: - return result - for index, filter in enumerate(filters): - if not isinstance(filter, Mapping): - raise AnsibleError('filter[{index}] must be a dictionary'.format( - index=index + 1, - )) - if len(filter) != 1: - raise AnsibleError('filter[{index}] must have exactly one key-value pair'.format( - index=index + 1, - )) - key, value = list(filter.items())[0] - if key not in _ALLOWED_KEYS: - raise AnsibleError('filter[{index}] must have a {allowed_keys} key, not "{key}"'.format( - index=index + 1, - key=key, - allowed_keys=' or '.join('"{key}"'.format(key=key) for key in _ALLOWED_KEYS), - )) - if not isinstance(value, (string_types, bool)): - raise AnsibleError('filter[{index}].{key} must be a string, not {value_type}'.format( - index=index + 1, - key=key, - value_type=type(value), - )) - result.append(filter) - return result - - -def filter_host(inventory_plugin, host, host_vars, filters): - """ - Determine whether a host should be accepted (``True``) or not (``False``). - """ - vars = { - 'inventory_hostname': host, - } - if host_vars: - vars.update(host_vars) - - def evaluate(condition): - if isinstance(condition, bool): - return condition - conditional = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % condition - templar = inventory_plugin.templar - old_vars = templar.available_variables - try: - templar.available_variables = vars - return boolean(templar.template(conditional)) - except Exception as e: - raise AnsibleParserError("Could not evaluate filter condition {condition!r} for host {host}: {err}".format( - host=host, - condition=condition, - err=to_native(e), - )) - finally: - templar.available_variables = old_vars - - for filter in filters: - if 'include' in filter: - expr = filter['include'] - if evaluate(expr): - return True - if 'exclude' in filter: - expr = filter['exclude'] - if evaluate(expr): - return False - - return True diff --git a/tests/integration/requirements.yml b/tests/integration/requirements.yml index 7b3e38d98..b4c045f43 100644 --- a/tests/integration/requirements.yml +++ b/tests/integration/requirements.yml @@ -5,6 +5,7 @@ collections: - ansible.posix -- community.internal_test_tools - community.crypto - community.general +- community.internal_test_tools +- community.library_inventory_filtering_v1 diff --git a/tests/unit/requirements.yml b/tests/unit/requirements.yml index 586a6a1b3..e91ea4745 100644 --- a/tests/unit/requirements.yml +++ b/tests/unit/requirements.yml @@ -5,3 +5,4 @@ collections: - community.internal_test_tools +- community.library_inventory_filtering_v1