using json as a password for docker_login module

I already asked that question on ansible-devel mailing list: https://groups.google.com/forum/#!msg/ansible-devel/aXyKGHRtDcQ/GvUxgopZAQAJ

I am trying to log into private docker container registry and I need to use a json-string as a password. So I do the following
`

docker_login:
registry: cr.yandex
username: json_key
password: “{{ lookup(‘file’, ‘key.json’) }}”
reauthorize: yes

`
And I got an error “Get https://cr.yandex/v2/: unauthorized: Password is invalid - must be JSON key”.

When I ran “ansible-playbook” in verbose mode I saw that the content of the file “key.json” was transcoded – double quotes were changed to single quotes so it have become an invalid json string.
Felix Fontein from ansible-devel mailing list pointed that the problem is that Ansible interprets JSON file as a dictionary, parses it and passes it on as a dictionary.

Is there any way to tell Ansible not to parse .json file to dictionary and keep it as a string?

If you are using Jinja2 2.10 or newer you could set it to preserve the variable by setting ANSIBLE_JINJA2_NATIVE[1]

Add to the task
   environment:
     ANSIBLE_JINJA2_NATIVE: 1

In my limited testing of Jinja native I haven't gotten the environment inside Ansible to work, it does worked when setting it on command line like so

   ANSIBLE_JINJA2_NATIVE=1 ansible-playbook <playbook>

but then it's set for every task.

[1] https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-jinja2-native

I added “jinja2_native = 1” to ansible.cfg and everything worked! Thanks a lot!