Quotes inside URI header

197 views
Skip to first unread message

1793...@gmail.com

unread,
Feb 21, 2022, 7:01:20 PM2/21/22
to Ansible Project

Hi there,

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


Regards,

Josue.

jbor...@gmail.com

unread,
Feb 21, 2022, 7:21:54 PM2/21/22
to Ansible Project
> 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.

1793...@gmail.com

unread,
Feb 21, 2022, 7:37:08 PM2/21/22
to Ansible Project
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:

            "headers": {

                "Authorization": "Token token="1234" "

            }


jbor...@gmail.com

unread,
Feb 21, 2022, 8:16:35 PM2/21/22
to Ansible Project
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".

1793...@gmail.com

unread,
Feb 21, 2022, 8:30:01 PM2/21/22
to Ansible Project
I see. Thank you! 
Reply all
Reply to author
Forward
0 new messages