diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 3667ea1f9f..3b9052c81b 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -59,7 +59,7 @@ jobs: WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY # Number of expected test passes, safety measure for accidental skip of # tests. Update value if you add/remove tests. - PYTEST_REQPASS: 801 + PYTEST_REQPASS: 802 steps: - name: Activate WSL1 if: "contains(matrix.shell, 'wsl')" diff --git a/examples/test_collection/README.md b/examples/test_collection/README.md new file mode 100644 index 0000000000..b4aaea442d --- /dev/null +++ b/examples/test_collection/README.md @@ -0,0 +1,3 @@ +# Ansible Collection - example.test_collection + +Documentation for the collection. diff --git a/examples/test_collection/galaxy.yml b/examples/test_collection/galaxy.yml new file mode 100644 index 0000000000..633719bd15 --- /dev/null +++ b/examples/test_collection/galaxy.yml @@ -0,0 +1,67 @@ +--- +### REQUIRED +# The namespace of the collection. This can be a company/brand/organization or product namespace under which all +# content lives. May only contain alphanumeric lowercase characters and underscores. Namespaces cannot start with +# underscores or numbers and cannot contain consecutive underscores +namespace: examples + +# The name of the collection. Has the same character restrictions as 'namespace' +name: test_collection + +# The version of the collection. Must be compatible with semantic versioning +version: 1.0.0 + +# The path to the Markdown (.md) readme file. This path is relative to the root of the collection +readme: README.md + +# A list of the collection's content authors. Can be just the name or in the format 'Full Name (url) +# @nicks:irc/im.site#channel' +authors: + - your name + +### OPTIONAL but strongly recommended +# A short summary description of the collection +description: your collection description + +# Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only +# accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file' +license: + - GPL-2.0-or-later + +# The path to the license file for the collection. This path is relative to the root of the collection. This key is +# mutually exclusive with 'license' +license_file: "" + +# A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character +# requirements as 'namespace' and 'name' +tags: [] + +# Collections that this collection requires to be installed for it to be usable. The key of the dict is the +# collection label 'namespace.name'. The value is a version range +# L(specifiers,https://python-semanticversion.readthedocs.io/en/latest/#requirement-specification). Multiple version +# range specifiers can be set and are separated by ',' +dependencies: {} + +# The URL of the originating SCM repository +repository: http://example.com/repository + +# The URL to any online docs +documentation: http://docs.example.com + +# The URL to the homepage of the collection/project +homepage: http://example.com + +# The URL to the collection issue tracker +issues: http://example.com/issue/tracker + +# A list of file glob-like patterns used to filter any files or directories that should not be included in the build +# artifact. A pattern is matched from the relative path of the file or directory of the collection directory. This +# uses 'fnmatch' to match the files or directories. Some directories and files like 'galaxy.yml', '*.pyc', '*.retry', +# and '.git' are always filtered. Mutually exclusive with 'manifest' +build_ignore: [] +# A dict controlling use of manifest directives used in building the collection artifact. The key 'directives' is a +# list of MANIFEST.in style +# L(directives,https://packaging.python.org/en/latest/guides/using-manifest-in/#manifest-in-commands). The key +# 'omit_default_directives' is a boolean that controls whether the default directives are used. Mutually exclusive +# with 'build_ignore' +# manifest: null diff --git a/examples/test_collection/roles/my_role/tasks/main.yml b/examples/test_collection/roles/my_role/tasks/main.yml new file mode 100644 index 0000000000..50c17c6786 --- /dev/null +++ b/examples/test_collection/roles/my_role/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- name: Task + ansible.builtin.include_role: + name: example.test_collection.my_role2 + vars: + my_role2_foo: something diff --git a/examples/test_collection/roles/my_role2/tasks/main.yml b/examples/test_collection/roles/my_role2/tasks/main.yml new file mode 100644 index 0000000000..27954a5de2 --- /dev/null +++ b/examples/test_collection/roles/my_role2/tasks/main.yml @@ -0,0 +1,4 @@ +--- +- name: Task + ansible.builtin.debug: + msg: "{{ my_role2_foo }}" diff --git a/src/ansiblelint/rules/var_naming.py b/src/ansiblelint/rules/var_naming.py index 771916ab30..109d4f5d0f 100644 --- a/src/ansiblelint/rules/var_naming.py +++ b/src/ansiblelint/rules/var_naming.py @@ -217,7 +217,8 @@ def matchtask( # noqa: C901 if ansible_module in ("include_role", "import_role"): action = task["action"] if isinstance(action, dict): - prefix = action.get("name", "").split("/")[-1] + role_fqcn = action.get("name", "") + prefix = role_fqcn.split("/" if "/" in role_fqcn else ".")[-1] for key in our_vars: match_error = self.get_var_naming_matcherror(key, prefix=prefix) if match_error: @@ -358,3 +359,10 @@ def test_var_naming_with_set_fact_and_cacheable() -> None: result = run_ansible_lint(role_path) assert result.returncode == RC.SUCCESS assert "var-naming" not in result.stdout + + def test_var_naming_with_include_role_import_role() -> None: + """Test with include role and import role.""" + role_path = "examples/test_collection/roles/my_role/tasks/main.yml" + result = run_ansible_lint(role_path) + assert result.returncode == RC.SUCCESS + assert "var-naming" not in result.stdout