diff --git a/lib/ansible/module_utils/azure_rm_common_ext.py b/lib/ansible/module_utils/azure_rm_common_ext.py index 4935b2c2c1a..bb7af27f03f 100644 --- a/lib/ansible/module_utils/azure_rm_common_ext.py +++ b/lib/ansible/module_utils/azure_rm_common_ext.py @@ -19,6 +19,8 @@ class AzureRMModuleBaseExt(AzureRMModuleBase): # first check if option was passed param = body.get(name) if not param: + if spec[name].get('purgeIfNone', False): + body.pop(name, None) continue # check if pattern needs to be used pattern = spec[name].get('pattern', None) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_snapshot.py b/lib/ansible/modules/cloud/azure/azure_rm_snapshot.py index f79ea6d5b90..759fd2d69b8 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_snapshot.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_snapshot.py @@ -17,7 +17,7 @@ DOCUMENTATION = ''' --- module: azure_rm_snapshot version_added: '2.9' -short_description: Manage Azure Snapshot instance +short_description: Manage Azure Snapshot instance. description: - Create, update and delete instance of Azure Snapshot. options: @@ -68,13 +68,17 @@ options: description: - This enumerates the possible sources of a disk's creation. type: str - default: Import choices: - Import + - Copy source_uri: description: - If I(createOption=Import), this is the URI of a blob to be imported into a managed disk. type: str + source_id: + description: + - If I(createOption=Copy), this is the resource ID of a managed disk to be copied from. + type: str state: description: - Assert the state of the Snapshot. @@ -82,8 +86,8 @@ options: default: present type: str choices: - - absent - - present + - absent + - present extends_documentation_fragment: - azure - azure_tags @@ -101,6 +105,15 @@ EXAMPLES = ''' creation_data: create_option: Import source_uri: 'https://mystorageaccount.blob.core.windows.net/osimages/osimage.vhd' + +- name: Create a snapshot by copying an existing managed disk. + azure_rm_snapshot: + resource_group: myResourceGroup + name: mySnapshot + location: eastus + creation_data: + create_option: Copy + source_id: '/subscriptions/sub123/resourceGroups/group123/providers/Microsoft.Compute/disks/disk123' ''' RETURN = ''' @@ -114,10 +127,8 @@ id: import time import json -import re from ansible.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt from ansible.module_utils.azure_rm_common_rest import GenericRestClient -from copy import deepcopy try: from msrestazure.azure_exceptions import CloudError except ImportError: @@ -177,12 +188,17 @@ class AzureRMSnapshots(AzureRMModuleBaseExt): create_option=dict( type='str', disposition='createOption', - choices=['Import'], - default='Import' + choices=['Import', 'Copy'], ), source_uri=dict( type='str', - disposition='sourceUri' + disposition='sourceUri', + purgeIfNone=True + ), + source_id=dict( + type='str', + disposition='sourceResourceId', + purgeIfNone=True ) ) ), @@ -355,6 +371,7 @@ class AzureRMSnapshots(AzureRMModuleBaseExt): expected_status_codes=self.status_code, polling_timeout=600, polling_interval=30) + response = json.loads(response.text) found = True self.log("Response : {0}".format(response)) # self.log("Snapshot instance : {0} found".format(response.name)) diff --git a/test/integration/targets/azure_rm_gallery/tasks/main.yml b/test/integration/targets/azure_rm_gallery/tasks/main.yml index 85f1695a682..4f18cb88cbe 100644 --- a/test/integration/targets/azure_rm_gallery/tasks/main.yml +++ b/test/integration/targets/azure_rm_gallery/tasks/main.yml @@ -53,7 +53,7 @@ - name: Create a snapshot by importing an unmanaged blob from the same subscription. azure_rm_snapshot: resource_group: "{{ resource_group }}" - name: mySnapshot + name: "mySnapshot-{{ rpfx }}" location: eastus creation_data: create_option: Import