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

no-shorthand: Avoid false positive with raw #2541

Merged
merged 1 commit into from
Oct 3, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ansiblelint/_internal/warning.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ runtime warnings found during execution. As stated by its name, they are
not counted as errors, so they do not influence the final outcome.

- `warning[empty-playbook]` is raised when a playbook file has no content.
- `warning[raw-non-string]` indicates that you are using `[raw](http://docs.ansible.com/ansible/latest/collections/ansible/builtin/raw_module.html#ansible-collections-ansible-builtin-raw-module)` module with
non-string arguments, which is not supported by Ansible.
4 changes: 4 additions & 0 deletions src/ansiblelint/rules/no_shorthand.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ syntax. Be sure you pass a dictionary to the module, so the short-hand parsing
is never triggered.
```

As `raw` module only accepts free-form, we trigger `no-shorthand[raw]` only if
we detect the presence of `executable=` inside raw calls. We advice the
explicit use of `args:` dictionary for configuring the executable to be run.

## Problematic code

```yaml
Expand Down
23 changes: 22 additions & 1 deletion src/ansiblelint/rules/no_shorthand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from typing import TYPE_CHECKING, Any

from ansiblelint._internal.rules import WarningRule
from ansiblelint.constants import INCLUSION_ACTION_NAMES, LINE_NUMBER_KEY
from ansiblelint.errors import MatchError
from ansiblelint.rules import AnsibleLintRule
Expand Down Expand Up @@ -32,7 +33,27 @@ def matchtask(
return results

action_value = task["__raw_task__"].get(action, None)
if isinstance(action_value, str) and "=" in action_value:
if task["action"].get("__ansible_module__", None) == "raw":
if isinstance(action_value, str) and "executable=" in action_value:
results.append(
self.create_matcherror(
message="Avoid embedding `executable=` inside raw calls, use explicit args dictionary instead.",
linenumber=task[LINE_NUMBER_KEY],
filename=file,
tag=f"{self.id}[raw]",
)
)
else:
results.append(
MatchError(
message="Passing a non string value to `raw` module is neither document nor supported.",
linenumber=task[LINE_NUMBER_KEY],
filename=file,
tag="warning[raw-non-string]",
rule=WarningRule(),
)
)
elif isinstance(action_value, str) and "=" in action_value:
results.append(
self.create_matcherror(
message=f"Avoid using shorthand (free-form) when calling module actions. ({action})",
Expand Down