Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Molecule uses ANSIBLE_FILTER_PLUGINS #4135

Merged
merged 11 commits into from
Apr 8, 2024
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
matrix: ${{ fromJson(needs.pre.outputs.matrix) }}

env:
PYTEST_REQPASS: 450
PYTEST_REQPASS: 452
environment: test
steps:
- uses: actions/checkout@v4
Expand Down
66 changes: 40 additions & 26 deletions src/molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,32 +515,7 @@ def default_env(self):
self._config.ansible_collections_path: ":".join(collections_path_list),
"ANSIBLE_LIBRARY": ":".join(self._get_modules_directories()),
"ANSIBLE_FILTER_PLUGINS": ":".join(
[
self._get_filter_plugin_directory(),
util.abs_path(
os.path.join(
self._config.scenario.ephemeral_directory,
"plugins",
"filter",
),
),
util.abs_path(
os.path.join(
self._config.project_directory,
"plugins",
"filter",
),
),
util.abs_path(
os.path.join(
os.path.expanduser("~"),
".ansible",
"plugins",
"filter",
),
),
"/usr/share/ansible/plugins/filter",
],
self._get_filter_plugins_directories(),
),
},
)
Expand Down Expand Up @@ -1012,5 +987,44 @@ def _get_modules_directories(self) -> list[str]:
def _get_filter_plugin_directory(self):
return util.abs_path(os.path.join(self._get_plugin_directory(), "filter"))

def _get_filter_plugins_directories(self) -> list[str]:
"""Return list of ansilbe filter plugins includes directories."""
paths: list[str | None] = []
if os.environ.get("ANSIBLE_FILTER_PLUGINS"):
paths = list(
map(util.abs_path, os.environ["ANSIBLE_FILTER_PLUGINS"].split(":")),
)

paths.extend(
[
self._get_filter_plugin_directory(),
util.abs_path(
os.path.join(
self._config.scenario.ephemeral_directory,
"plugins",
"filter",
),
),
util.abs_path(
os.path.join(
self._config.project_directory,
"plugins",
"filter",
),
),
util.abs_path(
os.path.join(
os.path.expanduser("~"),
".ansible",
"plugins",
"filter",
),
),
"/usr/share/ansible/plugins/filter",
],
)

return [path for path in paths if path is not None]

def _absolute_path_for(self, env, key):
return ":".join([self.abs_path(p) for p in env[key].split(":")])
41 changes: 41 additions & 0 deletions test/a_unit/provisioner/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,47 @@ def test_get_filter_plugin_directory(_instance):
assert x == parts[-5:]


def test_get_filter_plugins_directories_default(_instance, monkeypatch):
monkeypatch.delenv("ANSIBLE_FILTER_PLUGINS", raising=False)

paths = _instance._get_filter_plugins_directories()

assert len(paths) == 5
assert re.search(r"molecule/provisioner/ansible/plugins/filter$", paths[0])
assert re.search(r"\.cache/molecule/[^/]+/default/plugins/filter$", paths[1])
assert re.search(r"/filter$", paths[2])
assert re.search(r"\.ansible/plugins/filter$", paths[3])
assert re.search(r"/usr/share/ansible/plugins/filter$", paths[4])


def tes_get_filter_plugins_directories_single_ansible_filter_plugins(
_instance,
monkeypatch,
):
monkeypatch.setenv("ANSIBLE_FILTER_PLUGINS", "/abs/path/plugins/filter")

paths = _instance._get_filter_plugins_directories()

assert len(paths) == 6
assert paths[0] == "/abs/path/plugins/filter"


def test_get_filter_plugins_directories_multi_ansible_filter_plugins(
_instance,
monkeypatch,
):
monkeypatch.setenv(
"ANSIBLE_FILTER_PLUGINS",
"relpath/plugins/filter:/abs/path/plugins/filter",
)

paths = _instance._get_filter_plugins_directories()

assert len(paths) == 7
assert paths[0].endswith("relpath/plugins/filter")
assert paths[1] == "/abs/path/plugins/filter"


def test_absolute_path_for(_instance):
env = {"foo": "foo:bar"}
x = ":".join(
Expand Down