Skip to content

Commit 5b0046a

Browse files
Merge pull request #331 from AlanCoding/vault_dump
Use Ansible encoder for dumping event data Reviewed-by: https://github.com/apps/ansible-zuul
2 parents c6f2695 + 88ba3f4 commit 5b0046a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

ansible_runner/display_callback/events.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@
3131
__all__ = ['event_context']
3232

3333

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+
3454
class IsolatedFileWrite:
3555
'''
3656
Class that will write partial event data to a file
@@ -48,7 +68,7 @@ def set(self, key, value):
4868
os.mkdir(os.path.join(self.private_data_dir, 'job_events'), 0o700)
4969
dropoff_location = os.path.join(self.private_data_dir, 'job_events', filename)
5070
write_location = '.'.join([dropoff_location, 'tmp'])
51-
partial_data = json.dumps(value)
71+
partial_data = json.dumps(value, cls=AnsibleJSONEncoderLocal)
5272
with os.fdopen(os.open(write_location, os.O_WRONLY | os.O_CREAT, stat.S_IRUSR | stat.S_IWUSR), 'w') as f:
5373
f.write(partial_data)
5474
os.rename(write_location, dropoff_location)

0 commit comments

Comments
 (0)