From b5e48e61d148262bdc1ef045ce9b90c2fa6286de Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Wed, 19 Apr 2023 16:18:16 +0530 Subject: [PATCH] Avoid running args rule if we encounter a windows module (#3305) --- examples/playbooks/rule-args-module-pass.yml | 5 +++++ src/ansiblelint/rules/args.py | 16 +++++++++++++--- tools/install-reqs.sh | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/examples/playbooks/rule-args-module-pass.yml b/examples/playbooks/rule-args-module-pass.yml index 261c73e510..904c7e8f20 100644 --- a/examples/playbooks/rule-args-module-pass.yml +++ b/examples/playbooks/rule-args-module-pass.yml @@ -83,3 +83,8 @@ owner: false group: false use_ssh_args: true + + - name: Create software directory (Windows module - Bug 3200) + ansible.windows.win_file: + path: "c:\\test_dir" + state: directory diff --git a/src/ansiblelint/rules/args.py b/src/ansiblelint/rules/args.py index b1b9c6b63f..e47c0b8809 100644 --- a/src/ansiblelint/rules/args.py +++ b/src/ansiblelint/rules/args.py @@ -18,7 +18,7 @@ # pylint: disable=reimported import ansible.module_utils.basic as mock_ansible_module from ansible.module_utils import basic -from ansible.plugins import loader +from ansible.plugins.loader import PluginLoadContext, module_loader from ansiblelint.constants import LINE_NUMBER_KEY from ansiblelint.errors import MatchError @@ -63,9 +63,9 @@ @lru_cache -def load_module(module_name: str) -> loader.PluginLoadContext: +def load_module(module_name: str) -> PluginLoadContext: """Load plugin from module name and cache it.""" - return loader.module_loader.find_plugin_with_context(module_name) + return module_loader.find_plugin_with_context(module_name) class ValidationPassed(Exception): @@ -105,6 +105,16 @@ def matchtask( return [] loaded_module = load_module(module_name) + + # https://github.com/ansible/ansible-lint/issues/3200 + # since "ps1" modules cannot be executed on POSIX platforms, we will + # avoid running this rule for such modules + if isinstance( + loaded_module.plugin_resolved_path, + str, + ) and loaded_module.plugin_resolved_path.endswith(".ps1"): + return [] + module_args = { key: value for key, value in task["action"].items() diff --git a/tools/install-reqs.sh b/tools/install-reqs.sh index 61e3528a63..f0db84a69b 100755 --- a/tools/install-reqs.sh +++ b/tools/install-reqs.sh @@ -3,7 +3,7 @@ set -euo pipefail pushd examples/playbooks/collections >/dev/null MISSING=() export ANSIBLE_COLLECTIONS_PATH=. -for COLLECTION in ansible.posix community.docker community.general community.molecule; +for COLLECTION in ansible.posix community.docker community.general community.molecule ansible.windows; do COL_NAME=${COLLECTION//\./-} FILENAME=$(find . -maxdepth 1 -name "$COL_NAME*" -print -quit)