# How to handle binary data in module output?

**URL:** <https://forum.ansible.com/t/how-to-handle-binary-data-in-module-output/28202>\
**Category:** Ansible Developer\
**Created:** [July 30, 2018, 8:50pm UTC](https://forum.ansible.com/t/how-to-handle-binary-data-in-module-output/28202 "2018-07-30T20:50:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [July 30, 2018, 8:50pm UTC](https://forum.ansible.com/t/how-to-handle-binary-data-in-module-output/28202/1 "2018-07-30T20:50:20Z")

</div>

Hi,

while working on a module, I have the problem that I need to return  
proper binary data (something like random bytes) to the user; the user  
has to register the result to use it. The user then has to process this  
data somehow, usually by passing it along (write it into a file, pass  
it to another module, ...). This worked fine so far for me locally (with  
Python 3), but now that I added tests, it breaks for Python 2. I guess  
somewhere in the process, some conversion (UTF-8 \<-\> whatever) is done,  
and the data is corrupted.

Maybe I'm doing something wrong somewhere, but in general: is there a  
good / suggested / Ansible specific way to handle binary data in  
playbook variables, or as return values of modules?

I first though about returning the data Base64 encoded, but I'm not  
sure whether that's the best solution, especially if the user has to  
pass it to another Ansible module.

The PR in question is [https://github.com/ansible/ansible/pull/42158](https://github.com/ansible/ansible/pull/42158)  
(ACME: support for TLS-ALPN-01). The binary data is generated by  
hashlib.sha256().digest(), and is of type str in Python 2 and of type  
bytes in Python 3.  
([https://github.com/ansible/ansible/pull/42158/files#diff-bcd36cbc1ccdcaf6d84c28c69bfb1dd1R494](https://github.com/ansible/ansible/pull/42158/files#diff-bcd36cbc1ccdcaf6d84c28c69bfb1dd1R494))

Thanks for any hints / suggestions / ideas on how to handle binary data!

All the best,  
Felix

---

<div class="post-metadata">

**Author:** ![Andrew\_Gaffney](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@Andrew\_Gaffney](https://forum.ansible.com/u/Andrew_Gaffney)\
**Post date:** [July 30, 2018, 10:12pm UTC](https://forum.ansible.com/t/how-to-handle-binary-data-in-module-output/28202/2 "2018-07-30T22:12:16Z")

</div>

Using base64 encoding is probably the best way to handle this. This is how the “slurp” module works.

[https://docs.ansible.com/ansible/latest/modules/slurp\_module.html#examples](https://docs.ansible.com/ansible/latest/modules/slurp_module.html#examples)
