Skip to content

Commit

Permalink
Address ansible 2.15 compatibility related to AnsibleCollectionConfig (
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Jan 31, 2023
1 parent e0f0f0e commit 6f3a36d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
py38-ansible212
py38-ansible213
py39-ansible214
py311-devel
platforms: linux,macos
macos: minmax
build:
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ repos:
# empty args needed in order to match mypy cli behavior
args: ["--strict"]
additional_dependencies:
- ansible-core
- cached_property
- packaging
- pytest
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ disallow_any_generics = true
# warn_unused_configs = True
exclude = "test/local-content"

[[tool.mypy.overrides]]
module = "ansible.*"
ignore_missing_imports = true

[tool.isort]
profile = "black"

Expand Down
19 changes: 19 additions & 0 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ def _ensure_module_available(self) -> None:
"indicates a broken execution environment."
)

# For ansible 2.15+ we need to initialize the plugin loader
# https://github.com/ansible/ansible-lint/issues/2945
if self.version >= Version("2.15.0.dev0"):
# pylint: disable=import-outside-toplevel
from ansible.plugins.loader import init_plugin_loader

init_plugin_loader([])
else:
# noinspection PyProtectedMember
from ansible.utils.collection_loader._collection_finder import ( # pylint: disable=import-outside-toplevel
_AnsibleCollectionFinder,
)

# noinspection PyProtectedMember
# pylint: disable=protected-access
_AnsibleCollectionFinder(
paths=[os.path.dirname(os.environ.get(ansible_collections_path(), "."))]
)._install() # pylint: disable=protected-access

def clean(self) -> None:
"""Remove content of cache_dir."""
if self.cache_dir:
Expand Down
7 changes: 7 additions & 0 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def test_runtime_mismatch_ansible_module(monkeypatch: MonkeyPatch) -> None:
def test_runtime_require_module() -> None:
"""Check that require_module successful pass."""
Runtime(require_module=True)
# Now we try to set the collection path, something to check if that is
# causing an exception, as 2.15 introduced new init code.
from ansible.utils.collection_loader import ( # pylint: disable=import-outside-toplevel
AnsibleCollectionConfig,
)

AnsibleCollectionConfig.playbook_paths = "."


def test_runtime_version_fail_module(mocker: MockerFixture) -> None:
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ passenv =
LC_ALL
LC_CTYPE
setenv =
ANSIBLE_DEVEL_WARNING='false'
COVERAGE_FILE = {env:COVERAGE_FILE:{toxworkdir}/.coverage.{envname}}
PIP_DISABLE_PIP_VERSION_CHECK = 1
PIP_CONSTRAINT = {toxinidir}/requirements.txt
Expand Down

0 comments on commit 6f3a36d

Please sign in to comment.