From 6d952e4124399d1c1c874a1f21135a25cd2f49ad Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Tue, 18 Dec 2018 16:49:31 +0800 Subject: [PATCH] fixing vm state change when vm created with attached disk (#50071) * fixing vm state change when vm created with attached disk * minor fixes * fixing hanging indent --- .../cloud/azure/azure_rm_virtualmachine.py | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py index 876cac9478f..c0ae41052e4 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py @@ -1205,12 +1205,32 @@ class AzureRMVirtualMachine(AzureRMModuleBase): # pass if the availability set is not set pass - vm_resource = self.compute_models.VirtualMachine( - location=vm_dict['location'], - os_profile=self.compute_models.OSProfile( + if 'imageReference' in vm_dict['properties']['storageProfile'].keys(): + if 'id' in vm_dict['properties']['storageProfile']['imageReference'].keys(): + image_reference = self.compute_models.ImageReference( + id=vm_dict['properties']['storageProfile']['imageReference']['id'] + ) + else: + image_reference = self.compute_models.ImageReference( + 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') + ) + else: + image_reference = None + + if 'osProfile' in vm_dict['properties']: + os_profile = self.compute_models.OSProfile( admin_username=vm_dict['properties'].get('osProfile', {}).get('adminUsername'), computer_name=vm_dict['properties'].get('osProfile', {}).get('computerName') - ), + ) + else: + os_profile = None + + vm_resource = self.compute_models.VirtualMachine( + location=vm_dict['location'], + os_profile=os_profile, hardware_profile=self.compute_models.HardwareProfile( vm_size=vm_dict['properties']['hardwareProfile'].get('vmSize') ), @@ -1224,14 +1244,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase): 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'].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') - ), + image_reference=image_reference ), availability_set=availability_set_resource, network_profile=self.compute_models.NetworkProfile( @@ -1243,17 +1256,17 @@ class AzureRMVirtualMachine(AzureRMModuleBase): vm_resource.tags = vm_dict['tags'] # Add custom_data, if provided - if vm_dict['properties']['osProfile'].get('customData'): + if vm_dict['properties'].get('osProfile', {}).get('customData'): custom_data = vm_dict['properties']['osProfile']['customData'] # Azure SDK (erroneously?) wants native string type for this vm_resource.os_profile.custom_data = to_native(base64.b64encode(to_bytes(custom_data))) # Add admin password, if one provided - if vm_dict['properties']['osProfile'].get('adminPassword'): + if vm_dict['properties'].get('osProfile', {}).get('adminPassword'): vm_resource.os_profile.admin_password = vm_dict['properties']['osProfile']['adminPassword'] # Add linux configuration, if applicable - linux_config = vm_dict['properties']['osProfile'].get('linuxConfiguration') + linux_config = vm_dict['properties'].get('osProfile', {}).get('linuxConfiguration') if linux_config: ssh_config = linux_config.get('ssh', None) vm_resource.os_profile.linux_configuration = self.compute_models.LinuxConfiguration(