File tree Expand file tree Collapse file tree 3 files changed +32
-28
lines changed
tests/integration/targets/utils_update_fact/tasks Expand file tree Collapse file tree 3 files changed +32
-28
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ bugfixes :
3+ - Fix update_fact to update a fact where a key in the referenced path contains a bracket.
Original file line number Diff line number Diff line change @@ -70,39 +70,27 @@ def _field_split(path):
7070 :return: the individual parts of the path
7171 :rtype: list
7272 """
73- que = list (path )
74- val = que .pop (0 )
73+ que = path
7574 fields = []
76- try :
77- while True :
78- field = ""
79- # found a '.', move to the next character
80- if val == "." :
81- val = que .pop (0 )
82- # found a '[', pop until ']' and then get the next
83- if val == "[" :
84- val = que .pop (0 )
85- while val != "]" :
86- field += val
87- val = que .pop (0 )
88- val = que .pop (0 )
89- else :
90- while val not in ["." , "[" ]:
91- field += val
92- val = que .pop (0 )
93- try :
94- # make numbers numbers
95- fields .append (ast .literal_eval (field ))
96- except Exception :
97- # or strip the quotes
98- fields .append (re .sub ("['\" ]" , "" , field ))
99- except IndexError :
100- # pop'ed past the end of the que
101- # so add the final field
75+ # regex match dotted values and square brackets
76+ regex = re .compile (r'^[^\[\].]+|^\[[\'\"].+?[\'\"]\]|^\[.+?\]' )
77+ while (regex .match (que )):
78+ m = regex .match (que )
79+ # remove outer square brackets
80+ field = re .sub (r'(^\[)|(\]$)' , "" , que [m .start ():m .end ()])
10281 try :
82+ # make numbers numbers
10383 fields .append (ast .literal_eval (field ))
10484 except Exception :
85+ # or strip the quotes
10586 fields .append (re .sub ("['\" ]" , "" , field ))
87+ try :
88+ if que [m .end ()] == '.' :
89+ que = que [m .end () + 1 :]
90+ else :
91+ que = que [m .end ():]
92+ except IndexError :
93+ que = ''
10694 return fields
10795
10896 def set_value (self , obj , path , val ):
Original file line number Diff line number Diff line change 66 c :
77 - 1
88 - 2
9+ d :
10+ " [bracket_key] " : bracket_value
11+ key : value
912
1013- name : Update the fact
1114 ansible.utils.update_fact :
1417 value : 10
1518 - path : " a['b']['c'][1]"
1619 value : 20
20+ - path : " a['b'][d]['[bracket_key]']"
21+ value : new_bracket_value
22+ - path : " a['b'][d]['key']"
23+ value : new_value
1724 register : updated
1825
1926- name : Assert
2633 c :
2734 - 10
2835 - 20
36+ d :
37+ " [bracket_key] " : new_bracket_value
38+ key : new_value
2939
3040- name : Update the fact
3141 ansible.utils.update_fact :
7080 c :
7181 - 1
7282 - 20
83+ d :
84+ " [bracket_key] " : bracket_value
85+ key : value
You can’t perform that action at this time.
0 commit comments