2016-11-02 1 views
0

Я пытаюсь справиться с JSON жгуты анзибля без использования скриптов для оболочкисравнить значение от имени (ключа) в формате JSON с помощью строки анзибля

Так что я JSON, что я использую команду оболочки (скручивание), чтобы получить его:

{ 
    "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 взял из ранчо отдыха апи

Я пытаюсь получить одно из имен (ключ) с именем «состояние» и сравнить его с строка "active"

Я пробовал эту конфигурацию:

- 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 

Я пытался больше способов, но ничего не работает

любая идея?

ответ

0

Попробуйте это:

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

также заменить curl задачу с uri анзибль модуля вызова.

+0

Я получаю эту ошибку: failed ": true," msg ":" Условие условной проверки "[\" state \ "] == \" active \ "не выполнено. Ошибка была: ошибка при оценке условного (item [\ "state \"] == \ "active \"): 'unicode object' не имеет атрибута 'state' \ n \ n – dsaydon

+0

@dsaydon, пожалуйста, добавьте вывод '- debug : msg = "{{myvar.msg.data}}" 'к исходному сообщению. –

+0

Путь к json ket был неправильным. это решило мою проблему: with_items: "{{myvar.json.data}}" спасибо @ Константин Суворов – dsaydon