@@ -100,9 +100,9 @@ def parse_yaml_from_file(filepath: str) -> AnsibleBaseYAMLObject: # type: ignor
100
100
"""Extract a decrypted YAML object from file."""
101
101
dataloader = DataLoader ()
102
102
if hasattr (dataloader , "set_vault_secrets" ):
103
- dataloader .set_vault_secrets (
104
- [ ("default" , PromptVaultSecret (_bytes = to_bytes (DEFAULT_VAULT_PASSWORD )))]
105
- )
103
+ dataloader .set_vault_secrets ([
104
+ ("default" , PromptVaultSecret (_bytes = to_bytes (DEFAULT_VAULT_PASSWORD )))
105
+ ] )
106
106
107
107
return dataloader .load_from_file (filepath )
108
108
@@ -618,7 +618,7 @@ def _get_task_handler_children_for_tasks_or_playbooks(
618
618
basedir = os .path .dirname (basedir )
619
619
f = path_dwim (basedir , file_name )
620
620
return Lintable (f , kind = child_type )
621
- msg = f' The node contains none of: { ", " .join (sorted (INCLUSION_ACTION_NAMES ))} '
621
+ msg = f" The node contains none of: { ', ' .join (sorted (INCLUSION_ACTION_NAMES ))} "
622
622
raise LookupError (msg )
623
623
624
624
@@ -712,37 +712,6 @@ def normalize_task_v2(task: Task) -> dict[str, Any]:
712
712
return result
713
713
714
714
715
- def task_to_str (task : dict [str , Any ]) -> str :
716
- """Make a string identifier for the given task."""
717
- name = task .get ("name" )
718
- if name :
719
- return str (name )
720
- action = task .get ("action" )
721
- if isinstance (action , str ) or not isinstance (action , dict ):
722
- return str (action )
723
- args = [
724
- f"{ k } ={ v } "
725
- for (k , v ) in action .items ()
726
- if k
727
- not in [
728
- "__ansible_module__" ,
729
- "__ansible_module_original__" ,
730
- "_raw_params" ,
731
- LINE_NUMBER_KEY ,
732
- FILENAME_KEY ,
733
- ]
734
- ]
735
-
736
- raw_params = action .get ("_raw_params" , [])
737
- if isinstance (raw_params , list ):
738
- for item in raw_params :
739
- args .extend (str (item ))
740
- else :
741
- args .append (raw_params )
742
-
743
- return f"{ action ['__ansible_module__' ]} { ' ' .join (args )} "
744
-
745
-
746
715
# pylint: disable=too-many-nested-blocks
747
716
def extract_from_list ( # type: ignore[no-any-unimported]
748
717
blocks : AnsibleBaseYAMLObject ,
@@ -790,14 +759,16 @@ class Task(dict[str, Any]):
790
759
error:
791
760
This is normally None. It will be a MatchError when the raw_task cannot be
792
761
normalized due to an AnsibleParserError.
793
- position: Any
762
+ position:
763
+ The position of the task in the data structure using JSONPath like
764
+ notation (no $ prefix).
794
765
"""
795
766
796
767
raw_task : dict [str , Any ]
797
768
filename : str = ""
798
769
_normalized_task : dict [str , Any ] | _MISSING_TYPE = field (init = False , repr = False )
799
770
error : MatchError | None = None
800
- position : Any = None
771
+ position : str = ""
801
772
802
773
@property
803
774
def name (self ) -> str | None :
@@ -875,9 +846,43 @@ def is_handler(self) -> bool:
875
846
is_handler_file = "handlers" in paths
876
847
return is_handler_file or ".handlers[" in self .position
877
848
849
+ def __str__ (self ) -> str :
850
+ """Make a string identifier for the given task."""
851
+ name = self .get ("name" )
852
+ if name :
853
+ return str (name )
854
+ action = self .get ("action" )
855
+ if isinstance (action , str ) or not isinstance (action , dict ):
856
+ return str (action )
857
+ args = [
858
+ f"{ k } ={ v } "
859
+ for (k , v ) in action .items ()
860
+ if k
861
+ not in [
862
+ "__ansible_module__" ,
863
+ "__ansible_module_original__" ,
864
+ "_raw_params" ,
865
+ LINE_NUMBER_KEY ,
866
+ FILENAME_KEY ,
867
+ ]
868
+ ]
869
+
870
+ raw_params = action .get ("_raw_params" , [])
871
+ if isinstance (raw_params , list ):
872
+ for item in raw_params :
873
+ args .extend (str (item ))
874
+ else :
875
+ args .append (raw_params )
876
+
877
+ return f"{ action ['__ansible_module__' ]} { ' ' .join (args )} "
878
+
878
879
def __repr__ (self ) -> str :
879
880
"""Return a string representation of the task."""
880
- return f"Task('{ self .name } ' [{ self .position } ])"
881
+ result = f"Task('{ self .name or self .action } '"
882
+ if self .position :
883
+ result += f" [{ self .position } ])"
884
+ result += ")"
885
+ return result
881
886
882
887
def get (self , key : str , default : Any = None ) -> Any :
883
888
"""Get a value from the task."""
@@ -926,7 +931,6 @@ def task_in_list( # type: ignore[no-any-unimported]
926
931
"""Get action tasks from block structures."""
927
932
928
933
def each_entry (data : AnsibleBaseYAMLObject , position : str ) -> Iterator [Task ]: # type: ignore[no-any-unimported]
929
-
930
934
if not data or not isinstance (data , Iterable ):
931
935
return
932
936
for entry_index , entry in enumerate (data ):
@@ -958,7 +962,7 @@ def each_entry(data: AnsibleBaseYAMLObject, position: str) -> Iterator[Task]: #
958
962
if isinstance (item [attribute ], list ):
959
963
yield from each_entry (
960
964
item [attribute ],
961
- f"{ position } [{ item_index } ].{ attribute } " ,
965
+ f"{ position } [{ item_index } ].{ attribute } " ,
962
966
)
963
967
elif item [attribute ] is not None :
964
968
msg = f"Key '{ attribute } ' defined, but bad value: '{ item [attribute ]!s} '"
0 commit comments