ansible解析来自api的json数组回复 [英] ansible parse json array reply from api
问题描述
我正在尝试解析来自 API 的 json 响应.浏览器中的响应如下所示:
I am trying to parse a json response from an API. The response in a browser looks like:
[{url: "abc.com/xyz"}]
我从 ansible 请求它:
I request it from ansible:
- name: Get url
uri:
url: my-url...
method: GET
force: yes
return_content: yes
#HEADER_Content-Type: "application/json"
register: json_response
我收到 ansible 的回复,看起来像这样(带调试):
I get a reply from ansible that looks like this (with debug):
- name: print reply
debug:
var: json_response
verbosity: 1
给出:
ok: [server] => {
"json_response": {
... //removed for readability
"content": "({:url \"https://the-file-I-want\"})"
}
所以似乎已经发生了一些解析(注意冒号 :
).
So it seems like some parsing happened already (note the colons :
).
访问内容似乎有效(使用调试 json_response['content']
):
Accessing the content seem to work (with debug json_response['content']
):
ok: [server] => {
"json_response['content']": "({:url \"https://the-file-I-want\"})"
}
但我似乎无法访问 json 响应 url.如果我尝试取数组的第一个元素,我会得到 "("
所以它似乎仍然是一个字符串.
But I cannot seems to access the json response url. If I try to take the first element of the array, I get "("
so it seems it is still a string.
- name: print reply2
debug:
var: json_response['content'][0]
verbosity: 1
from_json
似乎不起作用:fatal: [server]: FAILED!=>{"failed": true, "msg": "字段 'args' 有一个无效值,它似乎包含一个未定义的变量.
...
from_json
does not seem to work: fatal: [server]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined.
...
如何解析这样的 json 回复?
How do I parse a json reply like this one?
推荐答案
我创建了一个 json 文件 response.json ,内容如下:
I created a json file response.json with the following contents:
{
content: ({:url \"https://the-file-I-want\"})
}
然后,在我的剧本中,我加载了文件并获取您需要的 url,我创建了一个自定义的 jinja 过滤器,因为 Jinja2 没有任何用于查找子字符串或正则表达式的过滤器.
Then, in my playbook I loaded the file and to get the url you need, I created a custom jinja filter since Jinja2 does not have any filter for finding sub-string or regexp.
我的自定义过滤器名为 filter.py(您可以任意命名)位于与我的剧本相同目录中的名为 filter_plugins 的目录中.我的filter.py文件如下:
My custom filter named filter.py(you can name it anything) is in a dir called filter_plugins in the same directory as my playbook. My filter.py file is as follows:
import re
class FilterModule(object):
''' Custom filters are loaded by FilterModule objects '''
def filters(self):
return {'urlsubstr': self.urlsubstr}
def urlsubstr(self,content):
url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', content)
return url[0]
创建自定义过滤器后,我得到了这样的网址:
After creating the custom filter, I got the url like this:
- hosts: localhost
vars:
json_response: "{{ lookup('file', 'response.json') | from_json }}"
tasks:
- debug: msg="{{ json_response.content | urlsubstr }}"
with_dict: "{{ json_response }}"
这是运行我的剧本的输出:
This is the output of running my playbook:
TASK [setup] *******************************************************************
ok: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => (item={'value': u'({:url "https://the-file-I-want"})', 'key': u'content'}) => {
"item": {
"key": "content",
"value": "({:url \"https://the-file-I-want\"})"
},
"msg": "https://the-file-I-want"
}
希望这会有所帮助.
这篇关于ansible解析来自api的json数组回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!