From ba05878e93cd20d3f1a4cd4197a98cd8d50b6086 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Mon, 10 Oct 2022 21:04:54 +0100 Subject: [PATCH] Soften no-free-form occurences (#2586) Fixes: #2573 --- .config/dictionary.txt | 1 + examples/playbooks/rule-no-free-form-pass.yml | 4 +++ src/ansiblelint/rules/no_free_form.py | 26 ++++++++++++++----- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.config/dictionary.txt b/.config/dictionary.txt index f9bcd1dd5a..44e0d9e6e6 100644 --- a/.config/dictionary.txt +++ b/.config/dictionary.txt @@ -86,6 +86,7 @@ libyaml lineinfile lintable literalinclude +localectl matchtask matchyaml maxdepth diff --git a/examples/playbooks/rule-no-free-form-pass.yml b/examples/playbooks/rule-no-free-form-pass.yml index 6103d16c45..1700dfed2d 100644 --- a/examples/playbooks/rule-no-free-form-pass.yml +++ b/examples/playbooks/rule-no-free-form-pass.yml @@ -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 diff --git a/src/ansiblelint/rules/no_free_form.py b/src/ansiblelint/rules/no_free_form.py index 18bb955f48..1a00e614a4 100644 --- a/src/ansiblelint/rules/no_free_form.py +++ b/src/ansiblelint/rules/no_free_form.py @@ -1,6 +1,7 @@ """Implementation of NoFreeFormRune.""" from __future__ import annotations +import re import sys from typing import TYPE_CHECKING, Any @@ -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 @@ -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