-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add no-shorthand rule as experimental
Fixes: #2117
- Loading branch information
Showing
20 changed files
with
182 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
- name: Example with discouraged shorthand syntax | ||
hosts: localhost | ||
tasks: | ||
- name: Create a placefolder file | ||
ansible.builtin.command: chdir=/tmp touch foo # <-- don't use shorthand | ||
changed_when: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
- name: Example with discouraged shorthand syntax | ||
hosts: localhost | ||
tasks: | ||
- name: Create a placefolder file | ||
ansible.builtin.command: | ||
cmd: touch foo | ||
chdir: /tmp | ||
changed_when: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--- | ||
- name: Shell instead of command | ||
shell: echo hello world | ||
ansible.builtin.shell: | ||
cmd: echo hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
--- | ||
- command: echo this is a task without a name | ||
- command: # noqa: fqcn | ||
cmd: echo this is a task without a name # noqa: no-shorthand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# no-shorthand | ||
|
||
This rule identifies any use of [short-hand (free-form)](https://docs.ansible.com/ansible/2.7/user_guide/playbooks_intro.html#action-shorthand) | ||
module calling syntax and asks for switching to the full syntax. | ||
|
||
**Shorthand** syntax, also known as **free-form**, is known to produce | ||
subtle bugs. Short-hand syntax also prevents editors from providing feedback, | ||
autocomplete and validation for the edited line. | ||
|
||
```{note} | ||
As long you just pass a YAML string that contains a `=` character inside as the | ||
parameter to the action module name, we consider this as being shorthand | ||
syntax. Be sure you pass a dictionary to the module, so the short-hand parsing | ||
is never triggered. | ||
``` | ||
|
||
## Problematic code | ||
|
||
```yaml | ||
--- | ||
- name: Example with discouraged shorthand syntax | ||
hosts: localhost | ||
tasks: | ||
- name: Create a placefolder file | ||
ansible.builtin.command: chdir=/tmp touch foo # <-- don't use shorthand | ||
``` | ||
## Correct code | ||
```yaml | ||
--- | ||
- name: Example that avoids shorthand syntax | ||
hosts: localhost | ||
tasks: | ||
- name: Create a placefolder file | ||
ansible.builtin.command: | ||
cmd: touch foo # <-- ansible will not touch it | ||
chdir: /tmp | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""Implementation of NoShorthandRule.""" | ||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from ansiblelint.constants import INCLUSION_ACTION_NAMES, LINE_NUMBER_KEY | ||
from ansiblelint.errors import MatchError | ||
from ansiblelint.rules import AnsibleLintRule | ||
|
||
if TYPE_CHECKING: | ||
from ansiblelint.file_utils import Lintable | ||
|
||
|
||
class NoShorthandRule(AnsibleLintRule): | ||
"""Rule for detecting discouraged shorthand syntax for action modules.""" | ||
|
||
id = "no-shorthand" | ||
description = "Avoid shorthand inside files as it can produce subtile bugs." | ||
severity = "MEDIUM" | ||
tags = ["syntax", "risk", "experimental"] | ||
version_added = "v6.8.0" | ||
needs_raw_task = True | ||
|
||
def matchtask( | ||
self, task: dict[str, Any], file: Lintable | None = None | ||
) -> list[MatchError]: | ||
results: list[MatchError] = [] | ||
action = task["action"]["__ansible_module_original__"] | ||
|
||
if action in INCLUSION_ACTION_NAMES: | ||
return results | ||
|
||
action_value = task["__raw_task__"].get(action, None) | ||
if isinstance(action_value, str) and "=" in action_value: | ||
results.append( | ||
self.create_matcherror( | ||
message="Avoid using shorthand (free-form) when calling module actions.", | ||
linenumber=task[LINE_NUMBER_KEY], | ||
filename=file, | ||
tag=f"{self.id}[{action}]", | ||
) | ||
) | ||
return results | ||
|
||
|
||
if "pytest" in sys.modules: # noqa: C901 | ||
|
||
import pytest | ||
|
||
from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports | ||
from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports | ||
|
||
@pytest.mark.parametrize( | ||
("file", "expected"), | ||
( | ||
pytest.param("examples/playbooks/rule-no-shorthand-pass.yml", 0, id="pass"), | ||
pytest.param("examples/playbooks/rule-no-shorthand-fail.yml", 1, id="fail"), | ||
), | ||
) | ||
def test_rule_no_shorthand( | ||
default_rules_collection: RulesCollection, file: str, expected: int | ||
) -> None: | ||
"""Validate that rule works as intended.""" | ||
results = Runner(file, rules=default_rules_collection).run() | ||
|
||
for result in results: | ||
assert result.rule.id == NoShorthandRule.id, result | ||
assert len(results) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters