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

Fix cmd args with no-free-form transformer #4215

Merged
merged 5 commits into from
Jun 14, 2024
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
11 changes: 11 additions & 0 deletions examples/playbooks/transform-no-free-form.transformed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
cmd: touch foo
changed_when: false

- name: Create a placefolder file
ansible.builtin.command: # <-- command can also go first
chdir: /tmp
cmd: touch bar
changed_when: false

- name: Use raw to echo
ansible.builtin.raw: echo foo # <-- don't use executable=
args:
Expand All @@ -17,3 +23,8 @@
- name: Example task with usage for '=' as module params
ansible.builtin.debug:
msg: "'Hello there world'"
changed_when: false

- name: Task that has a non-debug string with spaces
ansible.builtin.set_fact:
foo: '"String with spaces"'
8 changes: 8 additions & 0 deletions examples/playbooks/transform-no-free-form.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
ansible.builtin.command: chdir=/tmp touch foo # <-- don't use shorthand
changed_when: false

- name: Create a placefolder file
ansible.builtin.command: touch bar chdir=/tmp # <-- command can also go first
changed_when: false

- name: Use raw to echo
ansible.builtin.raw: executable=/bin/bash echo foo # <-- don't use executable=
changed_when: false

- name: Example task with usage for '=' as module params
ansible.builtin.debug: msg='Hello there world'
changed_when: false

- name: Task that has a non-debug string with spaces
ansible.builtin.set_fact: foo="String with spaces"
49 changes: 31 additions & 18 deletions src/ansiblelint/rules/no_free_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def matchtask(
"win_command",
"win_shell",
):
if self.cmd_shell_re.match(action_value):
if self.cmd_shell_re.search(action_value):
fail = True
else:
fail = True
Expand All @@ -107,33 +107,46 @@ def filter_values(
val: str,
filter_key: str,
filter_dict: dict[str, Any],
) -> bool:
"""Return True if module option is not present in the string."""
) -> str:
"""Pull out key=value pairs from a string and set them in filter_dict.

Returns unmatched strings.
"""
if filter_key not in val:
return True
return val

extra = ""
[k, v] = val.split(filter_key, 1)
if " " in k:
extra, k = k.rsplit(" ", 1)

if v[0] in "\"'":
# Keep quoted strings together
quote = v[0]
_, v, remainder = v.split(quote, 2)
v = f"{quote}{v}{quote}"
else:
try:
v, remainder = v.split(" ", 1)
except ValueError:
remainder = ""

[k, v] = val.split(filter_key)
filter_dict[k] = v
return False

extra = " ".join(
(extra, filter_values(remainder, filter_key, filter_dict)),
)
return extra.strip()

if match.tag == "no-free-form":
module_opts: dict[str, Any] = {}
for _ in range(len(task)):
k, v = task.popitem(False)
# identify module as key and process its value
if len(k.split(".")) == 3 and isinstance(v, str):
# if it is a message
if "msg" in v:
filter_values(v, "=", module_opts)
else:
# Filter the module options and command
module_opts["cmd"] = " ".join(
[
item
for item in v.split(" ")
if filter_values(item, "=", module_opts)
],
)
cmd = filter_values(v, "=", module_opts)
if cmd:
module_opts["cmd"] = cmd

sorted_module_opts = {}
for key in sorted(
Expand Down
2 changes: 1 addition & 1 deletion test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def fixture_runner_result(
),
pytest.param(
"examples/playbooks/transform-no-free-form.yml",
3,
5,
True,
True,
id="no_free_form_transform",
Expand Down
Loading