Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible parse json array reply from api

Tags:

json

ansible

I am trying to parse a json response from an API. The response in a browser looks like:

[{url: "abc.com/xyz"}]

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

I get a reply from ansible that looks like this (with debug):

- name: print reply
  debug:
    var: json_response
    verbosity: 1

which gives:

 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 :).

Accessing the content seem to work (with debug json_response['content']):

ok: [server] => {
    "json_response['content']": "({:url \"https://the-file-I-want\"})"
}

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 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....

How do I parse a json reply like this one?

like image 879
nha Avatar asked Jan 22 '26 14:01

nha


2 Answers

I created a json file response.json with the following contents:

{
content: ({:url \"https://the-file-I-want\"})
}

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.

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

Hope this helps.

like image 60
clever_bassi Avatar answered Jan 25 '26 19:01

clever_bassi


To make the response json use the to_json filter, then navigate to the url key. That should give the value you're looking for: https://.....

Here's the documentation: http://docs.ansible.com/ansible/playbooks_filters.html#filters-for-formatting-data

The original response works on any case but it seems overkill unless there's a problem with the conversion to JSON, like in yours.

Nevertheless I think it might help me do something else I intend to do, which is why I was looking in here.

like image 40
eco Avatar answered Jan 25 '26 19:01

eco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!