# Module uri - Image to base64 String for JSON object

**URL:** <https://forum.ansible.com/t/module-uri-image-to-base64-string-for-json-object/31585>\
**Category:** Ansible Project\
**Created:** [November 1, 2019, 6:35am UTC](https://forum.ansible.com/t/module-uri-image-to-base64-string-for-json-object/31585 "2019-11-01T06:35:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander1](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Alexander1](https://forum.ansible.com/u/Alexander1)\
**Post date:** [November 1, 2019, 6:35am UTC](https://forum.ansible.com/t/module-uri-image-to-base64-string-for-json-object/31585/1 "2019-11-01T06:35:52Z")

</div>

Hi community,

maybe one of you have an nice idea how to solve my problem.

I want to transfer a logo (jpg) over the ansible tower api “api/v2/settings/ui/”.

`

- name: Import over API  
uri:  
url: “{{ api\_url }}”  
user: “{{ username }}”  
password: “{{ password }}”  
method: PATCH  
body: “{{ lookup(‘template’, ‘logo.j2’) }}”  
force\_basic\_auth: yes  
body\_format: json  
status\_code: “200,201”  
validate\_certs: no  
`

The template “logo.j2” looks like this:

`{ "CUSTOM_LOGO": "data:image/jpeg;base64,/9j/4AAQSkZ.......AKKKKAP/2Q==" }`

To get the base64 string I uploaded the image with the Ansible-Tower GUI and call the API GET method. An alternative is to use this website: [https://www.base64-image.de/](https://www.base64-image.de/)

**My goal is to encode the image with ansible or with a jinja2 filter like this:**

`  
{  
“CUSTOM\_LOGO”: “{{ lookup(‘file’, ‘image.jpg’) | b64encode }}”  
}

`

Thanks for your support.

Alexander

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 1, 2019, 11:03am UTC](https://forum.ansible.com/t/module-uri-image-to-base64-string-for-json-object/31585/2 "2019-11-01T11:03:31Z")

</div>

This list for Ansible only and not Tower.  
For help with Tower contact RedHat support.

---

<div class="post-metadata">

**Author:** ![Alexander1](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Alexander1](https://forum.ansible.com/u/Alexander1)\
**Post date:** [November 1, 2019, 11:54am UTC](https://forum.ansible.com/t/module-uri-image-to-base64-string-for-json-object/31585/3 "2019-11-01T11:54:22Z")

</div>

Hi Kai,

its not an Ansible Tower question. Is general Ansible.

I want to encode a jpg image to base64 to use this image in a JSON object.

This JSON object I want to transfer as body with the Ansible module uri.

Thanks,

Alexander

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 1, 2019, 12:46pm UTC](https://forum.ansible.com/t/module-uri-image-to-base64-string-for-json-object/31585/4 "2019-11-01T12:46:17Z")

</div>

> Hi Kai,
> 
> its not an Ansible Tower question. Is general Ansible.

I'm sorry, probably read it to fast and jumped to conclusions.

> I want to encode a jpg image to base64 to use this image in a JSON object.  
> This JSON object I want to transfer as body with the Ansible module uri.

The b64decode in Ansible can only handle plain text/ascii, for binary date it gives corrupt result so my hunch that b64encode does the same and you need to use base64 command with command/shell module.

---

<div class="post-metadata">

**Author:** ![Alexander1](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Alexander1](https://forum.ansible.com/u/Alexander1)\
**Post date:** [November 8, 2019, 5:53am UTC](https://forum.ansible.com/t/module-uri-image-to-base64-string-for-json-object/31585/5 "2019-11-08T05:53:41Z")

</div>

Hi,

here is my solution:

`

- name: Get base64 string  
shell: “/usr/bin/base64 {{ role\_path }}/files/{{ ansible\_tower\_logo\_filename }} | tr -d ‘\n’”  
delegate\_to: 127.0.0.1  
register: base64\_string

- name: debug base64 string  
debug:  
var: base64\_string  
verbosity: 3

- name: Show template  
debug:  
msg: “{{ lookup(‘template’, ‘ui.j2’) | to\_nice\_json }}”  
verbosity: 2

- name: Import over API  
uri:  
url: “{{ api\_url }}”  
user: “{{ ansible\_tower\_inventory\_admin\_username }}”  
password: “{{ ansible\_tower\_inventory\_admin\_password }}”  
method: PATCH  
body: “{{ lookup(‘template’, ‘ui.j2’) }}”  
force\_basic\_auth: yes  
body\_format: json  
status\_code: “200,201”  
validate\_certs: no  
`

Here the content of the file ui.j2:

`{ "CUSTOM_LOGO": "data:image/jpeg;base64,/{{ base64_string.stdout }}", "CUSTOM_LOGIN_INFO": "{{ ansible_tower_login_info_message | default('') }}" }`

Thanks for your help  
Alexander
