From cb844ae32b896f116bde9f5c3f76a096fd666ba5 Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Tue, 19 Feb 2019 05:50:55 +0800 Subject: [PATCH] Backport/2.7/45269 (#52466) * Fixing issues with azure_rm_virtualmachine crashing (#45269) * vm fixes (cherry picked from commit f04139bfa3845a29f5acd4449506a33d5b891b3e) * added changelog --- .../fragments/45269-fix-vm-crashing.yaml | 2 ++ .../cloud/azure/azure_rm_virtualmachine.py | 34 +++++++++---------- 2 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 changelogs/fragments/45269-fix-vm-crashing.yaml diff --git a/changelogs/fragments/45269-fix-vm-crashing.yaml b/changelogs/fragments/45269-fix-vm-crashing.yaml new file mode 100644 index 00000000000..db8d1a16ef6 --- /dev/null +++ b/changelogs/fragments/45269-fix-vm-crashing.yaml @@ -0,0 +1,2 @@ +bugfixes: +- azure_rm_virtualmachine - fixed several crashes in module diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py index a63623d308d..e9b9f92c7d7 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py @@ -895,7 +895,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase): vm_dict['properties']['storageProfile']['osDisk']['caching'] = self.os_disk_caching if self.os_disk_size_gb and \ - self.os_disk_size_gb != vm_dict['properties']['storageProfile']['osDisk']['diskSizeGB']: + self.os_disk_size_gb != vm_dict['properties']['storageProfile']['osDisk'].get('diskSizeGB'): self.log('CHANGED: virtual machine {0} - OS disk size '.format(self.name)) differences.append('OS Disk size') changed = True @@ -1154,16 +1154,16 @@ class AzureRMVirtualMachine(AzureRMModuleBase): # os disk if not vm_dict['properties']['storageProfile']['osDisk'].get('managedDisk'): managed_disk = None - vhd = self.compute_models.VirtualHardDisk(uri=vm_dict['properties']['storageProfile']['osDisk']['vhd']['uri']) + vhd = self.compute_models.VirtualHardDisk(uri=vm_dict['properties']['storageProfile']['osDisk'].get('vhd', {}).get('uri')) else: vhd = None managed_disk = self.compute_models.ManagedDiskParameters( - storage_account_type=vm_dict['properties']['storageProfile']['osDisk']['managedDisk']['storageAccountType'] + storage_account_type=vm_dict['properties']['storageProfile']['osDisk']['managedDisk'].get('storageAccountType') ) availability_set_resource = None try: - availability_set_resource = self.compute_models.SubResource(vm_dict['properties']['availabilitySet']['id']) + availability_set_resource = self.compute_models.SubResource(vm_dict['properties']['availabilitySet'].get('id')) except Exception: # pass if the availability set is not set pass @@ -1171,29 +1171,29 @@ class AzureRMVirtualMachine(AzureRMModuleBase): vm_resource = self.compute_models.VirtualMachine( vm_dict['location'], os_profile=self.compute_models.OSProfile( - admin_username=vm_dict['properties']['osProfile']['adminUsername'], - computer_name=vm_dict['properties']['osProfile']['computerName'] + admin_username=vm_dict['properties'].get('osProfile', {}).get('adminUsername'), + computer_name=vm_dict['properties'].get('osProfile', {}).get('computerName') ), hardware_profile=self.compute_models.HardwareProfile( - vm_size=vm_dict['properties']['hardwareProfile']['vmSize'] + vm_size=vm_dict['properties']['hardwareProfile'].get('vmSize') ), storage_profile=self.compute_models.StorageProfile( os_disk=self.compute_models.OSDisk( - name=vm_dict['properties']['storageProfile']['osDisk']['name'], + name=vm_dict['properties']['storageProfile']['osDisk'].get('name'), vhd=vhd, managed_disk=managed_disk, - create_option=vm_dict['properties']['storageProfile']['osDisk']['createOption'], - os_type=vm_dict['properties']['storageProfile']['osDisk']['osType'], - caching=vm_dict['properties']['storageProfile']['osDisk']['caching'], - disk_size_gb=vm_dict['properties']['storageProfile']['osDisk']['diskSizeGB'] + create_option=vm_dict['properties']['storageProfile']['osDisk'].get('createOption'), + os_type=vm_dict['properties']['storageProfile']['osDisk'].get('osType'), + caching=vm_dict['properties']['storageProfile']['osDisk'].get('caching'), + disk_size_gb=vm_dict['properties']['storageProfile']['osDisk'].get('diskSizeGB') ), image_reference=self.compute_models.ImageReference( id=vm_dict['properties']['storageProfile']['imageReference']['id'], ) if 'id' in vm_dict['properties']['storageProfile']['imageReference'].keys() else self.compute_models.ImageReference( - publisher=vm_dict['properties']['storageProfile']['imageReference']['publisher'], - offer=vm_dict['properties']['storageProfile']['imageReference']['offer'], - sku=vm_dict['properties']['storageProfile']['imageReference']['sku'], - version=vm_dict['properties']['storageProfile']['imageReference']['version'] + publisher=vm_dict['properties']['storageProfile']['imageReference'].get('publisher'), + offer=vm_dict['properties']['storageProfile']['imageReference'].get('offer'), + sku=vm_dict['properties']['storageProfile']['imageReference'].get('sku'), + version=vm_dict['properties']['storageProfile']['imageReference'].get('version') ), ), availability_set=availability_set_resource, @@ -1237,7 +1237,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase): for data_disk in vm_dict['properties']['storageProfile']['dataDisks']: if data_disk.get('managedDisk'): - managed_disk_type = data_disk['managedDisk']['storageAccountType'] + managed_disk_type = data_disk['managedDisk'].get('storageAccountType') data_disk_managed_disk = self.compute_models.ManagedDiskParameters(storage_account_type=managed_disk_type) data_disk_vhd = None else: