b64decode returning wrong results in slurp

472 views
Skip to first unread message

T Karim

unread,
Oct 2, 2019, 3:43:59 AM10/2/19
to Ansible Project
Hi,
I am using the slurp module to retrieve and decode a binary file. However, I am getting wrong output with binary starting with 1F EF BD instead of what I have in a decoded file with other tools: 1F 8B 08

- name: Read backup file content
  slurp:
    src: "/tmp/{{ file }}"
  register: slurp_result
  delegate_to: localhost

- name: Define file content
  set_fact:
    content: "{{slurp_result.content | b64decode }}"


Any idea what could be the issue?
Thanks
Karim

Kai Stian Olstad

unread,
Oct 2, 2019, 7:30:38 AM10/2/19
to ansible...@googlegroups.com
The filter b64decode doesn't support binary data, only ASCII.

Since you are getting the file from localhost you can use lookup plugin
file.

{{ lookup('file', '/tmp/' ~ file) }}

--
Kai Stian Olstad

Karim Torkmen

unread,
Oct 3, 2019, 8:15:13 AM10/3/19
to ansible...@googlegroups.com
Hi Olstad,
Thanks for your quick answer.
I have tried the lookup before and it throws some error related to utf-8. That is why I had to use slurp.
Indeed, the file to decode is a .gz file which is received through SCP in a base64 encoded format.  
"msg": "An unhandled exception occurred while running the lookup plugin 'file'. Error was a <type 'exceptions.UnicodeDecodeError'>, original message: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte" 
Regards
Karim 




--
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/4d68e82804dd3667bcbe8e9d34971b7d%40olstad.com.

Kai Stian Olstad

unread,
Oct 3, 2019, 1:33:53 PM10/3/19
to ansible...@googlegroups.com
On 03.10.2019 14:14, Karim Torkmen wrote:
> Hi Olstad,
> Thanks for your quick answer.
> I have tried the lookup before and it throws some error related to
> utf-8.
> That is why I had to use slurp.
> Indeed, the file to decode is a .gz file which is received through SCP
> in a
> base64 encoded format.
> "msg": "An unhandled exception occurred while running the lookup plugin
> 'file'. Error was a <type 'exceptions.UnicodeDecodeError'>, original
> message: 'utf8' codec can't decode byte 0x8b in position 1: invalid
> start
> byte"

Can you just run shell or command module with "base64 -d" on the file.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages