Skip to content

Commit

Permalink
Avoid deprecated-bare-vars false positive with file paths
Browse files Browse the repository at this point in the history
Fixes: #3646
  • Loading branch information
ssbarnea committed Sep 4, 2023
1 parent 5e80a5b commit 4dfe005
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
6 changes: 0 additions & 6 deletions examples/playbooks/rule-deprecated-bare-vars-fail.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@
msg: "{{ item }}"
with_dict: my_dict

### Testing with_dict with a default empty dictionary
- name: Use with_dict loop using variable and default
ansible.builtin.debug:
msg: "{{ item.key }} - {{ item.value }}"
with_dict: uwsgi_ini | default({})

- name: Use with_nested loop using bare variable
ansible.builtin.debug:
msg: "{{ item.0 }} {{ item.1 }}"
Expand Down
9 changes: 9 additions & 0 deletions examples/playbooks/rule-deprecated-bare-vars-pass.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,12 @@
with_items: >-
{%- set ns = [1, 1, 2] -%}
{{- ns.keys | unique -}}
- name: Reproduce bug 3646
ansible.builtin.file:
path: "{{ item.path }}"
state: directory
mode: "{{ item.mode }}"
with_community.general.filetree:
- "../templates/SpaceVim.d/"
when: item.state == "directory" and ".git" not in item.path
10 changes: 7 additions & 3 deletions src/ansiblelint/rules/deprecated_bare_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from typing import TYPE_CHECKING, Any

from ansiblelint.rules import AnsibleLintRule
from ansiblelint.text import has_glob, has_jinja
from ansiblelint.text import has_glob, has_jinja, is_fqcn_or_name

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
Expand Down Expand Up @@ -84,7 +84,11 @@ def _matchvar(
task: dict[str, Any],
loop_type: str,
) -> bool | str:
if isinstance(varstring, str) and not has_jinja(varstring):
if (
isinstance(varstring, str)
and not has_jinja(varstring)
and is_fqcn_or_name(varstring)
):
valid = loop_type == "with_fileglob" and bool(
has_jinja(varstring) or has_glob(varstring),
)
Expand Down Expand Up @@ -121,4 +125,4 @@ def test_use_bare_negative() -> None:
failure = "examples/playbooks/rule-deprecated-bare-vars-fail.yml"
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
assert len(errs) == 12
assert len(errs) == 11

0 comments on commit 4dfe005

Please sign in to comment.