@@ -92,6 +92,7 @@ def matchtask(
9292 message = f"Avoid using free-form when calling module actions. ({ action } )" ,
9393 lineno = task .line ,
9494 filename = file ,
95+ details = action ,
9596 ),
9697 )
9798 return results
@@ -125,7 +126,10 @@ def filter_values(
125126 if v [0 ] in "\" '" :
126127 # Keep quoted strings together
127128 quote = v [0 ]
128- _ , v , remainder = v .split (quote , 2 )
129+ try :
130+ _ , v , remainder = v .split (quote , 2 )
131+ except ValueError :
132+ return val
129133 v = (
130134 DoubleQuotedScalarString
131135 if quote == '"'
@@ -146,10 +150,12 @@ def filter_values(
146150
147151 if match .tag == "no-free-form" :
148152 module_opts : dict [str , Any ] = {}
153+ target_module = match .details
154+
149155 for _ in range (len (task )):
150156 k , v = task .popitem (False )
151157 # identify module as key and process its value
152- if len ( k . split ( "." )) == 3 and isinstance (v , str ):
158+ if k == target_module and isinstance (v , str ):
153159 cmd = filter_values (v , "=" , module_opts )
154160 if cmd :
155161 module_opts ["cmd" ] = cmd
@@ -196,7 +202,7 @@ def filter_values(
196202 ("file" , "expected" ),
197203 (
198204 pytest .param ("examples/playbooks/rule-no-free-form-pass.yml" , 0 , id = "pass" ),
199- pytest .param ("examples/playbooks/rule-no-free-form-fail.yml" , 3 , id = "fail" ),
205+ pytest .param ("examples/playbooks/rule-no-free-form-fail.yml" , 6 , id = "fail" ),
200206 ),
201207 )
202208 def test_rule_no_free_form (
@@ -207,6 +213,26 @@ def test_rule_no_free_form(
207213 """Validate that rule works as intended."""
208214 results = Runner (file , rules = default_rules_collection ).run ()
209215
210- for result in results :
216+ rule_results = [r for r in results if r .rule .id == NoFreeFormRule .id ]
217+
218+ for result in rule_results :
211219 assert result .rule .id == NoFreeFormRule .id , result
212- assert len (results ) == expected
220+ assert len (rule_results ) == expected
221+
222+ def test_no_free_form_transform_error_handling () -> None :
223+ """Test that transform handles malformed quoted strings."""
224+ from ruamel .yaml .comments import CommentedMap
225+
226+ from ansiblelint .errors import MatchError
227+
228+ rule = NoFreeFormRule ()
229+ task = CommentedMap ({"ansible.builtin.shell" : 'chdir=" /tmp echo foo' })
230+ match = MatchError (
231+ message = "test" ,
232+ rule = rule ,
233+ details = "ansible.builtin.shell" ,
234+ tag = "no-free-form" ,
235+ )
236+
237+ rule .transform (match , None , task ) # type: ignore[arg-type]
238+ assert task ["ansible.builtin.shell" ] == {"cmd" : 'chdir=" /tmp echo foo' }
0 commit comments