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

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

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.

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.

You can use something like:

  • slurp:
    path: /some/local/path
    register: some_var
    delegate_to: localhost

Then it would use localhost as the target.

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

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

Jan