使用ansible解析json响应

问题描述 投票:0回答:1

我需要使用ansible解析来自API请求的json响应,然后对响应中返回的项目采取措施。这是我的剧本的一部分-

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

这是我先前运行的json响应

```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\":\"[email protected]\",\"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": "[email protected]",
                "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"
    }
}```

我想检索值"state": "INITIALISING""id": 1。我尝试了各种方法response.response.json.stateresponse['response']['json']['state']。而且它没有用。谁能帮我找回这些值,以便在以后的其他任务中使用?

json ansible ansible-2.x
1个回答
0
投票

我能够通过添加is defined来解决此问题。所以我的代码看起来像这样-failed_when: output is defined and output.json is defined and output.json.state != 'INITIALISING'

© www.soinside.com 2019 - 2024. All rights reserved.