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

azure_managed_disk enhancement (#50317)

This commit is contained in:
Yuwei Zhou 2019-01-10 10:41:43 +08:00 committed by Yunge Zhu
parent 6179f2f51c
commit c405fe3098
3 changed files with 74 additions and 41 deletions

View file

@ -51,17 +51,17 @@ options:
- Premium_LRS - Premium_LRS
create_option: create_option:
description: description:
- "Allowed values: empty, import, copy. C(import) from a VHD file in I(source_uri) and C(copy) from previous managed disk I(source_resource_uri)." - "Allowed values: empty, import, copy.
- C(import) from a VHD file in I(source_uri) and C(copy) from previous managed disk I(source_uri)."
choices: choices:
- empty - empty
- import - import
- copy - copy
source_uri: source_uri:
description: description:
- URI to a valid VHD file to be used when I(create_option) is C(import). - URI to a valid VHD file to be used or the resource ID of the managed disk to copy.
source_resource_uri: aliases:
description: - source_resource_uri
- The resource ID of the managed disk to copy when I(create_option) is C(copy).
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."
@ -74,7 +74,8 @@ options:
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.
- To detach a disk from a vm, keep undefined. - To detach a disk from a vm, explicitly set to ''.
- If this option is unset, the value will not be changed.
version_added: 2.5 version_added: 2.5
tags: tags:
description: description:
@ -154,8 +155,7 @@ def managed_disk_to_dict(managed_disk):
location=managed_disk.location, location=managed_disk.location,
tags=managed_disk.tags, tags=managed_disk.tags,
create_option=create_data.create_option.value.lower(), create_option=create_data.create_option.value.lower(),
source_uri=create_data.source_uri, source_uri=create_data.source_uri or create_data.source_resource_id,
source_resource_uri=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 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,
@ -193,10 +193,8 @@ class AzureRMManagedDisk(AzureRMModuleBase):
choices=['empty', 'import', 'copy'] choices=['empty', 'import', 'copy']
), ),
source_uri=dict( source_uri=dict(
type='str' type='str',
), aliases=['source_resource_uri']
source_resource_uri=dict(
type='str'
), ),
os_type=dict( os_type=dict(
type='str', type='str',
@ -211,7 +209,7 @@ class AzureRMManagedDisk(AzureRMModuleBase):
) )
required_if = [ required_if = [
('create_option', 'import', ['source_uri']), ('create_option', 'import', ['source_uri']),
('create_option', 'copy', ['source_resource_uri']), ('create_option', 'copy', ['source_uri']),
('create_option', 'empty', ['disk_size_gb']) ('create_option', 'empty', ['disk_size_gb'])
] ]
self.results = dict( self.results = dict(
@ -224,7 +222,6 @@ class AzureRMManagedDisk(AzureRMModuleBase):
self.storage_account_type = None self.storage_account_type = None
self.create_option = None self.create_option = None
self.source_uri = None self.source_uri = None
self.source_resource_uri = None
self.os_type = None self.os_type = None
self.disk_size_gb = None self.disk_size_gb = None
self.tags = None self.tags = None
@ -261,15 +258,17 @@ class AzureRMManagedDisk(AzureRMModuleBase):
result = True result = True
# unmount from the old virtual machine and mount to the new virtual machine # unmount from the old virtual machine and mount to the new virtual machine
vm_name = parse_resource_id(disk_instance.get('managed_by', '')).get('name') if disk_instance else None if self.managed_by or self.managed_by == '':
if self.managed_by != vm_name: vm_name = parse_resource_id(disk_instance.get('managed_by', '')).get('name') if disk_instance else None
changed = True vm_name = vm_name or ''
if not self.check_mode: if self.managed_by != vm_name:
if vm_name: changed = True
self.detach(vm_name, result) if not self.check_mode:
if self.managed_by: if vm_name:
self.attach(self.managed_by, result) self.detach(vm_name, result)
result = self.get_managed_disk() if self.managed_by:
self.attach(self.managed_by, result)
result = self.get_managed_disk()
if self.state == 'absent' and disk_instance: if self.state == 'absent' and disk_instance:
changed = True changed = True
@ -331,7 +330,7 @@ class AzureRMManagedDisk(AzureRMModuleBase):
creation_data['source_uri'] = self.source_uri creation_data['source_uri'] = self.source_uri
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_resource_uri creation_data['source_resource_id'] = self.source_uri
disk_params['creation_data'] = creation_data disk_params['creation_data'] = creation_data
return disk_params return disk_params

View file

@ -73,6 +73,43 @@ azure_managed_disk:
description: List of managed disk dicts. description: List of managed disk dicts.
returned: always returned: always
type: list type: list
contains:
id:
description:
- Resource id.
name:
description:
- Name of the managed disk.
location:
description:
- Valid Azure location.
storage_account_type:
description:
- Type of storage for the managed disk
sample: Standard_LRS
type: str
create_option:
description:
- Create option of the disk
sample: copy
type: str
source_uri:
description:
- URI to a valid VHD file to be used or the resource ID of the managed disk to copy.
os_type:
description:
- "Type of Operating System: C(linux) or C(windows)."
disk_size_gb:
description:
- Size in GB of the managed disk to be created.
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.
tags:
description:
- Tags to assign to the managed disk.
sample: { "tag": "value" }
type: dict
''' '''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase from ansible.module_utils.azure_rm_common import AzureRMModuleBase
@ -93,8 +130,7 @@ def managed_disk_to_dict(managed_disk):
location=managed_disk.location, location=managed_disk.location,
tags=managed_disk.tags, tags=managed_disk.tags,
create_option=create_data.create_option.value.lower(), create_option=create_data.create_option.value.lower(),
source_uri=create_data.source_uri, source_uri=create_data.source_uri or create_data.source_resource_id,
source_resource_uri=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 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,

View file

@ -65,7 +65,7 @@
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}2" name: "md{{ rpfx }}2"
create_option: "copy" create_option: "copy"
source_resource_uri: "{{ output.state.id }}" source_uri: "{{ output.state.id }}"
disk_size_gb: 1 disk_size_gb: 1
register: copy register: copy
@ -175,11 +175,12 @@
azure_rm_managed_disk_facts: azure_rm_managed_disk_facts:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1" name: "md{{ rpfx }}1"
register: fact
- assert: - assert:
that: that:
- "azure_managed_disk | length == 1" - "azure_managed_disk | length == 1"
- azure_managed_disk[0].storage_account_type == "Premium_LRS"
- azure_managed_disk[0].disk_size_gb == 2
- name: Gather facts - name: Gather facts
azure_rm_managed_disk_facts: azure_rm_managed_disk_facts:
@ -192,7 +193,7 @@
- set_fact: - set_fact:
parameter: "{{parameter |combine({item.key: item.value})}}" parameter: "{{parameter |combine({item.key: item.value})}}"
when: "{{item.key not in ['id', 'changed'] and item.value != None}}" when: "{{item.key not in ['id', 'changed'] and item.value != None}}"
with_dict: "{{ fact.ansible_facts.azure_managed_disk[0] }}" with_dict: "{{ azure_managed_disk[0] }}"
- name: Create disk with facts return value - name: Create disk with facts return value
azure_rm_managed_disk: azure_rm_managed_disk:
@ -294,7 +295,6 @@
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1" name: "md{{ rpfx }}1"
disk_size_gb: 2 disk_size_gb: 2
managed_by: "tr{{ rpfx }}"
tags: tags:
testing: testing testing: testing
delete: never delete: never
@ -310,6 +310,7 @@
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1" name: "md{{ rpfx }}1"
disk_size_gb: 2 disk_size_gb: 2
managed_by: ''
tags: tags:
testing: testing testing: testing
delete: never delete: never
@ -325,6 +326,7 @@
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1" name: "md{{ rpfx }}1"
disk_size_gb: 2 disk_size_gb: 2
managed_by: ''
tags: tags:
testing: testing testing: testing
delete: never delete: never
@ -339,6 +341,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"
managed_by: ''
disk_size_gb: 2 disk_size_gb: 2
tags: tags:
testing: testing testing: testing
@ -406,20 +409,15 @@
- output.changed - output.changed
- output.state - output.state
- name: Delete managed disk - name: Delete all managed disk
azure_rm_managed_disk: azure_rm_managed_disk:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}2" name: "md{{ rpfx }}{{ item }}"
managed_by: ''
state: absent state: absent
check_mode: no with_items:
- 1
- name: Delete copied managed disk - 2
azure_rm_managed_disk:
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1"
disk_size_gb: 2
state: absent
check_mode: no
- name: Delete virtual machine - name: Delete virtual machine
azure_rm_virtualmachine: azure_rm_virtualmachine: