JSON Query Filter Help

568 views
Skip to first unread message

Tito Valentin

unread,
Feb 15, 2018, 5:28:24 PM2/15/18
to Ansible Project
I have a task that hits a url and returns the results in JSON array like this:

  1. "json": {
           "statuses": [
               {
                   "application": "JIRA",
                   "completeKey": "com.atlassian.jira.plugins.jira-healthcheck-plugin:hsqlHealthCheck",
                   "description": "Checks if the instance is connected to an HSQL or H2 database",
                   "documentation": "https://confluence.atlassian.com/x/1guaEQ",
                    "failureReason": "You are not using an HSQL or H2 embedded database with a production license.",
                    "healthy": true,
                    "id": 0,
                    "isHealthy": true,
                    "name": "Embedded database",
                    "severity": "undefined",
                    "tag": "Supported Platforms",
                    "time": 1518726326309
                },
                {
                    "application": "JIRA",
                    "completeKey": "com.atlassian.jira.plugins.jira-healthcheck-plugin:eolHealthCheck",
                    "description": "Checks if the running version of JIRA is approaching, or has reached End of Life.",
                    "documentation": "https://confluence.atlassian.com/x/HjnRLg",
                    "failureReason": "Unable to verify the End of Life date for version '7.4.x'. The check has not been performed.",
                    "healthy": true,
                    "id": 0,
                    "isHealthy": true,
                    "name": "End of Life",
                    "severity": "undefined",
                    "tag": "Supported Platforms",
                    "time": 1518726326316
                },
    {
                    "application": "JIRA",
                    "completeKey": "com.atlassian.jira.plugins.jira-healthcheck-plugin:luceneSupportHealthCheck",
                    "description": "Checks the state of the search index is consistent with the database.",
                    "documentation": "https://confluence.atlassian.com/x/9IUfL",
                    "failureReason": "The issue index is inconsistent with the database state.",
                    "healthy": false,
                    "id": 0,
                    "isHealthy": false,
                    "name": "Lucene",
                    "severity": "major",
                    "tag": "Indexing",
                    "time": 1518726326372
                },
The two tasks that run looks like this:

---


- name: check if jira indexing is required and do it
  hosts
: localhost
  become
: no
  connection
: local
 
#vars_files:
 
#  - vault.yml


  tasks
:
   
- name: Check for "Lucene" health
      uri
:
        url
: https://example.com/rest/supportHealthCheck/1.0/check/
        method
: GET
        user
: user
        password
: password
        force_basic_auth
: yes
        body_format
: json
        return_content
: yes
        validate_certs
: no
        status_code
: 200
     
register: results


   
- name: Get health result
      debug
: var=item
      with_items
: "{{results|json_query(name_Lucene_query)}}"
      vars
:
        name_Lucene_query
: "statuses.name[?name=='Lucene'].isHealthy"


The final result looks like this:

  1. ###################
    ## results / output of "Check for "Lucene" health
     
    TASK [Check for "Lucene" health] **********************************************************************************************************************************************************************************
    task path: /home/user/jiraHealth.yml:11
     
     
    "json": {
           "statuses": [
               {
                   "application": "JIRA",
                   "completeKey": "com.atlassian.jira.plugins.jira-healthcheck-plugin:hsqlHealthCheck",
                   "description": "Checks if the instance is connected to an HSQL or H2 database",
                   "documentation": "https://confluence.atlassian.com/x/1guaEQ",
                    "failureReason": "You are not using an HSQL or H2 embedded database with a production license.",
                    "healthy": true,
                    "id": 0,
                    "isHealthy": true,
                    "name": "Embedded database",
                    "severity": "undefined",
                    "tag": "Supported Platforms",
                    "time": 1518726326309
                },
                {
                    "application": "JIRA",
                    "completeKey": "com.atlassian.jira.plugins.jira-healthcheck-plugin:eolHealthCheck",
                    "description": "Checks if the running version of JIRA is approaching, or has reached End of Life.",
                    "documentation": "https://confluence.atlassian.com/x/HjnRLg",
                    "failureReason": "Unable to verify the End of Life date for version '7.4.x'. The check has not been performed.",
                    "healthy": true,
                    "id": 0,
                    "isHealthy": true,
                    "name": "End of Life",
                    "severity": "undefined",
                    "tag": "Supported Platforms",
                    "time": 1518726326316
                },
    {
                    "application": "JIRA",
                    "completeKey": "com.atlassian.jira.plugins.jira-healthcheck-plugin:luceneSupportHealthCheck",
                    "description": "Checks the state of the search index is consistent with the database.",
                    "documentation": "https://confluence.atlassian.com/x/9IUfL",
                    "failureReason": "The issue index is inconsistent with the database state.",
                    "healthy": false,
                    "id": 0,
                    "isHealthy": false,
                    "name": "Lucene",
                    "severity": "major",
                    "tag": "Indexing",
                    "time": 1518726326372
                },
     
    ####################
    ## results / output of "Get health result
     
    TASK [Get health result] ******************************************************************************************************************************************************************************************
    task path: /home/user/jiraHealth.yml:25
    ok: [localhost] => (item=) => {
       "changed": false,
       "item": ""
    }
The issue I am having is that my second task is not storing the result I am looking for. What I am trying to do is from all of the data structure, I want to find the one that has the key named name with the value of Lucene and for that data structure, give me the result of isHealthy so that I can use that result to run a third task. As you can see from the output of the data, isHealthy is false but my second task is not storing that value in item. What am I doing wrong here with my filter? Basically, I want to create and run a third task that is like this based on the isHealthy value:

- name: Reindex db
  command
: runthis
 
when: "{{ name_Lucene_query }}" == false


...but I can't get to that third task to run yet until my second task gets the value I am looking for. Any help is greatly appreciated.

Matt Martz

unread,
Feb 15, 2018, 5:38:49 PM2/15/18
to ansible...@googlegroups.com
Honestly, I try to stay away from json_query when possible, and stick to just standard jinja2 pipelines:

"{{ (results.json.statuses|selectattr('name', 'match', 'Lucene')|list|first).isHealthy }}"

You can use a variety of tests instead of `match`, such as `search` or `equalto`.

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ff35e970-8a2c-427b-b3eb-ea570e1c8931%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Tito Valentin

unread,
Feb 15, 2018, 6:50:28 PM2/15/18
to Ansible Project
@Matt - that worked perfectly. I'm assuming that the jinja2 pipeline you added as a sample works as follows:

It pipes the results from my results variable grabbing only the json.statuses data structure, selects the attribute name that matches the value Lucene and from that data structure or position, look for the first isHealthy key and give me only that result. Do I have that correct?
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.

Matt Martz

unread,
Feb 15, 2018, 6:51:32 PM2/15/18
to ansible...@googlegroups.com
That is correct.


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages