Skip to content

Commit

Permalink
Soften no-free-form occurences (#2586)
Browse files Browse the repository at this point in the history
Fixes: #2573
  • Loading branch information
ssbarnea committed Oct 10, 2022
1 parent 66f961c commit ba05878
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ libyaml
lineinfile
lintable
literalinclude
localectl
matchtask
matchyaml
maxdepth
Expand Down
4 changes: 4 additions & 0 deletions examples/playbooks/rule-no-free-form-pass.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
args:
executable: /bin/bash
changed_when: false
- name: Configure locale
# https://github.com/ansible/ansible-lint/issues/2573
ansible.builtin.command: localectl set-locale LANG=en_GB.UTF-8
when: not ansible_check_mode
26 changes: 20 additions & 6 deletions src/ansiblelint/rules/no_free_form.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of NoFreeFormRune."""
from __future__ import annotations

import re
import sys
from typing import TYPE_CHECKING, Any

Expand All @@ -22,6 +23,9 @@ class NoFreeFormRune(AnsibleLintRule):
tags = ["syntax", "risk", "experimental"]
version_added = "v6.8.0"
needs_raw_task = True
cmd_shell_re = re.compile(
r"(chdir|creates|executable|removes|stdin|stdin_add_newline|warn)="
)

def matchtask(
self, task: dict[str, Any], file: Lintable | None = None
Expand Down Expand Up @@ -55,13 +59,23 @@ def matchtask(
)
)
elif isinstance(action_value, str) and "=" in action_value:
results.append(
self.create_matcherror(
message=f"Avoid using free-form when calling module actions. ({action})",
linenumber=task[LINE_NUMBER_KEY],
filename=file,
fail = False
if task["action"].get("__ansible_module__") in (
"command",
"shell",
):
if self.cmd_shell_re.match(action_value):
fail = True
else:
fail = True
if fail:
results.append(
self.create_matcherror(
message=f"Avoid using free-form when calling module actions. ({action})",
linenumber=task[LINE_NUMBER_KEY],
filename=file,
)
)
)
return results


Expand Down

0 comments on commit ba05878

Please sign in to comment.