如何检查来自 Ansible URI 调用的 json 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40235550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inspect a json response from Ansible URI call
提问by Hafiz
I have a service call that returns system status in jsonformat. I want to use the ansible URImodule to make the call and then inspect the response to decide whether the system is up or down
我有一个以json格式返回系统状态的服务调用。我想使用 ansible URI模块进行调用,然后检查响应以确定系统是启动还是关闭
{"id":"20161024140306","version":"5.6.1","status":"UP"}
This would be the jsonthat is returned
这将json是返回的
This is the ansible task that makes a call:
这是进行调用的 ansible 任务:
- name: check sonar web is up
uri:
url: http://sonarhost:9000/sonar/api/system/status
method: GET
return_content: yes
status_code: 200
body_format: json
register: data
Question is how can I access dataand inspect it as per ansible documentation this is how we store results of a call. I am not sure of the final step which is to check the status.
问题是我如何data根据 ansible 文档访问和检查它,这是我们存储调用结果的方式。我不确定最后一步是检查状态。
回答by Hafiz
This works for me.
这对我有用。
- name: check sonar web is up
uri:
url: http://sonarhost:9000/sonar/api/system/status
method: GET
return_content: yes
status_code: 200
body_format: json
register: result
until: result.json.status == "UP"
retries: 10
delay: 30
Notice that resultis a ansible dictionary and when you set return_content=yesthe response is added to this dictionary and is accessible using jsonkey
请注意,这result是一个 ansible 字典,当您设置return_content=yes响应时,响应将添加到此字典中,并且可以使用jsonkey访问
Also ensure you have indented the task properly as shown above.
还要确保您已正确缩进任务,如上所示。
回答by ocean
You've made the right first step by saving the output into a variable.
通过将输出保存到变量中,您已经迈出了正确的第一步。
The next step is to use either when:or failed_when:statement in your next task, which will then switch based on the contents of the variable. There are a whole powerful set of statements for use in these, the Jinja2 builtin filters, but they are not really linked well into the Ansible documentation, or summarised nicely.
下一步是在您的下一个任务中使用when:orfailed_when:语句,然后将根据变量的内容进行切换。有一整套强大的语句可供使用,Jinja2 内置过滤器,但它们并没有很好地链接到 Ansible 文档中,或者很好地总结。
I use super explicitly named output variables, so they make sense to me later in the playbook :) I would probably write yours something like:
我使用超级显式命名的输出变量,所以它们对我稍后在剧本中很有意义:) 我可能会写你的类似:
- name: check sonar web is up
uri:
url: http://sonarhost:9000/sonar/api/system/status
method: GET
return_content: yes
status_code: 200
body_format: json
register: sonar_web_api_status_output
- name: do this thing if it is NOT up
shell: echo "OMG it's not working!"
when: sonar_web_api_status_output.stdout.find('UP') == -1
That is, the text "UP" is not found in the variable's stdout.
也就是说,在变量的标准输出中找不到文本“UP”。
Other Jinja2 builtin filtersI've used are:
我使用过的其他Jinja2 内置过滤器是:
changed_when: "'<some text>' not in your_variable_name.stderr"when: some_number_of_files_changed.stdout|int > 0
changed_when: "'<some text>' not in your_variable_name.stderr"when: some_number_of_files_changed.stdout|int > 0
The Ansible "Conditionals" docs pagehas some of this info. This blog postwas also very informative.
该Ansible“条件”文档页面有一些这个信息的。这篇博文也非常有用。
回答by Boeboe
As per documentation at https://docs.ansible.com/ansible/latest/modules/uri_module.html
根据https://docs.ansible.com/ansible/latest/modules/uri_module.html 上的文档
Whether or not to return the body of the response as a "content" key in the dictionary result. 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.
是否将响应正文作为字典结果中的“内容”键返回。与此选项无关,如果报告的 Content-type 为“application/json”,则 JSON 始终加载到字典结果中名为 json 的键中。
---
- name: Example of JSON body parsing with uri module
connection: local
gather_facts: true
hosts: localhost
tasks:
- name: Example of JSON body parsing with uri module
uri:
url: https://jsonplaceholder.typicode.com/users
method: GET
return_content: yes
status_code: 200
body_format: json
register: data
# failed_when: <optional condition based on JSON returned content>
- name: Print returned json dictionary
debug:
var: data.json
- name: Print certain element
debug:
var: data.json[0].address.city

