0

I need to parse the json response from an API request using ansible and then take action on items returned in the response. Here is part of my ansible playbook -

```- name: Invoke Import API
  uri: 
    url: "{{ atl_bitbucket_dataset_url }}/rest/api/1.0/migration/imports"
    user: admin
    password: "{{ atl_bitbucket_admin_password }}"
    method: POST
    follow_redirects: yes
    force_basic_auth: yes
    creates: "{{ atl_product_home_shared }}/data/migration/import/lock.file"
    body: "{ \"archivePath\": \"{{ atl_bitbucket_dataset_url | basename }}\" }"
    body_format: json
    return_content: yes
  register: response
  until: response.status == 200
  retries: 6
  delay: 15
  failed_when: response.response.json.state != 'INITIALISING'

- name: get status of import
  debug: var=response```

and here is the json response i got on a previous run

```TASK [bitbucket_dataset_restore : get status of import] ************************
ok: [localhost] => {
    "response": {
        "attempts": 1,
        "cache_control": "no-cache, no-transform",
        "changed": false,
        "connection": "close",
        "content": "{\"id\":1,\"initiator\":{\"name\":\"admin\",\"emailAddress\":\"admin@yourcompany.com\",\"id\":1,\"displayName\":\"AdminIstrator\",\"active\":true,\"slug\":\"admin\",\"type\":\"NORMAL\",\"links\":{\"self\":[{\"href\":\"http://bbdc-test-loadbala-t6vnlr2363vl-1404860112.us-west-2.elb.amazonaws.com/users/admin\"}]}},\"nodeId\":\"e72aa995-5016-4b2c-80e8-edba5eda3ab4\",\"progress\":{\"percentage\":0},\"startDate\":1574803309090,\"state\":\"INITIALISING\",\"type\":\"com.atlassian.bitbucket.migration.import\",\"updateDate\":1574803309090}",
        "content_type": "application/json;charset=UTF-8",
        "cookies": {},
        "cookies_string": "",
        "date": "Tue, 26 Nov 2019 21:21:49 GMT",
        "elapsed": 1,
        "failed": false,
        "failed_when_result": False,
        "json": {
            "id": 1,
            "initiator": {
                "active": True,
                "displayName": "AdminIstrator",
                "emailAddress": "admin@yourcompany.com",
                "id": 1,
                "links": {
                    "self": [
                        {
                            "href": "http://bbdc-test-loadbala-t6vnlr2363vl-1404860112.us-west-2.elb.amazonaws.com/users/admin"
                        }
                    ]
                },
                "name": "admin",
                "slug": "admin",
                "type": "NORMAL"
            },
            "nodeId": "e72aa995-5016-4b2c-80e8-edba5eda3ab4",
            "progress": {
                "percentage": 0
            },
            "startDate": 1574803309090,
            "state": "INITIALISING",
            "type": "com.atlassian.bitbucket.migration.import",
            "updateDate": 1574803309090
        },
        "msg": "OK (unknown bytes)",
        "redirected": False,
        "status": 200,
        "transfer_encoding": "chunked",
        "url": "http://localhost:7990/rest/api/1.0/migration/imports",
        "vary": "accept-encoding,x-auserid,cookie,x-ausername,accept-encoding",
        "warnings": [
            "The value True (type bool) in a string field was converted to u'True' (type string). If this does not look like what you expect, quote the entire value to ensure it does not change."
        ],
        "x_arequestid": "@XXAH7Kx1281x1x0",
        "x_asen": "SEN-500",
        "x_auserid": "1",
        "x_ausername": "admin",
        "x_content_type_options": "nosniff"
    }
}```

I want to retrieve the value "state": "INITIALISING" and the "id": 1. I have tried various methods response.response.json.state or response['response']['json']['state']. And it has not worked. Can anyone help me on how i can retrieve those values so i can use in other tasks further down?

raj.andy1
  • 121
  • 1
  • 7
  • What does "and it has not worked" mean? It produces an error? It gives you back the wrong data? It melts your computer? It's also confusing when you say "the response from a previous run" -- was it working once upon a time and now no longer works? – mdaniel Dec 06 '19 at 07:05
  • Sorry shoud have been more clear - here is the error i get ```TASK [bitbucket_dataset_restore : Invoke Import API] *************************** fatal: [localhost]: FAILED! => {"msg": "The conditional check 'response.json.state != 'INITIALISING'' failed. The error was: error while evaluating conditional (response.json.state != 'INITIALISING'): 'dict object' has no attribute 'json'"}```. In the successful case - the playbook had ```failed_when: "'INITIALISING' not in response.content"```. I want to retrieve the exact key:value for the status. – raj.andy1 Dec 06 '19 at 18:52

1 Answers1

0

I was able to fix this by adding is defined. So my code looks like this - failed_when: output is defined and output.json is defined and output.json.state != 'INITIALISING'

raj.andy1
  • 121
  • 1
  • 7