比较json中name(key)的值和使用ansible的字符串

时间:2016-11-02 14:46:35

标签: json ansible

我试图在不使用shell脚本的情况下使用ansible处理json

所以我有一个json,我使用shell命令(curl)来获取它:

{
    "actions": {},
    "createDefaults": {},
    "createTypes": {},
    "data": [
        {
            "accountId": "1a87",
            "actions": {
                "addservicelink": "http://rancher.localhost:8080/v1/services/1s94/?action=addservicelink",
                "deactivate": "http://rancher.localhost:8080/v1/services/1s94/?action=deactivate",
                "remove": "http://rancher.localhost:8080/v1/services/1s94/?action=remove",
                "removeservicelink": "http://rancher.localhost:8080/v1/services/1s94/?action=removeservicelink",
                "restart": "http://rancher.localhost:8080/v1/services/1s94/?action=restart",
                "setservicelinks": "http://rancher.localhost:8080/v1/services/1s94/?action=setservicelinks",
                "update": "http://rancher.localhost:8080/v1/services/1s94/?action=update",
                "upgrade": "http://rancher.localhost:8080/v1/services/1s94/?action=upgrade"
            },
            "assignServiceIpAddress": false,
            "createIndex": 2,
            "created": "2016-10-31T14:34:07Z",
            "createdTS": 1477924447000,
            "currentScale": 2,
            "data": {
                "fields": {
                    "assignServiceIpAddress": false,
                    "currentScale": 2,
                    "launchConfig": {
                        "healthCheck": {
                            "healthyThreshold": 2,
                            "initializingTimeout": 60000,
                            "interval": 2000,
                            "port": 7000,
                            "requestLine": "GET \"/servicecheck/servicecheck.ashx\" \"HTTP/1.0\"",
                            "responseTimeout": 2000,
                            "strategy": "recreate",
                            "unhealthyThreshold": 3
                        },
                        "imageUuid": "docker_image",
                        "kind": "container",
                        "labels": {
                            "io.rancher.container.pull_image": "always",
                            "io.rancher.container.start_once": "true",
                            "io.rancher.service.hash": "##########################"
                        },
                        "logConfig": {},
                        "networkMode": "managed",
                        "privileged": false,
                        "publishAllPorts": false,
                        "readOnly": false,
                        "startOnCreate": true,
                        "stdinOpen": false,
                        "tty": false,
                        "vcpu": 1,
                        "version": "0"
                    },
                    "metadata": {
                        "io.rancher.service.hash": "########################"
                    },
                    "scale": 2,
                    "secondaryLaunchConfigs": [],
                    "startOnCreate": false,
                    "token": "########################",
                    "transitioningMessage": "Reconciling"
                }
            },
            "description": null,
            "environmentId": "1e63",
            "externalId": null,
            "fqdn": null,
            "healthState": "started-once",
            "id": "1s94",
            "kind": "service",
            "launchConfig": {
                "healthCheck": {
                    "healthyThreshold": 2,
                    "initializingTimeout": 60000,
                    "interval": 2000,
                    "port": 7000,
                    "requestLine": "GET \"/servicecheck\" \"HTTP/1.0\"",
                    "responseTimeout": 2000,
                    "strategy": "recreate",
                    "unhealthyThreshold": 3
                },
                "imageUuid": "docker_image",
                "kind": "container",
                "labels": {
                    "io.rancher.container.pull_image": "always",
                    "io.rancher.container.start_once": "true",
                    "io.rancher.service.hash": "###############"
                },
                "logConfig": {},
                "networkMode": "managed",
                "privileged": false,
                "publishAllPorts": false,
                "readOnly": false,
                "startOnCreate": true,
                "stdinOpen": false,
                "tty": false,
                "vcpu": 1,
                "version": "0"
            },
            "links": {
                "account": "http://rancher.localhost:8080/v1/services/1s94/account",
                "configItemStatuses": "http://rancher.localhost:8080/v1/services/1s94/configitemstatuses",
                "consumedbyservices": "http://rancher.localhost:8080/v1/services/1s94/consumedbyservices",
                "consumedservices": "http://rancher.localhost:8080/v1/services/1s94/consumedservices",
                "containerStats": "http://rancher.localhost:8080/v1/services/1s94/containerstats",
                "environment": "http://rancher.localhost:8080/v1/services/1s94/environment",
                "instances": "http://rancher.localhost:8080/v1/services/1s94/instances",
                "self": "http://rancher.localhost:8080/v1/services/1s94",
                "serviceExposeMaps": "http://rancher.localhost:8080/v1/services/1s94/serviceexposemaps"
            },
            "metadata": {
                "io.rancher.service.hash": "a69616e9fb4b91ad962ddf1eab6cd3662234c2bc"
            },
            "name": "myapp",
            "publicEndpoints": null,
            "removeTime": null,
            "removed": null,
            "retainIp": null,
            "scale": 2,
            "scalePolicy": null,
            "secondaryLaunchConfigs": [],
            "selectorContainer": null,
            "selectorLink": null,
            "startOnCreate": false,
            "state": "active",
            "transitioning": "no",
            "transitioningMessage": null,
            "transitioningProgress": null,
            "type": "service",
            "upgrade": null,
            "uuid": "###################",
            "vip": null
        }
    ],
    "filters": {
        "accountId": null,
        "createIndex": null,
        "created": null,
        "description": null,
        "environmentId": null,
        "externalId": null,
        "healthState": null,
        "id": null,
        "kind": null,
        "name": [
            {
                "modifier": "eq",
                "value": "myapp"
            }
        ],
        "removeTime": null,
        "removed": null,
        "selectorContainer": null,
        "selectorLink": null,
        "state": null,
        "uuid": null,
        "vip": null
    },
    "links": {
        "self": "http://rancher.localhost:8080/v1/services"
    },
    "pagination": {
        "first": null,
        "limit": 100,
        "next": null,
        "partial": false,
        "previous": null,
        "total": null
    },
    "resourceType": "service",
    "sort": null,
    "sortLinks": {
        "accountId": "http://rancher.localhost:8080/v1/services?name=myapp&sort=accountId",
        "createIndex": "http://rancher.localhost:8080/v1/services?name=myapp&sort=createIndex",
        "created": "http://rancher.localhost:8080/v1/services?name=myapp&sort=created",
        "description": "http://rancher.localhost:8080/v1/services?name=myapp&sort=description",
        "environmentId": "http://rancher.localhost:8080/v1/services?name=myapp&sort=environmentId",
        "externalId": "http://rancher.localhost:8080/v1/services?name=myapp&sort=externalId",
        "healthState": "http://rancher.localhost:8080/v1/services?name=myapp&sort=healthState",
        "id": "http://rancher.localhost:8080/v1/services?name=myapp&sort=id",
        "kind": "http://rancher.localhost:8080/v1/services?name=myapp&sort=kind",
        "name": "http://rancher.localhost:8080/v1/services?name=myapp&sort=name",
        "removeTime": "http://rancher.localhost:8080/v1/services?name=myapp&sort=removeTime",
        "removed": "http://rancher.localhost:8080/v1/services?name=myapp&sort=removed",
        "selectorContainer": "http://rancher.localhost:8080/v1/services?name=myapp&sort=selectorContainer",
        "selectorLink": "http://rancher.localhost:8080/v1/services?name=myapp&sort=selectorLink",
        "state": "http://rancher.localhost:8080/v1/services?name=myapp&sort=state",
        "uuid": "http://rancher.localhost:8080/v1/services?name=myapp&sort=uuid",
        "vip": "http://rancher.localhost:8080/v1/services?name=myapp&sort=vip"
    },
    "type": "collection"
}

json取自牧场主休息api

我尝试使用名称"州" 获取其中一个名称(密钥),并将其与字符串"活动&进行比较#34;

我试过这个配置:

- name: check service status
  shell: 'curl -s -u "###############:#############" -X Get -H "Accept: application/json" -H "Content-Type: application/json" "http://rancher.localhost:8080/v1/services?name=myapp" | python -m json.tool'
  register: output

- set_fact:
    myvar: "{{output.stdout|from_json}}"

- name: Downloading the packages.
  shell: 'echo "hello"'
  when: item.value["state"] == "active"
  with_items: myvar.msg.data

我尝试了更多方法,但没有任何工作

任何想法?

1 个答案:

答案 0 :(得分:0)

试试这个:

- name: Downloading the packages.
  debug:
    msg: hello
  when: item["state"] == "active"
  with_items: "{{ myvar.msg.data }}"

同时将curl任务替换为uri Ansible模块调用。