31
31
__all__ = ['event_context' ]
32
32
33
33
34
+ # use a custom JSON serializer so we can properly handle !unsafe and !vault
35
+ # objects that may exist in events emitted by the callback plugin
36
+ # see: https://github.com/ansible/ansible/pull/38759
37
+ class AnsibleJSONEncoderLocal (json .JSONEncoder ):
38
+ '''
39
+ The class AnsibleJSONEncoder exists in Ansible core for this function
40
+ this performs a mostly identical function via duck typing
41
+ '''
42
+
43
+ def default (self , o ):
44
+ if getattr (o , 'yaml_tag' , None ) == '!vault' :
45
+ encrypted_form = o ._ciphertext
46
+ if isinstance (encrypted_form , bytes ):
47
+ encrypted_form = encrypted_form .decode ('utf-8' )
48
+ return {'__ansible_vault' : encrypted_form }
49
+ elif isinstance (o , (datetime .date , datetime .datetime )):
50
+ return o .isoformat ()
51
+ return super (AnsibleJSONEncoderLocal , self ).default (o )
52
+
53
+
34
54
class IsolatedFileWrite :
35
55
'''
36
56
Class that will write partial event data to a file
@@ -48,7 +68,7 @@ def set(self, key, value):
48
68
os .mkdir (os .path .join (self .private_data_dir , 'job_events' ), 0o700 )
49
69
dropoff_location = os .path .join (self .private_data_dir , 'job_events' , filename )
50
70
write_location = '.' .join ([dropoff_location , 'tmp' ])
51
- partial_data = json .dumps (value )
71
+ partial_data = json .dumps (value , cls = AnsibleJSONEncoderLocal )
52
72
with os .fdopen (os .open (write_location , os .O_WRONLY | os .O_CREAT , stat .S_IRUSR | stat .S_IWUSR ), 'w' ) as f :
53
73
f .write (partial_data )
54
74
os .rename (write_location , dropoff_location )
0 commit comments