1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

azure_rm_managed_disk: properly handle os_type on creation and update (#49385)

This commit is contained in:
James E. King III 2019-01-11 09:09:26 -05:00 committed by Zim Kalinowski
parent 22c1c5ffcb
commit 1ae365c112
3 changed files with 74 additions and 6 deletions

View file

@ -64,13 +64,18 @@ options:
- source_resource_uri - source_resource_uri
os_type: os_type:
description: 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: choices:
- linux - linux
- windows - windows
disk_size_gb: disk_size_gb:
description: 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: managed_by:
description: 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. - 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 resource_group: Testing
disk_size_gb: 4 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 - name: Mount the managed disk to VM
azure_rm_managed_disk: azure_rm_managed_disk:
name: mymanageddisk name: mymanageddisk
@ -157,7 +172,7 @@ def managed_disk_to_dict(managed_disk):
create_option=create_data.create_option.value.lower(), create_option=create_data.create_option.value.lower(),
source_uri=create_data.source_uri or create_data.source_resource_id, source_uri=create_data.source_uri or create_data.source_resource_id,
disk_size_gb=managed_disk.disk_size_gb, 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, storage_account_type=managed_disk.sku.name.value if managed_disk.sku else None,
managed_by=managed_disk.managed_by 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))) self.fail("Error getting virtual machine {0} - {1}".format(name, str(exc)))
def generate_managed_disk_property(self): def generate_managed_disk_property(self):
# TODO: Add support for EncryptionSettings, DiskIOPSReadWrite, DiskMBpsReadWrite, Zones
disk_params = {} disk_params = {}
creation_data = {} creation_data = {}
disk_params['location'] = self.location disk_params['location'] = self.location
@ -323,7 +339,6 @@ class AzureRMManagedDisk(AzureRMModuleBase):
storage_account_type = self.compute_models.DiskSku(name=self.storage_account_type) storage_account_type = self.compute_models.DiskSku(name=self.storage_account_type)
disk_params['sku'] = storage_account_type disk_params['sku'] = storage_account_type
disk_params['disk_size_gb'] = self.disk_size_gb disk_params['disk_size_gb'] = self.disk_size_gb
# TODO: Add support for EncryptionSettings
creation_data['create_option'] = self.compute_models.DiskCreateOption.empty creation_data['create_option'] = self.compute_models.DiskCreateOption.empty
if self.create_option == 'import': if self.create_option == 'import':
creation_data['create_option'] = self.compute_models.DiskCreateOption.import_enum creation_data['create_option'] = self.compute_models.DiskCreateOption.import_enum
@ -331,6 +346,14 @@ class AzureRMManagedDisk(AzureRMModuleBase):
elif self.create_option == 'copy': elif self.create_option == 'copy':
creation_data['create_option'] = self.compute_models.DiskCreateOption.copy creation_data['create_option'] = self.compute_models.DiskCreateOption.copy
creation_data['source_resource_id'] = self.source_uri 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 disk_params['creation_data'] = creation_data
return disk_params return disk_params
@ -352,6 +375,9 @@ class AzureRMManagedDisk(AzureRMModuleBase):
if new_disk.get('disk_size_gb'): if new_disk.get('disk_size_gb'):
if not found_disk['disk_size_gb'] == new_disk['disk_size_gb']: if not found_disk['disk_size_gb'] == new_disk['disk_size_gb']:
resp = True 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 new_disk.get('sku'):
if not found_disk['storage_account_type'] == new_disk['sku'].name: if not found_disk['storage_account_type'] == new_disk['sku'].name:
resp = True resp = True

View file

@ -132,7 +132,7 @@ def managed_disk_to_dict(managed_disk):
create_option=create_data.create_option.value.lower(), create_option=create_data.create_option.value.lower(),
source_uri=create_data.source_uri or create_data.source_resource_id, source_uri=create_data.source_uri or create_data.source_resource_id,
disk_size_gb=managed_disk.disk_size_gb, 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, storage_account_type=managed_disk.sku.name.value if managed_disk.sku else None,
managed_by=managed_disk.managed_by managed_by=managed_disk.managed_by
) )

View file

@ -7,9 +7,10 @@
- name: Clearing (if) previous disks were created - name: Clearing (if) previous disks were created
azure_rm_managed_disk: azure_rm_managed_disk:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "{{item }}" name: "{{ item }}"
state: absent state: absent
with_items: with_items:
- "md{{ rpfx }}os"
- "md{{ rpfx }}2" - "md{{ rpfx }}2"
- "md{{ rpfx }}1" - "md{{ rpfx }}1"
@ -17,6 +18,7 @@
azure_rm_managed_disk: azure_rm_managed_disk:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1" name: "md{{ rpfx }}1"
storage_account_type: "Standard_LRS"
disk_size_gb: 1 disk_size_gb: 1
tags: tags:
testing: testing testing: testing
@ -59,6 +61,45 @@
that: that:
- output.changed - output.changed
- output.state.id is defined - 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 - name: Copy disk to a new managed disk
azure_rm_managed_disk: azure_rm_managed_disk:
@ -181,6 +222,7 @@
- "azure_managed_disk | length == 1" - "azure_managed_disk | length == 1"
- azure_managed_disk[0].storage_account_type == "Premium_LRS" - azure_managed_disk[0].storage_account_type == "Premium_LRS"
- azure_managed_disk[0].disk_size_gb == 2 - azure_managed_disk[0].disk_size_gb == 2
- "azure_managed_disk[0].os_type == 'linux'"
- name: Gather facts - name: Gather facts
azure_rm_managed_disk_facts: azure_rm_managed_disk_facts: