Skip to content

Commit 0afe99c

Browse files
audgirkapre-commit-ci[bot]ssbarneashatakshiiiischwarmco
authored
Transform for deprecated-local-action rule (#3689)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sorin Sbarnea <[email protected]> Co-authored-by: Shatakshi Mishra <[email protected]> Co-authored-by: Joachim Schwarm <[email protected]> Co-authored-by: Ruchi Pakhle <[email protected]>
1 parent c1b41f2 commit 0afe99c

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

.github/workflows/tox.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
7272
# Number of expected test passes, safety measure for accidental skip of
7373
# tests. Update value if you add/remove tests.
74-
PYTEST_REQPASS: 813
74+
PYTEST_REQPASS: 814
7575
steps:
7676
- name: Activate WSL1
7777
if: "contains(matrix.shell, 'wsl')"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: Fixture for deprecated-local-action
3+
hosts: localhost
4+
tasks:
5+
- name: Task example
6+
ansible.builtin.debug:
7+
delegate_to: localhost
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: Fixture for deprecated-local-action
3+
hosts: localhost
4+
tasks:
5+
- name: Task example
6+
local_action:
7+
module: ansible.builtin.debug

src/ansiblelint/rules/deprecated_local_action.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
import sys
77
from typing import TYPE_CHECKING
88

9-
from ansiblelint.rules import AnsibleLintRule
9+
from ansiblelint.rules import AnsibleLintRule, TransformMixin
1010

1111
if TYPE_CHECKING:
12+
from ruamel.yaml.comments import CommentedMap, CommentedSeq
13+
14+
from ansiblelint.errors import MatchError
1215
from ansiblelint.file_utils import Lintable
1316
from ansiblelint.utils import Task
1417

1518

16-
class TaskNoLocalAction(AnsibleLintRule):
19+
class TaskNoLocalAction(AnsibleLintRule, TransformMixin):
1720
"""Do not use 'local_action', use 'delegate_to: localhost'."""
1821

1922
id = "deprecated-local-action"
@@ -35,6 +38,24 @@ def matchtask(
3538

3639
return False
3740

41+
def transform(
42+
self,
43+
match: MatchError,
44+
lintable: Lintable,
45+
data: CommentedMap | CommentedSeq | str,
46+
) -> None:
47+
if match.tag == self.id:
48+
target_task = self.seek(match.yaml_path, data)
49+
for _ in range(len(target_task)):
50+
k, v = target_task.popitem(False)
51+
if k == "local_action":
52+
module_name = v["module"]
53+
target_task[module_name] = None
54+
target_task["delegate_to"] = "localhost"
55+
else:
56+
target_task[k] = v
57+
match.fixed = True
58+
3859

3960
# testing code to be loaded only with pytest or when executed the rule file
4061
if "pytest" in sys.modules:

src/ansiblelint/schemas/rulebook.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"$id": "https://raw.githubusercontent.com/ansible/ansible-rulebook/main/ansible_rulebook/schema/ruleset_schema.json",
4+
"title": "Ansible Rulebook",
5+
"description": "See https://ansible.readthedocs.io/projects/rulebook/en/stable/rulebooks.html",
46
"type": "array",
57
"items": {
68
"$ref": "#/$defs/ruleset"

test/test_transformer.py

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ def fixture_runner_result(
9696
True,
9797
id="cmd_instead_of_shell",
9898
),
99+
pytest.param(
100+
"examples/playbooks/transform-deprecated-local-action.yml",
101+
1,
102+
True,
103+
id="dep_local_action",
104+
),
99105
),
100106
)
101107
def test_transformer( # pylint: disable=too-many-arguments, too-many-locals

0 commit comments

Comments
 (0)