Reputation: 247
The API is supposed to return a 10 letter string and using curl
command on the API URL returns the correct string. The Ansible uri
module is returning a status message.
- name: Get variable output from api
uri:
url: "http://apiurl"
register: my_variable
- name: Print my_variable
debug:
msg: "Value of the variable is {{ my_variable }}"
The message is:
"msg": "Value of the variable {'redirected': False, 'url': 'http://apiurl', 'status': 200, 'server': 'nginx/1.10.3', 'date': 'Thu, 27 Oct 2022 17:47:49 GMT', 'content_type': 'text/plain; charset=utf-8', 'content_length': '23', 'connection': 'close', 'cookies_string': '', 'cookies': {}, 'msg': 'OK (23 bytes)', 'elapsed': 0, 'changed': False, 'failed': False}"
What's going on here?
Upvotes: 1
Views: 2807
Reputation: 12002
You are registering the result of the task which means you can then access all attributes of the task. if you want the response content then you need to set the return_content
attribute as detailed in the docs
Whether or not to return the body of the response as a “content” key in the dictionary result no matter it succeeded or failed.
Independently of this option, if the reported Content-type is “application/json”, then the JSON is always loaded into a key called json in the dictionary results.
Choices:
no ← (default)
yes
The docs also provide examples on how to use this https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html#examples
- name: Check that a page returns a status 200 and fail if the word AWESOME is not in the page contents
ansible.builtin.uri:
url: http://www.example.com
return_content: yes
register: this
you could then access this.content
to get the body of the response. Alternativly you could just use the get_url
module and access the body attribute
Upvotes: 3