大网数据广告活动
服务器运维问答

How to check the JSON response from a uri request with Ansible?

· 52CE 我爱测

I have an Ansible task that makes a URI request to a website to get a JSON response. I want Ansible to do something if the nested JSON variable is defined, and something else if it isn't. - name: Get JSON from the Interwebs uri: …

问题原文

原帖链接 · Neil · CC BY-SA 3.0

I have an Ansible task that makes a URI request to a website to get a JSON response. I want Ansible to do something if the nested JSON variable is defined, and something else if it isn't.

- name: Get JSON from the Interwebs
  uri: url="http://whatever.com/jsonresponse" return_content=yes
  register: json_response

- name: Write nested JSON variable to disk
  copy: content={{json_response.json.nested1.nested2}} dest="/tmp/foo.txt"

Note that using ignore_errors only works for the task's command failing, not for checking undefined values in nested data structures within a Jinja template. So if json_response.json.nested1.nested2 isn't defined, this task will still fail despite ignore_errors=yes being set.

How do I get this playbook to store some default value in /tmp/foo.txt if the request fails, or if the request doesn't have the right nested value defined?

社区高分解答

原帖链接 · alfredocambera · CC BY-SA 3.0

You need to use a jinja2 filter (http://docs.ansible.com/ansible/playbooks_filters.html). In this case, the name of the filter is from_json. In the following example I'll take an action when the key is found and other action when the could not be found:

 ---                                                                                                            

 - hosts: somehost                                                                                               
   sudo: yes                                                                                                    

   tasks:                                                                                                       

   - name: Get JSON from the Interwebs                                                                          
     uri: url="https://raw.githubusercontent.com/ljharb/node-json-file/master/package.json" return_content=yes  
     register: json_response                                                                                    

   - debug: msg="Error - undefined tag"                                                                         
     when: json_response["non_existent_tag"] is not defined                                                     

   - debug: msg="Success - tag defined =>{{  (json_response.content|from_json)['scripts']['test'] }}<="  
     when:  (json_response.content|from_json)['scripts']['test']  is defined    

Now replace the debug for the appropriate to take the desired action.

Hope it helps,

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-09-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。