2017-09-08 1 views
0

azureの 'az'コマンドから入力を読み込む以下のAnsibleコードがあります。基本的には、VMSの作成が完了したリソースグループを確認するために使用します。ここでシェルプログラムからのjson出力を解析するために、コマンドモジュールを使用してください。

#!/usr/local/bin/ansible-playbook 
 
--- 
 
- name: Firewall Rules 
 
    hosts: localhost 
 
    connection: local 
 
    vars: 
 
    iscbr: false 
 

 
    tasks: 
 
    - name: Register status of group x 
 
    command: az group deployment list -g Tes1Pod 
 
    register: azdeployment 
 

 
    - name: 
 
    set_fact: 
 
     provisioningstatus: "{{ (azdeployment.stdout|from_json) }}" 
 

 

 
    - name: debug provisioningstatus 
 
    debug: 
 
     var: "{{item}}" 
 
    with_items: "{{provisioningstatus.properties}}" 
 

 

 
    # - name: Debug stuff 
 
    # debug: 
 
    #  msg: " item.provisioningState" 
 
    # with_items: "{{provisioningstatus" 
 

 
    # - debug: var=provisioningstatus
Ansibleのデバッグから出力さ:だから基本的に私は、各項目のproperties.Provisioningstateを取得したい(成功したかどうか)

ok: [localhost] => { 
 
"provisioningstatus": [ 
 
    { 
 
     "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Resources/deployments/vm_deploy_xxxxxxxx", 
 
     "name": "vm_deploy_xxxxxxxx", 
 
     "properties": { 
 
      "correlationId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", 
 
      "debugSetting": null, 
 
      "dependencies": [ 
 
       { 
 
        "dependsOn": [ 
 
         { 
 
          "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Network/networkSecurityGroups/gtsNSG", 
 
          "resourceGroup": "Tes1Pod", 
 
          "resourceName": "gtsNSG", 
 
          "resourceType": "Microsoft.Network/networkSecurityGroups" 
 
         }, 
 
         { 
 
          "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Network/publicIPAddresses/gtsPublicIP", 
 
          "resourceGroup": "Tes1Pod", 
 
          "resourceName": "gtsPublicIP", 
 
          "resourceType": "Microsoft.Network/publicIPAddresses" 
 
         } 
 
        ], 
 
        "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Network/networkInterfaces/gtsVMNic", 
 
        "resourceGroup": "Tes1Pod", 
 
        "resourceName": "gtsVMNic", 
 
        "resourceType": "Microsoft.Network/networkInterfaces" 
 
       }, 
 
       { 
 
        "dependsOn": [ 
 
         { 
 
          "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Network/networkInterfaces/gtsVMNic", 
 
          "resourceGroup": "Tes1Pod", 
 
          "resourceName": "gtsVMNic", 
 
          "resourceType": "Microsoft.Network/networkInterfaces" 
 
         } 
 
        ], 
 
        "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Compute/virtualMachines/gts", 
 
        "resourceGroup": "Tes1Pod", 
 
        "resourceName": "gts", 
 
        "resourceType": "Microsoft.Compute/virtualMachines" 
 
       } 
 
      ], 
 
      "mode": "Incremental", 
 
      "outputs": {}, 
 
      "parameters": {}, 
 
      "parametersLink": null, 
 
      "providers": [ 
 
       { 
 
        "id": null, 
 
        "namespace": "Microsoft.Network", 
 
        "registrationState": null, 
 
        "resourceTypes": [ 
 
         { 
 
          "aliases": null, 
 
          "apiVersions": null, 
 
          "locations": [ 
 
           "eastus2" 
 
          ], 
 
          "properties": null, 
 
          "resourceType": "networkSecurityGroups" 
 
         }, 
 
         { 
 
          "aliases": null, 
 
          "apiVersions": null, 
 
          "locations": [ 
 
           "eastus2" 
 
          ], 
 
          "properties": null, 
 
          "resourceType": "publicIPAddresses" 
 
         }, 
 
         { 
 
          "aliases": null, 
 
          "apiVersions": null, 
 
          "locations": [ 
 
           "eastus2" 
 
          ], 
 
          "properties": null, 
 
          "resourceType": "networkInterfaces" 
 
         } 
 
        ] 
 
       }, 
 
       { 
 
        "id": null, 
 
        "namespace": "Microsoft.Compute", 
 
        "registrationState": null, 
 
        "resourceTypes": [ 
 
         { 
 
          "aliases": null, 
 
          "apiVersions": null, 
 
          "locations": [ 
 
           "eastus2" 
 
          ], 
 
          "properties": null, 
 
          "resourceType": "virtualMachines" 
 
         } 
 
        ] 
 
       } 
 
      ], 
 
      "provisioningState": "Succeeded", 
 
      "template": null, 
 
      "templateLink": null, 
 
      "timestamp": "2017-09-05T19:42:14.572793+00:00" 
 
     }, 
 
     "resourceGroup": "Tes1Pod" 
 
    }, 
 
    { 
 
     "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Resources/deployments/vm_deploy_xxxxxxxxxxxxxxxx", 
 
     "name": "vm_deploy_xxxxxxxxxxxxxxxx", 
 
     "properties": { 
 
      "correlationId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", 
 
      "debugSetting": null, 
 
      "dependencies": [ 
 
       { 
 
        "dependsOn": [ 
 
         { 
 
          "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Network/networkSecurityGroups/cbrNSG", 
 
          "resourceGroup": "Tes1Pod", 
 
          "resourceName": "cbrNSG", 
 
          "resourceType": "Microsoft.Network/networkSecurityGroups" 
 
         }, 
 
         { 
 
          "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Network/publicIPAddresses/cbrPublicIP", 
 
          "resourceGroup": "Tes1Pod", 
 
          "resourceName": "cbrPublicIP", 
 
          "resourceType": "Microsoft.Network/publicIPAddresses" 
 
         } 
 
        ], 
 
        "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Network/networkInterfaces/cbrVMNic", 
 
        "resourceGroup": "Tes1Pod", 
 
        "resourceName": "cbrVMNic", 
 
        "resourceType": "Microsoft.Network/networkInterfaces" 
 
       }, 
 
       { 
 
        "dependsOn": [ 
 
         { 
 
          "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Network/networkInterfaces/cbrVMNic", 
 
          "resourceGroup": "Tes1Pod", 
 
          "resourceName": "cbrVMNic", 
 
          "resourceType": "Microsoft.Network/networkInterfaces" 
 
         } 
 
        ], 
 
        "id": "/subscriptions/redacted/resourceGroups/Tes1Pod/providers/Microsoft.Compute/virtualMachines/cbr", 
 
        "resourceGroup": "Tes1Pod", 
 
        "resourceName": "cbr", 
 
        "resourceType": "Microsoft.Compute/virtualMachines" 
 
       } 
 
      ], 
 
      "mode": "Incremental", 
 
      "outputs": {}, 
 
      "parameters": {}, 
 
      "parametersLink": null, 
 
      "providers": [ 
 
       { 
 
        "id": null, 
 
        "namespace": "Microsoft.Network", 
 
        "registrationState": null, 
 
        "resourceTypes": [ 
 
         { 
 
          "aliases": null, 
 
          "apiVersions": null, 
 
          "locations": [ 
 
           "eastus2" 
 
          ], 
 
          "properties": null, 
 
          "resourceType": "networkSecurityGroups" 
 
         }, 
 
         { 
 
          "aliases": null, 
 
          "apiVersions": null, 
 
          "locations": [ 
 
           "eastus2" 
 
          ], 
 
          "properties": null, 
 
          "resourceType": "publicIPAddresses" 
 
         }, 
 
         { 
 
          "aliases": null, 
 
          "apiVersions": null, 
 
          "locations": [ 
 
           "eastus2" 
 
          ], 
 
          "properties": null, 
 
          "resourceType": "networkInterfaces" 
 
         } 
 
        ] 
 
       }, 
 
       { 
 
        "id": null, 
 
        "namespace": "Microsoft.Compute", 
 
        "registrationState": null, 
 
        "resourceTypes": [ 
 
         { 
 
          "aliases": null, 
 
          "apiVersions": null, 
 
          "locations": [ 
 
           "eastus2" 
 
          ], 
 
          "properties": null, 
 
          "resourceType": "virtualMachines" 
 
         } 
 
        ] 
 
       } 
 
      ], 
 
      "provisioningState": "Succeeded", 
 
      "template": null, 
 
      "templateLink": null, 
 
      "timestamp": "2017-09-05T19:42:11.590506+00:00" 
 
     }, 
 
     "resourceGroup": "Tes1Pod" 
 
    } 
 
] 
 
}

。私は勇敢にそうしようとしているにもかかわらず、これらをマップすることはできない。

パブリッシャーからの出力はJSONに変換されますが、属性を選択またはマップすることはできません。私はいつも働いていない様々な事柄についてエラーを出します(別名では財産を見つけることができません)。

「定義されていない変数」というメッセージが表示されるたびに、主なエラーは単にprovisioningstatus.properties.provisioningStateを抽出できません。

ok: [localhost] => { 
    "provisioningstatus.properties": "VARIABLE IS NOT DEFINED!" 
} 
+0

このサンプル出力を、 'provisioningstatus'変数の内容を示す可能な' debug'タスクの出力に置き換えることはできますか?また、発生しているエラーの種類(特定のエラーメッセージとそれを生成するタスク)を正確に表示できますか? – larsks

+0

私はそれを行います。約1時間後に従うようになる人もいます。 –

+0

完了。私は尋ねられたように質問を更新しました。 –

答えて

0

あなたはこれを試すことができます。

- debug: 
    msg: "{{ item.properties.provisioningState }}" 
    with_items: "{{ provisioningstatus }}" 
0

をあなたにない正確な答えは質問しながら、私はあなたのansible構成でアームテンプレート展開手順を使用することをお勧め。彼らはあなたのために状態を追跡し、(あなたがとにかく欲しいものです)偶然です。

- name: Deploy Something 
    azure_rm_deployment: 
    deployment_name: SomeDeploymentName 
    resource_group_name: "{{ variable }}" 
    location: '{{ location }}' 
    template: "{{ lookup('file', './templates/azuredeploy.json') }}" 
    parameters: 
     deploymentPrefix: 
     value: "{{ variable }}" 
     redisCacheNames: 
     value: "xxx" 
関連する問題