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

Ignore args rule finding if value for choice has jinja template #2929

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/playbooks/rule-args-module-fail-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@
foo: # this is a nested object which will have the __ injections
# that we later need to clean
bar: true

- name: Remove deployment dir
# module should produce: 'value of state must be one of: absent, directory, file, hard, link, touch, got: away'
ansible.builtin.file:
path: /opt/software/deployment
state: away
9 changes: 9 additions & 0 deletions examples/playbooks/rule-args-module-pass-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@
name: httpd
enabled: false
masked: false

- name: Clear deployment dir
ansible.builtin.file:
path: /opt/software/deployment
state: "{{ item }}"
mode: 0755
with_items:
- absent
- directory
20 changes: 19 additions & 1 deletion src/ansiblelint/rules/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@ def _parse_failed_msg(
)
return results

value_not_in_choices_error = re.search(
r"value of (?P<name>.*) must be one of:", error_message
)
if value_not_in_choices_error:
# ignore templated value not in allowed choices
choice_key = value_not_in_choices_error.group("name")
choice_value = task["action"][choice_key]
if has_jinja(choice_value):
_logger.debug(
"Value checking ignored for '%s' option in task '%s' at line %s.",
choice_key,
module_name,
task[LINE_NUMBER_KEY],
)
return results

results.append(
self.create_matcherror(
message=error_message,
Expand All @@ -236,7 +252,7 @@ def test_args_module_fail() -> None:
collection.register(ArgsRule())
success = "examples/playbooks/rule-args-module-fail-1.yml"
results = Runner(success, rules=collection).run()
assert len(results) == 4
assert len(results) == 5
assert results[0].tag == "args[module]"
assert "missing required arguments" in results[0].message
assert results[1].tag == "args[module]"
Expand All @@ -245,6 +261,8 @@ def test_args_module_fail() -> None:
assert "Unsupported parameters for" in results[2].message
assert results[3].tag == "args[module]"
assert "Unsupported parameters for" in results[2].message
assert results[4].tag == "args[module]"
assert "value of state must be one of" in results[4].message

def test_args_module_pass() -> None:
"""Test rule valid module options."""
Expand Down