accessing json data

1,485 views
Skip to first unread message

Brent Langston

unread,
Feb 10, 2014, 2:02:10 PM2/10/14
to ansible...@googlegroups.com
I'm sure this is probably something I'm doing wrong, but I can't seem to figure out the right thing, so hopefully someone else can.

This is the value of the registered variable "jenkinsStatus": http://hastebin.com/cacusiwiki.tex

Given the following playbook:

---
- name: Jenkins Status Info
  hosts: localhost
  connection: local
  user: root
  gather_facts: False
  vars:
    project: member-web-dev

  tasks:

    - name: Get jenkins status
      uri: url={{ jenkins_url }}/{{ project }}/lastBuild/api/json return_content=yes status_code=200
      register: jenkinsStatus

    - name: show the output
      debug: var=jenkinsStatus.json.changeSet.items.0.comment

I'm trying to access the comment data, but am not getting back any value:
ok: [127.0.0.1] => {
    "jenkinsStatus.json.changeSet.items.0.comment": "{{ jenkinsStatus.json.changeSet.items.0.comment }}"
}

and if I just use debug: var=jenkinsStatus.json.changeSet.items

ok: [127.0.0.1] => {
    "jenkinsStatus.json.changeSet.items": "<built-in method items of dict object at 0x7fd259a89ae0>"
}

So how can I access/use the value stored in comment?

> ansible-playbook --version
ansible-playbook 1.5,

Michael DeHaan

unread,
Feb 10, 2014, 2:08:24 PM2/10/14
to ansible...@googlegroups.com
It seems like you need to index your array like so:

jenkinsStatus.json.changeSet.items[0].comment 

Since the ".0." would be looking for a hash member named "0" versus an array index.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Brent Langston

unread,
Feb 10, 2014, 2:14:17 PM2/10/14
to ansible...@googlegroups.com, Michael DeHaan
I was wondering about that. I *did* try that earlier (with so many other combinations as well.) This is what I have now:

      debug: var=jenkinsStatus.json.changeSet.items[0].comment

result:

    "jenkinsStatus.json.changeSet.items[0].comment": "{{ jenkinsStatus.json.changeSet.items[0].comment }}"

I can’t figure out what’s wrong with that statement, though.

Brent Langston

unread,
Feb 10, 2014, 2:28:42 PM2/10/14
to ansible...@googlegroups.com, Michael DeHaan
So I downloaded the json to a file, and tried to access that value using ‘jq’:

cat document.json | jq .json.changeSet.items[0].comment

that works fine, so I think you’re right about the syntax.  Is it possible that Ansible can’t access objects nested that deep?

Michael DeHaan

unread,
Feb 10, 2014, 2:29:40 PM2/10/14
to Brent Langston, ansible...@googlegroups.com
Nope.

Save it in a sample Python test.py file and get the right python syntax to survive a print statement, and then you'll be on the right track.


Brent Langston

unread,
Feb 10, 2014, 2:53:27 PM2/10/14
to Michael DeHaan, ansible...@googlegroups.com
This appears to work:

      debug: var="{{jenkinsStatus.json.changeSet["items"][0].comment}}”

My theory is that when the parser reached “items” in the json output, it called the python method “items” and received it’s output as a list.

In this case, it was necessary to surround the variable with “{{ }}”, and expressly reference [“items”], to get it to interpret correctly.

Michael DeHaan

unread,
Feb 10, 2014, 3:45:40 PM2/10/14
to Brent Langston, ansible...@googlegroups.com
This could be true, actually.   It wouldn't call the method without parens but it would have errored out.


J Hawkesworth

unread,
Sep 10, 2015, 5:07:49 AM9/10/15
to Ansible Project, bren...@oufan.com
I hit this yesterday too.  

Sorry to resurrect very old thread but thought I would add that in the end I wound up with the following syntax (to retrieve a list of conmmit messages from a jenkins build).  Note that I wound up with single quotes around items:

     - name: collect the commit messages
       set_fact:
          commit_messages: "{{ build_info.json.changeSet['items']|map(attribute='msg')|list }}"
Reply all
Reply to author
Forward
0 new messages