I have a scenario where I have to pass a token in the URI Authorization header, but it must be enclosed in double quotes as per my endpoint’s requirement. When I attempt to do it, the quotes are being parsed into JSON and the token is enclosed by backslashes.
How can I avoid Ansible evaluating those manually added double quotes? Here’s my URI task snippet.
name: Login
uri:
url: “test.com”
method: GET
validate_certs: no
return_content: yes
headers:
Authorization: ‘Token token=“{{ token }}”’
register: output
When I attempt to do it, the quotes are being parsed into JSON and the token is enclosed by backslashes.
Why do you believe this is the case, if it’s due to the backslashes appearing in the task output that’s just due to the output being encoded as json. The literal value is what that json represents not the json literally. Using the yaml callback [1] is a nice way to get a more “literal” value back in the output, although it’s not perfect. The yaml formatting has also been introduced in the default callback in ansible-core 2.13 [2] through the result_format option but that hasn’t been released yet.
I understand. In that case, beyond that output appearance, it’s the actual value that I thought was being encoded/modified. That’s the one I need in double quotes. The header value must look like this:
If it’s showing “Authorization”: “Token token="1234"” then that’s to be expected. The json formatting of the output needs to escape the inner double quotes to make it valid json. The literal value sent to the module is still Token token=“1234”.