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

1,254 views
Skip to first unread message

Jan Burian

unread,
Jan 20, 2020, 11:26:14 AM1/20/20
to Ansible Project
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

Matt Martz

unread,
Jan 20, 2020, 11:34:46 AM1/20/20
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7f923096-1f9c-42be-9a4f-246db57dfd32%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Jan Burian

unread,
Jan 20, 2020, 11:47:23 AM1/20/20
to Ansible Project
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.


On Monday, January 20, 2020 at 5:34:46 PM UTC+1, Matt Martz wrote:
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.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Matt Martz

unread,
Jan 20, 2020, 11:52:33 AM1/20/20
to ansible...@googlegroups.com
You can use something like:

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

Then it would use localhost as the target.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1fbae5b8-3334-45d5-9a83-93528cb9fd6c%40googlegroups.com.

Jan Burian

unread,
Jan 20, 2020, 1:41:45 PM1/20/20
to Ansible Project
Oh, didn't know it can be used this way.
Thanks for tip. That's better then shell or command.

Jan


On Monday, January 20, 2020 at 5:52:33 PM UTC+1, Matt Martz wrote:
You can use something like:

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

Then it would use localhost as the target.

Reply all
Reply to author
Forward
0 new messages