Replies: 6 comments 11 replies
-
That one is now quite easy to implement and very useful as we should really discourage use of the old short syntax, as it is almost impossible to get it integrated with editors and YAML/JSON schema verification. I am personally even considering a more extreme move in which we would even remove the parsing of short-hand syntax from the linter and only keep that new rule. The only downside is that it may upset few people that used debug module with shorthand. |
Beta Was this translation helpful? Give feedback.
-
I'm planning to implement this. Has anyone else already worked on it? |
Beta Was this translation helpful? Give feedback.
-
We use the following custom linter rule:
However, I'm not convinced it should go into the official ruleset. It detects too many false positives. Maybe someone can improve it? |
Beta Was this translation helpful? Give feedback.
-
Here is my rule: https://github.com/cognifloyd/ansible-lint/blob/fmt/src/ansiblelint/rules/TaskNoActionShorthand.py class TaskNoActionShorthand(AnsibleLintRule):
id = "no-action-shorthand"
shortdesc = "Use YAML args instead of action shorthand."
description = (
"Use YAML args instead of action shorthand.\n"
"Instead of ``module: arg1=value arg2=42``, use:\n"
" module:\n"
" arg1: value\n"
" arg2: 42\n"
"Early versions of Ansible used a shorthand to define args, but "
"(1) action shorthand relies on Ansible's magic type casting "
"which is the source of many obscure, difficult-to-debug issues; and "
"(2) schema based linting cannot detect issues when args are hidden "
"in the action shorthand. "
)
# Action shorthand was removed from ansible's documentation after v2.9
# https://docs.ansible.com/ansible/2.9/user_guide/playbooks_intro.html#action-shorthand
severity = 'MEDIUM'
tags = ['idiom']
version_added = "5.3"
needs_raw_task = True
def matchrawtask(
self,
raw_task: Dict[str, Any],
task: Dict[str, Any],
file: Optional[Lintable] = None,
) -> Union[bool, str]:
if task["action"]["__ansible_module__"] in FREE_FORM_MODULES:
return False
module = task["action"]["__ansible_module_original__"]
raw_action_block = raw_task[module]
if isinstance(raw_action_block, str):
return True
# raw_action_block should be a dict, which is what we want.
# if isinstance(raw_action_block, dict):
# return False
return False To make that work, I modified Aside: I'm building a |
Beta Was this translation helpful? Give feedback.
-
We don't really need a new rule to implement this one, we can implement it by updating the schemas. There is a pull request to add this directly to our schemas at ansible/schemas#310 and that will enforce it not only on the linter but also on vscode-ansible extension. |
Beta Was this translation helpful? Give feedback.
-
Implemented in #2527 |
Beta Was this translation helpful? Give feedback.
-
Issue Type
Ansible and Ansible Lint details
Desired Behaviour
Add a rule to flag lines that make use of the old ansible short-hand syntax.
This would be flagged:
while this won't:
Beta Was this translation helpful? Give feedback.
All reactions