I am trying to get back into some Ansible automation after some absence but get stuck immediately trying to get the IP address of an Azure VM from ansible facts:
ok: [localhost] => {
“interface_facts.ansible_facts.azure_networkinterfaces”: [
{
“etag”: “W/"a4csdf-b1120d-5c9fb31354129"”,
“id”: “/subscriptions/a444df-b1120d-5c9fb31-354129-2gbf3e5b/resourceGroups/testRG/providers/Microsoft.Network/networkInterfaces/testvm1-eth0”,
“location”: “westeurope”,
“properties”: {
“dnsSettings”: {
“appliedDnsServers”: ,
“dnsServers”: ,
“internalDomainNameSuffix”: “kttxupoc3ofsdf25452peia.ax.internal.cloudapp.net”
},
“enableIPForwarding”: false,
“ipConfigurations”: [
{
“etag”: “W/"ae60d652-3b57-4cfd-b10d-5c9fb11296e6"”,
“id”: “/subscriptions/a444df-b1120d-5c9fb31-354129-2gbf3e5b/resourceGroups/testRG/providers/Microsoft.Network/networkInterfaces/testvm1-eth0/ipConfigurations/default”,
“name”: “default”,
“properties”: {
“privateIPAddress”: “192.168.1.2”,
“privateIPAddressVersion”: “IPv4”,
“privateIPAllocationMethod”: “Dynamic”,
“provisioningState”: “Succeeded”,
“subnet”: {
“id”: “/subscriptions/a444df-b1120d-5c9fb31-354129-2gbf3e5b/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworks/IaaS-Vnet/subnets/IaaS-Test-FE-Subnet”
}
}
}
],
“macAddress”: “0E-0D-3E-29-FE-AF”,
“networkSecurityGroup”: {
“id”: “/subscriptions/a4csdf-b1120d-5c9fb31354129/resourceGroups/testRG/providers/Microsoft.Network/networkSecurityGroups/testvm1-eth001”
},
“primary”: true,
“provisioningState”: “Succeeded”,
“resourceGuid”: “a7182a83-61bc-3bed-9758-2d72f251952d”,
“virtualMachine”: {
“id”: “/subscriptions/1a4csdf-b1120d-5c9fb31329-e5b/resourceGroups/testRG/providers/Microsoft.Compute/virtualMachines/testvm1”
}
}
}
]
}
I Thought that I could get the IP by using "interface_facts.ansible_facts.azure_networkinterfaces.properties,ipConfigurations.properties.privateIPAddress
But no amounts of dots and square brackets gets me past the interface_facts.ansible_facts.azure_networkinterfaces level, anything below just responds “VARIABLE IS NOT DEFINED!”
I am pretty sure this is just a YAML list/dict noob issue, but after hours of searching I am still lost.
Can anyone please point me in the right direction?
I am trying to get back into some Ansible automation after some absence but
get stuck immediately trying to get the IP address of an Azure VM from
ansible facts:
ok: [localhost] => {
"*interface_facts.ansible_facts.azure_networkinterfaces*": [
Here you have a list.
{
"etag": "W/\"a4csdf-b1120d-5c9fb31354129\"",
"id":
"/subscriptions/a444df-b1120d-5c9fb31-354129-2gbf3e5b/resourceGroups/testRG/providers/Microsoft.Network/networkInterfaces/testvm1-eth0",
"location": "westeurope",
"*properties*": {
"dnsSettings": {
"appliedDnsServers": ,
"dnsServers": ,
"internalDomainNameSuffix":
"kttxupoc3ofsdf25452peia.ax.internal.cloudapp.net"
},
"enableIPForwarding": false,
"*ipConfigurations*": [
Here you have a list.
{
"etag":
"W/\"ae60d652-3b57-4cfd-b10d-5c9fb11296e6\"",
"id":
"/subscriptions/a444df-b1120d-5c9fb31-354129-2gbf3e5b/resourceGroups/testRG/providers/Microsoft.Network/networkInterfaces/testvm1-eth0/ipConfigurations/default",
"name": "default",
"*properties*": {
"privateIPAddress": "192.168.1.2",
*"privateIPAddressVersion": "IPv4",*
"privateIPAllocationMethod": "Dynamic",
"provisioningState": "Succeeded",
"subnet": {
"id":
"/subscriptions/a444df-b1120d-5c9fb31-354129-2gbf3e5b/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworks/IaaS-Vnet/subnets/IaaS-Test-FE-Subnet"
}
}
}
],
"macAddress": "0E-0D-3E-29-FE-AF",
"networkSecurityGroup": {
"id":
"/subscriptions/a4csdf-b1120d-5c9fb31354129/resourceGroups/testRG/providers/Microsoft.Network/networkSecurityGroups/testvm1-eth001"
},
"primary": true,
"provisioningState": "Succeeded",
"resourceGuid": "a7182a83-61bc-3bed-9758-2d72f251952d",
"virtualMachine": {
"id":
"/subscriptions/1a4csdf-b1120d-5c9fb31329-e5b/resourceGroups/testRG/providers/Microsoft.Compute/virtualMachines/testvm1"
}
}
}
]
}
I Thought that I could get the IP by using
"interface_facts.ansible_facts.azure_networkinterfaces.properties,ipConfigurations.properties.privateIPAddress
You have two list in you structure to reach privateIPAddress, try
interface_facts.ansible_facts.azure_networkinterfaces.0.properties.ipConfigurations.0.properties.privateIPAddress
it can also be written like this
interface_facts.ansible_facts.azure_networkinterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress
Solved, and for posterity the answer is: interface_facts.ansible_facts.azure_networkinterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress