我有以下Ansible代码,它从azure中的'az'命令读取输入。我基本上想用它来查看资源组何时完成创建我的VMS。
#!/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
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"
}
]
}
所以基本上我想得到属性。每个项目的设置状态(成功与否)。即使我试图勇敢地这样做,我也无法回答这些问题。
ansible的输出转换为JSON,但我无法选择或映射属性。我总是得到各种不起作用的错误(也就是找不到属性等等)。
每次我收到“未定义变量”消息时,主要错误根本无法提取provisioningstatus.properties.provisioningState。
ok: [localhost] => {
"provisioningstatus.properties": "VARIABLE IS NOT DEFINED!"
}
答案 0 :(得分:0)
你可以试试这个:
- debug:
msg: "{{ item.properties.provisioningState }}"
with_items: "{{ provisioningstatus }}"
答案 1 :(得分:0)
虽然不完全是您的问题的答案,但我建议您在ansible配置中使用arm模板部署步骤。他们为你追踪状态并且是幂等的(无论如何你都想要这样)。
- 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"