6
6
import sys
7
7
from typing import TYPE_CHECKING
8
8
9
- from ansiblelint .rules import AnsibleLintRule
9
+ from ansiblelint .rules import AnsibleLintRule , TransformMixin
10
10
11
11
if TYPE_CHECKING :
12
+ from ruamel .yaml .comments import CommentedMap , CommentedSeq
13
+
14
+ from ansiblelint .errors import MatchError
12
15
from ansiblelint .file_utils import Lintable
13
16
from ansiblelint .utils import Task
14
17
15
18
16
- class TaskNoLocalAction (AnsibleLintRule ):
19
+ class TaskNoLocalAction (AnsibleLintRule , TransformMixin ):
17
20
"""Do not use 'local_action', use 'delegate_to: localhost'."""
18
21
19
22
id = "deprecated-local-action"
@@ -35,6 +38,24 @@ def matchtask(
35
38
36
39
return False
37
40
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
+
38
59
39
60
# testing code to be loaded only with pytest or when executed the rule file
40
61
if "pytest" in sys .modules :
0 commit comments