| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = ''' |
| 14 |
--- |
| 15 |
module: azure_rm_virtualmachine_info |
| 16 |
|
| 17 |
version_added: "0.1.2" |
| 18 |
|
| 19 |
short_description: Get virtual machine facts |
| 20 |
|
| 21 |
description: |
| 22 |
- Get facts for one or all virtual machines in a resource group. |
| 23 |
|
| 24 |
options: |
| 25 |
resource_group: |
| 26 |
description: |
| 27 |
- Name of the resource group containing the virtual machines (required when filtering by vm name). |
| 28 |
type: str |
| 29 |
name: |
| 30 |
description: |
| 31 |
- Name of the virtual machine. |
| 32 |
type: str |
| 33 |
tags: |
| 34 |
description: |
| 35 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 36 |
type: list |
| 37 |
elements: str |
| 38 |
|
| 39 |
extends_documentation_fragment: |
| 40 |
- azure.azcollection.azure |
| 41 |
|
| 42 |
author: |
| 43 |
- Gustavo Muniz do Carmo (@gustavomcarmo) |
| 44 |
- Zim Kalinowski (@zikalino) |
| 45 |
|
| 46 |
''' |
| 47 |
|
| 48 |
EXAMPLES = ''' |
| 49 |
- name: Get facts for all virtual machines of a resource group |
| 50 |
azure_rm_virtualmachine_info: |
| 51 |
resource_group: myResourceGroup |
| 52 |
|
| 53 |
- name: Get facts by name |
| 54 |
azure_rm_virtualmachine_info: |
| 55 |
resource_group: myResourceGroup |
| 56 |
name: myVm |
| 57 |
|
| 58 |
- name: Get facts by tags |
| 59 |
azure_rm_virtualmachine_info: |
| 60 |
resource_group: myResourceGroup |
| 61 |
tags: |
| 62 |
- testing |
| 63 |
- foo:bar |
| 64 |
''' |
| 65 |
|
| 66 |
RETURN = ''' |
| 67 |
vms: |
| 68 |
description: |
| 69 |
- List of virtual machines. |
| 70 |
returned: always |
| 71 |
type: complex |
| 72 |
contains: |
| 73 |
additional_capabilities: |
| 74 |
description: |
| 75 |
- Enables or disables a capability on the virtual machine. |
| 76 |
type: dict |
| 77 |
returned: always |
| 78 |
sample: {ultra_ssd_enabled: False} |
| 79 |
admin_username: |
| 80 |
description: |
| 81 |
- Administrator user name. |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: admin |
| 85 |
boot_diagnostics: |
| 86 |
description: |
| 87 |
- Information about the boot diagnostics settings. |
| 88 |
returned: always |
| 89 |
type: complex |
| 90 |
contains: |
| 91 |
enabled: |
| 92 |
description: |
| 93 |
- Indicates if boot diagnostics are enabled. |
| 94 |
returned: always |
| 95 |
type: bool |
| 96 |
sample: true |
| 97 |
storage_uri: |
| 98 |
description: |
| 99 |
- Indicates the storage account used by boot diagnostics. |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
sample: https://mystorageaccountname.blob.core.windows.net/ |
| 103 |
console_screenshot_uri: |
| 104 |
description: |
| 105 |
- Contains a URI to grab a console screenshot. |
| 106 |
- Only present if enabled. |
| 107 |
returned: always |
| 108 |
type: str |
| 109 |
sample: https://mystorageaccountname.blob.core.windows.net/bootdiagnostics-myvm01-a4db09a6-ab7f-4d80-9da8-fbceaef9288a/ |
| 110 |
myVm.a4db09a6-ab7f-4d80-9da8-fbceaef9288a.screenshot.bmp |
| 111 |
serial_console_log_uri: |
| 112 |
description: |
| 113 |
- Contains a URI to grab the serial console log. |
| 114 |
- Only present if enabled. |
| 115 |
returned: always |
| 116 |
type: str |
| 117 |
sample: https://mystorageaccountname.blob.core.windows.net/bootdiagnostics-myvm01-a4db09a6-ab7f-4d80-9da8-fbceaef9288a/ |
| 118 |
myVm.a4db09a6-ab7f-4d80-9da8-fbceaef9288a.serialconsole.log |
| 119 |
data_disks: |
| 120 |
description: |
| 121 |
- List of attached data disks. |
| 122 |
returned: always |
| 123 |
type: complex |
| 124 |
contains: |
| 125 |
caching: |
| 126 |
description: |
| 127 |
- Type of data disk caching. |
| 128 |
returned: always |
| 129 |
type: str |
| 130 |
sample: ReadOnly |
| 131 |
disk_size_gb: |
| 132 |
description: |
| 133 |
- The initial disk size in GB for blank data disks. |
| 134 |
returned: always |
| 135 |
type: int |
| 136 |
sample: 64 |
| 137 |
lun: |
| 138 |
description: |
| 139 |
- The logical unit number for data disk. |
| 140 |
returned: always |
| 141 |
type: int |
| 142 |
sample: 0 |
| 143 |
managed_disk_type: |
| 144 |
description: |
| 145 |
- Managed data disk type. |
| 146 |
returned: always |
| 147 |
type: str |
| 148 |
sample: Standard_LRS |
| 149 |
managed_disk_id: |
| 150 |
description: |
| 151 |
- Managed data disk ID. |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/Microsoft.Compute/disks/diskName |
| 155 |
write_accelerator_enabled: |
| 156 |
description: |
| 157 |
- Specifies whether writeAccelerator should be enabled or disabled on the disk. |
| 158 |
type: bool |
| 159 |
returned: always |
| 160 |
sample: False |
| 161 |
user_data: |
| 162 |
description: |
| 163 |
- UserData for the VM, which must be base-64 encoded. |
| 164 |
type: str |
| 165 |
returned: always |
| 166 |
sample: None |
| 167 |
id: |
| 168 |
description: |
| 169 |
- Resource ID. |
| 170 |
returned: always |
| 171 |
type: str |
| 172 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVm |
| 173 |
image: |
| 174 |
description: |
| 175 |
- Image specification. |
| 176 |
returned: always |
| 177 |
type: complex |
| 178 |
contains: |
| 179 |
offer: |
| 180 |
description: |
| 181 |
- The offer of the platform image or marketplace image used to create the virtual machine. |
| 182 |
type: str |
| 183 |
returned: when created from marketplace image |
| 184 |
sample: RHEL |
| 185 |
publisher: |
| 186 |
description: |
| 187 |
- Publisher name. |
| 188 |
type: str |
| 189 |
returned: when created from marketplace image |
| 190 |
sample: RedHat |
| 191 |
sku: |
| 192 |
description: |
| 193 |
- SKU name. |
| 194 |
type: str |
| 195 |
returned: when created from marketplace image |
| 196 |
sample: 7-RAW |
| 197 |
version: |
| 198 |
description: |
| 199 |
- Image version. |
| 200 |
type: str |
| 201 |
returned: when created from marketplace image |
| 202 |
sample: 7.5.2018050901 |
| 203 |
id: |
| 204 |
description: |
| 205 |
- Custom image resource ID. |
| 206 |
type: str |
| 207 |
returned: when created from custom image |
| 208 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myImage |
| 209 |
community_gallery_image_id: |
| 210 |
description: |
| 211 |
- The community gallery image unique id for vm deployment. |
| 212 |
type: str |
| 213 |
returned: when created from community gallery image |
| 214 |
sample: "/CommunityGalleries/yellowbrick-fc7e81f1-87dd-4989-9ca8-03743762e873/Images/Ubuntu-5.15.0-1035-azure_22.04" |
| 215 |
location: |
| 216 |
description: |
| 217 |
- Resource location. |
| 218 |
returned: always |
| 219 |
type: str |
| 220 |
sample: japaneast |
| 221 |
maintenance_redeploy_status: |
| 222 |
description: |
| 223 |
- If maintenance is scheduled for this server set to something like example, otherwise empty dict |
| 224 |
returned: always |
| 225 |
type: dict |
| 226 |
sample: { "is_customer_initiated_maintenance_allowed": true, "last_operation_result_code": "None", "maintenance_window_end_time": |
| 227 |
"2025-06-18T23:59:00.000Z", "maintenance_window_start_time": "2025-06-12T00:00:00.000Z", "pre_maintenance_window_end_time": |
| 228 |
"2025-06-11T23:30:00.000Z", "pre_maintenance_window_start_time": "2025-01-30T00:00:00.000Z" } |
| 229 |
name: |
| 230 |
description: |
| 231 |
- Resource name. |
| 232 |
returned: always |
| 233 |
type: str |
| 234 |
sample: myVm |
| 235 |
network_interface_names: |
| 236 |
description: |
| 237 |
- List of attached network interfaces. |
| 238 |
returned: always |
| 239 |
type: list |
| 240 |
sample: [ |
| 241 |
"myNetworkInterface" |
| 242 |
] |
| 243 |
proximityPlacementGroup: |
| 244 |
description: |
| 245 |
- The name or ID of the proximity placement group the VM should be associated with. |
| 246 |
type: dict |
| 247 |
returned: always |
| 248 |
sample: { "id": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Compute/proximityPlacementGroups/testid13"} |
| 249 |
CapacityReservation: |
| 250 |
description: |
| 251 |
- The name or ID of the capacity reservation group to be associated with. |
| 252 |
type: dict |
| 253 |
returned: always |
| 254 |
sample: { } |
| 255 |
os_disk_caching: |
| 256 |
description: |
| 257 |
- Type of OS disk caching. |
| 258 |
returned: always |
| 259 |
type: str |
| 260 |
sample: ReadOnly |
| 261 |
os_type: |
| 262 |
description: |
| 263 |
- Base type of operating system. |
| 264 |
returned: always |
| 265 |
type: str |
| 266 |
sample: Linux |
| 267 |
resource_group: |
| 268 |
description: |
| 269 |
- Resource group. |
| 270 |
returned: always |
| 271 |
type: str |
| 272 |
sample: myResourceGroup |
| 273 |
state: |
| 274 |
description: |
| 275 |
- State of the resource. |
| 276 |
returned: always |
| 277 |
type: str |
| 278 |
sample: present |
| 279 |
tags: |
| 280 |
description: |
| 281 |
- Resource tags. |
| 282 |
returned: always |
| 283 |
type: dict |
| 284 |
sample: { "key1":"value1" } |
| 285 |
vm_agent_version: |
| 286 |
description: |
| 287 |
- Version of the Azure VM Agent (waagent) running inside the VM. |
| 288 |
returned: always |
| 289 |
type: str |
| 290 |
sample: '2.9.1.1' |
| 291 |
vm_size: |
| 292 |
description: |
| 293 |
- Virtual machine size. |
| 294 |
returned: always |
| 295 |
type: str |
| 296 |
sample: Standard_D4 |
| 297 |
zones: |
| 298 |
description: |
| 299 |
- A list of Availability Zones for your VM. |
| 300 |
type: list |
| 301 |
sample: [1] |
| 302 |
power_state: |
| 303 |
description: |
| 304 |
- Power state of the virtual machine. |
| 305 |
returned: always |
| 306 |
type: str |
| 307 |
sample: running |
| 308 |
display_status: |
| 309 |
description: |
| 310 |
- The short localizable label for the status. |
| 311 |
returned: always |
| 312 |
type: str |
| 313 |
sample: "VM running" |
| 314 |
provisioning_state: |
| 315 |
description: |
| 316 |
- The provisioning state, which only appears in the response. |
| 317 |
returned: always |
| 318 |
type: str |
| 319 |
sample: running |
| 320 |
identity: |
| 321 |
description: |
| 322 |
- The identity of the virtual machine. |
| 323 |
type: dict |
| 324 |
returned: always |
| 325 |
sample: { |
| 326 |
"principal_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 327 |
"tenant_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 328 |
"type": "SystemAssigned, UserAssigned", |
| 329 |
"user_assigned_identities": { |
| 330 |
"/subscriptions/xxx/resourceGroups/testRG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test": { |
| 331 |
"client_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 332 |
"principal_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
security_profile: |
| 337 |
description: |
| 338 |
- Specifies the Security related profile settings for the virtual machine. |
| 339 |
type: complex |
| 340 |
returned: when-used |
| 341 |
contains: |
| 342 |
encryption_at_host: |
| 343 |
description: |
| 344 |
- This property can be used by user in the request to enable or disable the Host Encryption for the virtual machine. |
| 345 |
- This will enable the encryption for all the disks including Resource/Temp disk at host itself. |
| 346 |
type: bool |
| 347 |
returned: when-enabled |
| 348 |
sample: True |
| 349 |
security_type: |
| 350 |
description: |
| 351 |
- Specifies the SecurityType of the virtual machine. |
| 352 |
- It is set as TrustedLaunch to enable UefiSettings. |
| 353 |
type: str |
| 354 |
returned: when-enabled |
| 355 |
sample: TrustedLaunch |
| 356 |
uefi_settings: |
| 357 |
description: |
| 358 |
- Specifies the security settings like secure boot and vTPM used while creating the virtual machine. |
| 359 |
type: complex |
| 360 |
returned: when-enabled |
| 361 |
contains: |
| 362 |
secure_boot_enabled: |
| 363 |
description: |
| 364 |
- Specifies whether secure boot should be enabled on the virtual machine. |
| 365 |
type: bool |
| 366 |
returned: when-enabled |
| 367 |
sample: True |
| 368 |
v_tpm_enabled: |
| 369 |
description: |
| 370 |
- Specifies whether vTPM should be enabled on the virtual machine. |
| 371 |
type: bool |
| 372 |
returned: when-enabled |
| 373 |
sample: True |
| 374 |
''' |
| 375 |
|
| 376 |
try: |
| 377 |
from azure.mgmt.core.tools import parse_resource_id |
| 378 |
from azure.core.exceptions import ResourceNotFoundError |
| 379 |
except Exception: |
| 380 |
|
| 381 |
pass |
| 382 |
|
| 383 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 384 |
from ansible.module_utils.six.moves.urllib.parse import urlparse |
| 385 |
import re |
| 386 |
|
| 387 |
|
| 388 |
AZURE_OBJECT_CLASS = 'VirtualMachine' |
| 389 |
|
| 390 |
AZURE_ENUM_MODULES = ['azure.mgmt.compute.models'] |
| 391 |
|
| 392 |
|
| 393 |
class AzureRMVirtualMachineInfo(AzureRMModuleBase): |
| 394 |
|
| 395 |
def __init__(self): |
| 396 |
|
| 397 |
self.module_arg_spec = dict( |
| 398 |
resource_group=dict(type='str'), |
| 399 |
name=dict(type='str'), |
| 400 |
tags=dict(type='list', elements='str') |
| 401 |
) |
| 402 |
|
| 403 |
self.results = dict( |
| 404 |
changed=False, |
| 405 |
vms=[] |
| 406 |
) |
| 407 |
|
| 408 |
self.resource_group = None |
| 409 |
self.name = None |
| 410 |
self.tags = None |
| 411 |
|
| 412 |
super(AzureRMVirtualMachineInfo, self).__init__(self.module_arg_spec, |
| 413 |
supports_check_mode=True, |
| 414 |
supports_tags=False, |
| 415 |
facts_module=True) |
| 416 |
|
| 417 |
def exec_module(self, **kwargs): |
| 418 |
|
| 419 |
for key in self.module_arg_spec: |
| 420 |
setattr(self, key, kwargs[key]) |
| 421 |
|
| 422 |
if self.name and not self.resource_group: |
| 423 |
self.fail("Parameter error: resource group required when filtering by name.") |
| 424 |
if self.name: |
| 425 |
self.results['vms'] = self.get_item() |
| 426 |
elif self.resource_group: |
| 427 |
self.results['vms'] = self.list_items_by_resourcegroup() |
| 428 |
else: |
| 429 |
self.results['vms'] = self.list_all_items() |
| 430 |
|
| 431 |
return self.results |
| 432 |
|
| 433 |
def get_item(self): |
| 434 |
self.log('Get properties for {0}'.format(self.name)) |
| 435 |
item = None |
| 436 |
result = [] |
| 437 |
|
| 438 |
item = self.get_vm(self.resource_group, self.name) |
| 439 |
|
| 440 |
if item and self.has_tags(item.get('tags'), self.tags): |
| 441 |
result = [item] |
| 442 |
|
| 443 |
return result |
| 444 |
|
| 445 |
def list_items_by_resourcegroup(self): |
| 446 |
self.log('List all items') |
| 447 |
try: |
| 448 |
items = self.compute_client.virtual_machines.list(self.resource_group) |
| 449 |
except ResourceNotFoundError as exc: |
| 450 |
self.fail("Failed to list all items - {0}".format(str(exc))) |
| 451 |
|
| 452 |
results = [] |
| 453 |
for item in items: |
| 454 |
if self.has_tags(item.tags, self.tags): |
| 455 |
results.append(self.get_vm(self.resource_group, item.name)) |
| 456 |
return results |
| 457 |
|
| 458 |
def list_all_items(self): |
| 459 |
self.log('List all items') |
| 460 |
try: |
| 461 |
items = self.compute_client.virtual_machines.list_all() |
| 462 |
except ResourceNotFoundError as exc: |
| 463 |
self.fail("Failed to list all items - {0}".format(str(exc))) |
| 464 |
|
| 465 |
results = [] |
| 466 |
for item in items: |
| 467 |
if self.has_tags(item.tags, self.tags): |
| 468 |
results.append(self.get_vm(parse_resource_id(item.id).get('resource_group'), item.name)) |
| 469 |
return results |
| 470 |
|
| 471 |
def get_vm(self, resource_group, name): |
| 472 |
''' |
| 473 |
Get the VM with expanded instanceView |
| 474 |
|
| 475 |
:return: VirtualMachine object |
| 476 |
''' |
| 477 |
try: |
| 478 |
vm = self.compute_client.virtual_machines.get(resource_group, name, expand='instanceview') |
| 479 |
return self.serialize_vm(vm) |
| 480 |
except ResourceNotFoundError as exc: |
| 481 |
self.fail("Error getting virtual machine {0} - {1}".format(self.name, str(exc))) |
| 482 |
|
| 483 |
def serialize_vm(self, vm): |
| 484 |
''' |
| 485 |
Convert a VirtualMachine object to dict. |
| 486 |
|
| 487 |
:param vm: VirtualMachine object |
| 488 |
:return: dict |
| 489 |
''' |
| 490 |
|
| 491 |
result = self.serialize_obj(vm, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES) |
| 492 |
resource_group = parse_resource_id(result['id']).get('resource_group') |
| 493 |
instance = None |
| 494 |
power_state = None |
| 495 |
display_status = None |
| 496 |
|
| 497 |
try: |
| 498 |
instance = self.compute_client.virtual_machines.instance_view(resource_group, vm.name) |
| 499 |
instance = self.serialize_obj(instance, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES) |
| 500 |
except Exception as exc: |
| 501 |
self.fail("Error getting virtual machine {0} instance view - {1}".format(vm.name, str(exc))) |
| 502 |
|
| 503 |
for index in range(len(instance['statuses'])): |
| 504 |
code = instance['statuses'][index]['code'].split('/') |
| 505 |
if code[0] == 'PowerState': |
| 506 |
power_state = code[1] |
| 507 |
display_status = instance['statuses'][index]['display_status'] |
| 508 |
elif code[0] == 'OSState' and code[1] == 'generalized': |
| 509 |
display_status = instance['statuses'][index]['display_status'] |
| 510 |
power_state = 'generalized' |
| 511 |
break |
| 512 |
elif code[0] == 'ProvisioningState' and code[1] == 'failed': |
| 513 |
display_status = instance['statuses'][index]['display_status'] |
| 514 |
power_state = '' |
| 515 |
break |
| 516 |
|
| 517 |
new_result = {} |
| 518 |
|
| 519 |
if instance.get('maintenance_redeploy_status') is not None: |
| 520 |
new_result['maintenance_redeploy_status'] = instance['maintenance_redeploy_status'] |
| 521 |
else: |
| 522 |
new_result['maintenance_redeploy_status'] = dict() |
| 523 |
|
| 524 |
if instance.get('vm_agent') is not None: |
| 525 |
new_result['vm_agent_version'] = instance['vm_agent'].get('vm_agent_version') |
| 526 |
else: |
| 527 |
new_result['vm_agent_version'] = 'Unknown' |
| 528 |
|
| 529 |
if vm.security_profile is not None: |
| 530 |
new_result['security_profile'] = dict() |
| 531 |
if vm.security_profile.encryption_at_host is not None: |
| 532 |
new_result['security_profile']['encryption_at_host'] = vm.security_profile.encryption_at_host |
| 533 |
if vm.security_profile.security_type is not None: |
| 534 |
new_result['security_profile']['security_type'] = vm.security_profile.security_type |
| 535 |
if vm.security_profile.uefi_settings is not None: |
| 536 |
new_result['security_profile']['uefi_settings'] = dict() |
| 537 |
if vm.security_profile.uefi_settings.secure_boot_enabled is not None: |
| 538 |
new_result['security_profile']['uefi_settings']['secure_boot_enabled'] = vm.security_profile.uefi_settings.secure_boot_enabled |
| 539 |
if vm.security_profile.uefi_settings.v_tpm_enabled is not None: |
| 540 |
new_result['security_profile']['uefi_settings']['v_tpm_enabled'] = vm.security_profile.uefi_settings.v_tpm_enabled |
| 541 |
|
| 542 |
new_result['power_state'] = power_state |
| 543 |
new_result['display_status'] = display_status |
| 544 |
new_result['provisioning_state'] = vm.provisioning_state |
| 545 |
new_result['id'] = vm.id |
| 546 |
new_result['resource_group'] = resource_group |
| 547 |
new_result['name'] = vm.name |
| 548 |
new_result['state'] = 'present' |
| 549 |
new_result['location'] = vm.location |
| 550 |
new_result['vm_size'] = result['hardware_profile']['vm_size'] |
| 551 |
new_result['user_data'] = result.get('user_data', None) |
| 552 |
|
| 553 |
new_result['proximityPlacementGroup'] = result.get('proximity_placement_group') |
| 554 |
new_result['zones'] = result.get('zones', None) |
| 555 |
new_result['additional_capabilities'] = result.get('additional_capabilities') |
| 556 |
new_result['identity'] = result.get('identity') |
| 557 |
new_result['capacity_reservation'] = dict() |
| 558 |
if result.get('capacity_reservation') is not None: |
| 559 |
new_result['capacity_reservation']['capacity_reservation_group'] = result.get('capacity_reservation', {}).get('capacity_reservation_group') |
| 560 |
os_profile = result.get('os_profile') |
| 561 |
if os_profile is not None: |
| 562 |
new_result['admin_username'] = os_profile.get('admin_username') |
| 563 |
image = result['storage_profile'].get('image_reference') |
| 564 |
if image is not None: |
| 565 |
if image.get('publisher', None) is not None: |
| 566 |
new_result['image'] = { |
| 567 |
'publisher': image['publisher'], |
| 568 |
'sku': image['sku'], |
| 569 |
'offer': image['offer'], |
| 570 |
'version': image['version'] |
| 571 |
} |
| 572 |
elif image.get('community_gallery_image_id') is not None: |
| 573 |
new_result['image'] = { |
| 574 |
'community_gallery_image_id': image.get('community_gallery_image_id') |
| 575 |
} |
| 576 |
else: |
| 577 |
new_result['image'] = { |
| 578 |
'id': image.get('id', None) |
| 579 |
} |
| 580 |
|
| 581 |
new_result['boot_diagnostics'] = { |
| 582 |
'enabled': 'diagnostics_profile' in result and |
| 583 |
'boot_diagnostics' in result['diagnostics_profile'] and |
| 584 |
result['diagnostics_profile']['boot_diagnostics']['enabled'] or False, |
| 585 |
'storage_uri': 'diagnostics_profile' in result and |
| 586 |
'boot_diagnostics' in result['diagnostics_profile'] and |
| 587 |
result['diagnostics_profile']['boot_diagnostics'].get('storageUri', None) |
| 588 |
} |
| 589 |
if new_result['boot_diagnostics']['enabled']: |
| 590 |
new_result['boot_diagnostics']['console_screenshot_uri'] = result['instance_view']['boot_diagnostics'].get('console_screenshot_blob_uri') |
| 591 |
new_result['boot_diagnostics']['serial_console_log_uri'] = result['instance_view']['boot_diagnostics'].get('serial_console_log_blob_uri') |
| 592 |
|
| 593 |
vhd = result['storage_profile']['os_disk'].get('vhd') |
| 594 |
if vhd is not None: |
| 595 |
url = urlparse(vhd['uri']) |
| 596 |
new_result['storage_account_name'] = url.netloc.split('.')[0] |
| 597 |
new_result['storage_container_name'] = url.path.split('/')[1] |
| 598 |
new_result['storage_blob_name'] = url.path.split('/')[-1] |
| 599 |
|
| 600 |
new_result['os_disk'] = result['storage_profile']['os_disk'] |
| 601 |
new_result['os_disk_caching'] = result['storage_profile']['os_disk']['caching'] |
| 602 |
new_result['os_type'] = result['storage_profile']['os_disk']['os_type'] |
| 603 |
new_result['data_disks'] = [] |
| 604 |
disks = result['storage_profile']['data_disks'] |
| 605 |
for disk_index in range(len(disks)): |
| 606 |
new_result['data_disks'].append({ |
| 607 |
'lun': disks[disk_index].get('lun'), |
| 608 |
'name': disks[disk_index].get('name'), |
| 609 |
'disk_size_gb': disks[disk_index].get('disk_size_gb'), |
| 610 |
'managed_disk_type': disks[disk_index].get('managed_disk', {}).get('storage_account_type'), |
| 611 |
'managed_disk_id': disks[disk_index].get('managed_disk', {}).get('id'), |
| 612 |
'caching': disks[disk_index].get('caching'), |
| 613 |
'write_accelerator_enabled': disks[disk_index].get('write_accelerator_enabled', False) |
| 614 |
}) |
| 615 |
|
| 616 |
new_result['network_interface_names'] = [] |
| 617 |
nics = result['network_profile']['network_interfaces'] |
| 618 |
for nic_index in range(len(nics)): |
| 619 |
new_result['network_interface_names'].append(re.sub('.*networkInterfaces/', '', nics[nic_index]['id'])) |
| 620 |
|
| 621 |
new_result['tags'] = vm.tags |
| 622 |
return new_result |
| 623 |
|
| 624 |
|
| 625 |
def main(): |
| 626 |
AzureRMVirtualMachineInfo() |
| 627 |
|
| 628 |
|
| 629 |
if __name__ == '__main__': |
| 630 |
main() |