# How to read binary file from ansible master to use it in k8s module

**URL:** <https://forum.ansible.com/t/how-to-read-binary-file-from-ansible-master-to-use-it-in-k8s-module/32006>\
**Category:** Ansible Project\
**Tags:** kubernetes\
**Created:** [January 20, 2020, 4:26pm UTC](https://forum.ansible.com/t/how-to-read-binary-file-from-ansible-master-to-use-it-in-k8s-module/32006 "2020-01-20T16:26:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Burian](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Burian](https://forum.ansible.com/u/Jan_Burian)\
**Post date:** [January 20, 2020, 4:26pm UTC](https://forum.ansible.com/t/how-to-read-binary-file-from-ansible-master-to-use-it-in-k8s-module/32006/1 "2020-01-20T16:26:13Z")

</div>

Hi,

I’m trying to read binary file from ansible master (localhost) to use it in k8s module.

The task looks like this:

`

- name: Create ConfigMap with keytab  
k8s:  
host: “{{ api\_host }}”  
api\_version: v1  
kind: ConfigMap  
state: present  
definition:  
metadata:  
name: cm-binary-data  
namespace: test-namespace  
binaryData:  
binary-file.bin: “{{ lookup(‘file’, ‘files/binary\_file.bin’) | b64encode }}”  
`

So tha vaule of k8s.definition.binaryData.binary-file.bin should be base64 encoded string.  
My goal is to have universal ansible role and in every use there will be different file - so I can’t put there the string directly, instead I will pass the file as variable to the playbook.

The problem is that lookup file cannot read binary file - only utf8 text file.

Workaround I can now think of is that I can encode base64 file using shell module and register the result. Then I can read the base64 string from registered variable.  
BUT I have to use shell module which is not recomended way (it is not best practice) and I’m trying not to use shell module.

Any tips how to do that without shell module?

Thanks and Regards  
Jan

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 20, 2020, 4:33pm UTC](https://forum.ansible.com/t/how-to-read-binary-file-from-ansible-master-to-use-it-in-k8s-module/32006/2 "2020-01-20T16:33:55Z")

</div>

In short, you cannot send binary data in a module argument.

If you can send base64 data instead of binary, you can use the `slurp` module to get the base64 encoded data. But you won’t be able to do `{{ whatever.content|b64decode }}` since that would try and produce binary data. Effectively you cannot send binary data through the templating engine.

---

<div class="post-metadata">

**Author:** ![Jan\_Burian](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Burian](https://forum.ansible.com/u/Jan_Burian)\
**Post date:** [January 20, 2020, 4:47pm UTC](https://forum.ansible.com/t/how-to-read-binary-file-from-ansible-master-to-use-it-in-k8s-module/32006/3 "2020-01-20T16:47:23Z")

</div>

Thanks for quick response.

I understand now, that lookup is templating engine which works only with utf8 text.  
I don’t need to do `{{ whatever.content|b64decode }}` - decode.  
I need only base64 encode binary file so I can pass the base64 string to k8s module.  
slurp would be fine except that slurp fetch binary file from remote host. I have file on local host.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 20, 2020, 4:51pm UTC](https://forum.ansible.com/t/how-to-read-binary-file-from-ansible-master-to-use-it-in-k8s-module/32006/4 "2020-01-20T16:51:41Z")

</div>

You can use something like:

- slurp:  
path: /some/local/path  
register: some\_var  
delegate\_to: localhost

Then it would use localhost as the target.

---

<div class="post-metadata">

**Author:** ![Jan\_Burian](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Burian](https://forum.ansible.com/u/Jan_Burian)\
**Post date:** [January 20, 2020, 6:41pm UTC](https://forum.ansible.com/t/how-to-read-binary-file-from-ansible-master-to-use-it-in-k8s-module/32006/5 "2020-01-20T18:41:44Z")

</div>

Oh, didn’t know it can be used this way.

Thanks for tip. That’s better then shell or command.

Jan
