Skip to content

Read a JSON text into a variable in Ansible without parsing it

For one project I need to insert the content of a local file into another file on the remote system, and the first file happens to be JSON. The JSON file is in compact format (jq --compact-output) and is supposed to stay this way. When Ansible reads the content of the file, it determines that the content is JSON, and parses the content into the variable - and along the way is uncompressing the format. Not what I want.


To keep the file content as string, it is necessary to tell Ansible to handle this as string.

Reading the file:

- name: Read JSON from file
    json_content: "{{ lookup('file', '/home/user/text-shrinked.txt') | string }}"

When writing the file on the remote host (simplified):

- name: Write JSON
    content: "{{ json_content | string }}"
    dest: "/tmp/json.txt"

The usage of the string filter avoids parsing the content as JSON.


No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options