Hi guys,
I am facing the problem of not being able to use the JSON part of the HTTP response in my subsequent tasks in an Ansible playbook. Here are the relevant tasks from the playbook:
- name: Login to account as the specified user and obtain its id
local_action: >
uri url='http://{{ account_app }}.{{ target_domain}}/auth/user'
body='{ "email": "{{ userId }}", "password": "{{ sa_password }}" }'
method=POST
return_content=yes status_code=200
HEADER_Content-Type="application/json"
register: usp_user
tags:
- config-manager
- bootstrap
- api
- name: Archive previous top-level settings config
local_action: >
command
curl -i -f -H "content-type: application/json"
-H "{{ lookup('template', './user_header.json.j2') }}"
-XPUT http://{{ config_manager }}.{{ target_domain }}/archive/settings
# uri url='http://{{ config_manager }}.{{ target_domain }}/archive/settings'
# method=PUT
# return_content=yes status_code=200
# HEADER_Content-Type="application/json"
# HEADER_user="{{ usp_user.json }}"
tags:
- config-manager
- bootstrap
- api
- name: Create new top-level settings config
local_action: >
command
curl -i -f -H "content-type: application/json"
-H "{{ lookup('template', './user_header.json.j2') }}"
-XPOST http://{{ config_manager }}.{{ target_domain }}/config/settings
-d '{{ lookup('template', './stages_request.json.j2') }}'
# uri url='http://{{ config_manager }}.{{ target_domain }}/config/settings'
# body='{{ lookup('template', './stages_request.json.j2') }}'
# method=POST
# return_content=yes status_code=200
# HEADER_Content-Type="application/json"
# HEADER_user="{{ usp_user.json }}"
tags:
- config-manager
- bootstrap
- api
The `user_header.json.j2` is a simple header template to overcome a YAML parsing problem of having a ':' followed by the curly braces
user: {{ usp_user.json }}
Basically what I am trying to do it to log in as a particular user to an account-managing app, and then use the JSON part of the response that contains required credentials (like the user id and the token) to do some requests to another app. It seems to work if I form the JSON header manually using the individual values from the JSON response, but not when I try to use the JSON as it is by referring to `variable.json`.
In the commented out code I tried to use the `uri` module which I've become a fan of lately and used `curl` command to test whether the problem is in the way `uri` supplies header in the request. Both `curl` and `uri` versions fail with the 400 on the "Archive previous top-level settings config" task (where no body is used in the request) and in the verbose output I see that the `usp_user.json` has 'u' prefixes before any of the key/value pairs, like follows:
"user: {u'customer': u'52afd279fa33dd1f00000004',...
I suspect this is at least part of the problem if not the problem on itself - please correct me if I am wrong and this is just the weird formatting that is only visible in the verbose output. I tried inserting the task `debug var=usp_user.json` and it seems to output well-formatted JSON though, without any of the 'u' attached.
I would appreciate a guidance on how to use the json part of the response in forming headers for succeeding requests.