来自api的ansible parse json数组回复 [英] ansible parse json array reply from api

查看:79
本文介绍了来自api的ansible parse json数组回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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": "the field 'args' has an invalid value, which appears to include a variable that is undefined. ...

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"
}

希望这会有所帮助.

这篇关于来自api的ansible parse json数组回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆