Skip to content

Commit

Permalink
Improve transformation for no-free-form rule (#3945)
Browse files Browse the repository at this point in the history
  • Loading branch information
audgirka authored Dec 15, 2023
1 parent 3870c63 commit 185ec05
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 4 additions & 0 deletions examples/playbooks/transform-no-free-form.transformed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
args:
executable: /bin/bash
changed_when: false

- name: Example task with usage for '=' as module params
ansible.builtin.debug:
msg: "'Hello there world'"
3 changes: 3 additions & 0 deletions examples/playbooks/transform-no-free-form.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
- 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'
24 changes: 14 additions & 10 deletions src/ansiblelint/rules/no_free_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def filter_values(
if filter_key not in val:
return True

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

Expand All @@ -121,14 +121,18 @@ def filter_values(
k, v = task.popitem(False)
# identify module as key and process its value
if len(k.split(".")) == 3 and isinstance(v, str):
# Filter the module options and command
module_opts["cmd"] = " ".join(
[
item
for item in v.split(" ")
if filter_values(item, "=", module_opts)
],
)
# 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)
],
)

sorted_module_opts = {}
for key in sorted(
Expand All @@ -152,7 +156,7 @@ def filter_values(
[
item
for item in v.split(" ")
if filter_values(item, "executable=", exec_key_val)
if filter_values(item, "=", exec_key_val)
],
)
task["args"] = exec_key_val
Expand Down
2 changes: 1 addition & 1 deletion test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def fixture_runner_result(
),
pytest.param(
"examples/playbooks/transform-no-free-form.yml",
2,
3,
True,
True,
id="no_free_form_transform",
Expand Down

0 comments on commit 185ec05

Please sign in to comment.