From 1ae365c11295001b17347dd1a6aea24494c69c62 Mon Sep 17 00:00:00 2001 From: "James E. King III" Date: Fri, 11 Jan 2019 09:09:26 -0500 Subject: [PATCH] azure_rm_managed_disk: properly handle os_type on creation and update (#49385) --- .../cloud/azure/azure_rm_managed_disk.py | 34 ++++++++++++-- .../azure/azure_rm_managed_disk_facts.py | 2 +- .../azure_rm_managed_disk/tasks/main.yml | 44 ++++++++++++++++++- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py b/lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py index e4cb9d42207..9ce75c01b7b 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py @@ -64,13 +64,18 @@ options: - source_resource_uri os_type: description: - - "Type of Operating System: C(linux) or C(windows). Used when I(create_option) is either C(copy) or C(import) and the source is an OS disk." + - "Type of Operating System: C(linux) or C(windows)." + - "Used when I(create_option) is either C(copy) or C(import) and the source is an OS disk." + - "If omitted during creation, no value is set." + - "If omitted during an update, no change is made." + - "Once set, this value cannot be cleared." choices: - linux - windows disk_size_gb: description: - - Size in GB of the managed disk to be created. If I(create_option) is C(copy) then the value must be greater than or equal to the source's size. + - "Size in GB of the managed disk to be created." + - "If I(create_option) is C(copy) then the value must be greater than or equal to the source's size." managed_by: description: - Name of an existing virtual machine with which the disk is or will be associated, this VM should be in the same resource group. @@ -96,6 +101,16 @@ EXAMPLES = ''' resource_group: Testing disk_size_gb: 4 + - name: Create managed operating system disk from page blob + azure_rm_managed_disk: + name: mymanageddisk + location: eastus2 + resource_group: Testing + create_option: import + source_uri: https://storageaccountname.blob.core.windows.net/containername/blob-name.vhd + os_type: windows + storage_account_type: Premium_LRS + - name: Mount the managed disk to VM azure_rm_managed_disk: name: mymanageddisk @@ -157,7 +172,7 @@ def managed_disk_to_dict(managed_disk): create_option=create_data.create_option.value.lower(), source_uri=create_data.source_uri or create_data.source_resource_id, disk_size_gb=managed_disk.disk_size_gb, - os_type=managed_disk.os_type.value if managed_disk.os_type else None, + os_type=managed_disk.os_type.value.lower() if managed_disk.os_type else None, storage_account_type=managed_disk.sku.name.value if managed_disk.sku else None, managed_by=managed_disk.managed_by ) @@ -315,6 +330,7 @@ class AzureRMManagedDisk(AzureRMModuleBase): self.fail("Error getting virtual machine {0} - {1}".format(name, str(exc))) def generate_managed_disk_property(self): + # TODO: Add support for EncryptionSettings, DiskIOPSReadWrite, DiskMBpsReadWrite, Zones disk_params = {} creation_data = {} disk_params['location'] = self.location @@ -323,7 +339,6 @@ class AzureRMManagedDisk(AzureRMModuleBase): storage_account_type = self.compute_models.DiskSku(name=self.storage_account_type) disk_params['sku'] = storage_account_type disk_params['disk_size_gb'] = self.disk_size_gb - # TODO: Add support for EncryptionSettings creation_data['create_option'] = self.compute_models.DiskCreateOption.empty if self.create_option == 'import': creation_data['create_option'] = self.compute_models.DiskCreateOption.import_enum @@ -331,6 +346,14 @@ class AzureRMManagedDisk(AzureRMModuleBase): elif self.create_option == 'copy': creation_data['create_option'] = self.compute_models.DiskCreateOption.copy creation_data['source_resource_id'] = self.source_uri + if self.os_type: + typecon = { + 'linux': self.compute_models.OperatingSystemTypes.linux, + 'windows': self.compute_models.OperatingSystemTypes.windows + } + disk_params['os_type'] = typecon[self.os_type] + else: + disk_params['os_type'] = None disk_params['creation_data'] = creation_data return disk_params @@ -352,6 +375,9 @@ class AzureRMManagedDisk(AzureRMModuleBase): if new_disk.get('disk_size_gb'): if not found_disk['disk_size_gb'] == new_disk['disk_size_gb']: resp = True + if new_disk.get('os_type'): + if not found_disk['os_type'] == new_disk['os_type']: + resp = True if new_disk.get('sku'): if not found_disk['storage_account_type'] == new_disk['sku'].name: resp = True diff --git a/lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py index 7439bea908c..d3f11dcc764 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py @@ -132,7 +132,7 @@ def managed_disk_to_dict(managed_disk): create_option=create_data.create_option.value.lower(), source_uri=create_data.source_uri or create_data.source_resource_id, disk_size_gb=managed_disk.disk_size_gb, - os_type=managed_disk.os_type.value if managed_disk.os_type else None, + os_type=managed_disk.os_type.value.lower() if managed_disk.os_type else None, storage_account_type=managed_disk.sku.name.value if managed_disk.sku else None, managed_by=managed_disk.managed_by ) diff --git a/test/integration/targets/azure_rm_managed_disk/tasks/main.yml b/test/integration/targets/azure_rm_managed_disk/tasks/main.yml index 2acc0cab8ed..7f65032783f 100644 --- a/test/integration/targets/azure_rm_managed_disk/tasks/main.yml +++ b/test/integration/targets/azure_rm_managed_disk/tasks/main.yml @@ -7,9 +7,10 @@ - name: Clearing (if) previous disks were created azure_rm_managed_disk: resource_group: "{{ resource_group }}" - name: "{{item }}" + name: "{{ item }}" state: absent with_items: + - "md{{ rpfx }}os" - "md{{ rpfx }}2" - "md{{ rpfx }}1" @@ -17,6 +18,7 @@ azure_rm_managed_disk: resource_group: "{{ resource_group }}" name: "md{{ rpfx }}1" + storage_account_type: "Standard_LRS" disk_size_gb: 1 tags: testing: testing @@ -59,6 +61,45 @@ that: - output.changed - output.state.id is defined + - output.state.os_type == None + + - name: Change the operating system type of the managed disk to linux + azure_rm_managed_disk: + resource_group: "{{ resource_group }}" + name: "md{{ rpfx }}1" + disk_size_gb: 1 + os_type: linux + storage_account_type: "Standard_LRS" + tags: + testing: testing + delete: never + register: output + + - assert: + that: + - output.changed + - output.state.os_type == 'linux' + + - name: Create new managed disk with an os_type specified at creation + azure_rm_managed_disk: + resource_group: "{{ resource_group }}" + name: "md{{ rpfx }}os" + storage_account_type: "Standard_LRS" + disk_size_gb: 1 + os_type: windows + register: win + + - name: Assert status of os_type specified at creation + assert: + that: + - win.changed + - win.state.os_type == 'windows' + + - name: Clean up managed disk created with os_type + azure_rm_managed_disk: + resource_group: "{{ resource_group }}" + name: "md{{ rpfx }}os" + state: absent - name: Copy disk to a new managed disk azure_rm_managed_disk: @@ -181,6 +222,7 @@ - "azure_managed_disk | length == 1" - azure_managed_disk[0].storage_account_type == "Premium_LRS" - azure_managed_disk[0].disk_size_gb == 2 + - "azure_managed_disk[0].os_type == 'linux'" - name: Gather facts azure_rm_managed_disk_facts: