Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_manageddisk_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, Paul Aiton <@paultaiton>
5 # Copyright: (c) 2016, Bruno Medina Bolanos Cacho <[email protected]>
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12 DOCUMENTATION = r'''
13 ---
14 module: azure_rm_manageddisk_info
15
16 version_added: "0.1.2"
17
18 short_description: Get managed disk facts
19
20 description:
21 - Get facts for a specific managed disk or all managed disks.
22
23 notes:
24 - This module was called M(azure.azcollection.azure_rm_managed_disk_facts) before Ansible 2.8. The usage did not change.
25
26 options:
27 name:
28 description:
29 - Limit results to a specific managed disk.
30 type: str
31 resource_group:
32 description:
33 - Limit results to a specific resource group.
34 - Required if I(name) is set
35 type: str
36 tags:
37 description:
38 - Limit results by providing a list of tags.
39 - Format tags as 'key' or 'key:value'.
40 type: list
41 elements: str
42 managed_by:
43 description:
44 - Limit results to disks managed by the given VM fqid.
45 type: str
46
47 extends_documentation_fragment:
48 - azure.azcollection.azure
49
50 author:
51 - Bruno Medina (@brusMX)
52 - Paul Aiton (@paultaiton)
53 '''
54
55 EXAMPLES = r'''
56 - name: Get facts for one managed disk
57 azure_rm_manageddisk_info:
58 name: Testing
59 resource_group: myResourceGroup
60
61 - name: Get facts for all managed disks
62 azure_rm_manageddisk_info:
63
64 - name: Get facts for all managed disks managed by a specific vm
65 azure_rm_manageddisk_info:
66 managed_by: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/rgName/Microsoft.Compute/virtualMachines/vmName'
67
68 - name: Get facts by tags
69 azure_rm_manageddisk_info:
70 tags:
71 - testing
72 '''
73
74 RETURN = r'''
75 azure_managed_disk:
76 description:
77 - List of managed disk dicts.
78 returned: always
79 type: list
80 contains:
81 id:
82 description:
83 - Resource id.
84 type: str
85 name:
86 description:
87 - Name of the managed disk.
88 type: str
89 location:
90 description:
91 - Valid Azure location.
92 type: str
93 storage_account_type:
94 description:
95 - Type of storage for the managed disk.
96 - See U(https://docs.microsoft.com/en-us/azure/virtual-machines/windows/disks-types) for more information about this type.
97 type: str
98 sample: Standard_LRS
99 create_option:
100 description:
101 - Create option of the disk.
102 type: str
103 sample: copy
104 source_uri:
105 description:
106 - URI to a valid VHD file to be used or the resource ID of the managed disk to copy.
107 type: str
108 os_type:
109 description:
110 - Type of Operating System.
111 choices:
112 - linux
113 - windows
114 type: str
115 disk_size_gb:
116 description:
117 - Size in GB of the managed disk to be created.
118 type: str
119 managed_by:
120 description:
121 - Name of an existing virtual machine with which the disk is or will be associated, this VM should be in the same resource group.
122 type: str
123 sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/testVM"
124 max_shares:
125 description:
126 - The maximum number of VMs that can attach to the disk at the same time.
127 - Value greater than one indicates a disk that can be mounted on multiple VMs at the same time.
128 type: int
129 sample: 3
130 managed_by_extended:
131 description:
132 - List ID of an existing virtual machine with which the disk is or will be associated.
133 type: list
134 sample: ["/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/testVM"]
135 tags:
136 description:
137 - Tags to assign to the managed disk.
138 type: dict
139 sample: { "tag": "value" }
140 tier:
141 description:
142 - Performance tier assigned to the managed disk.
143 - See U(https://learn.microsoft.com/en-us/azure/virtual-machines/disks-change-performance) for more information about disk performance tiers.
144 type: str
145 sample: "P30"
146 time_created:
147 description:
148 - The time the disk was created.
149 type: str
150 sample: "2018-01-01T11:08:15.338648900:00"
151 disk_iops_read_write:
152 description:
153 - The number of IOPS allowed for this disk.
154 - Only settable for I(storage_account_type=UltraSSD_LRS) disks.
155 - One operation can transfer between 4k and 256k bytes.
156 type: int
157 returned: always
158 sample: 200
159 disk_m_bps_read_write:
160 description:
161 - The bandwidth allowed for this disk.
162 - Only settable for I(storage_account_type=UltraSSD_LRS) disks.
163 - One operation can transfer between 4k and 256k bytes.
164 type: int
165 returned: always
166 sample: 30
167 disk_iops_read_only:
168 description:
169 - The total throughput (MBps) that will be allowed across all VMs mounting the shared disk as ReadOnly.
170 - One operation can transfer between 4k and 256k bytes.
171 type: int
172 returned: always
173 sample: 200
174 disk_m_bps_read_only:
175 description:
176 - The total throughput (MBps) that will be allowed across all VMs mounting the shared disk as ReadOnly.
177 - MBps means millions of bytes per second - MB here uses the ISO notation, of powers of 10.
178 type: int
179 returned: always
180 sample: 30
181 network_access_policy:
182 description:
183 - Policy for accessing the disk via network.
184 type: str
185 returned: always
186 sample: AllowAll
187 public_network_access:
188 description:
189 - Policy for controlling export on the disk.
190 type: str
191 returned: always
192 sample: Enabled
193 disk_access_id:
194 description:
195 - ARM ID of the DiskAccess resource for using private endpoints on disks.
196 type: str
197 returned: always
198 sample: '/subscriptions/*********/resourceGroups/myRG/providers/Microsoft.Compute/diskAccesses/diskacc'
199 performance_plus:
200 description:
201 - The flag of the performance target of the disk deployed.
202 type: bool
203 returned: always
204 sample: False
205 upload_size_bytes:
206 description:
207 - This is the size of the contents of the upload including the VHD footer.
208 type: int
209 returned: always
210 sample: None
211 gallery_image_reference:
212 description:
213 - The Gallery Image info.
214 type: dict
215 returned: always
216 sample: None
217 image_reference:
218 description:
219 - Disk source information for PIR or user images or Gallery Image.
220 type: dict
221 returned: always
222 sample: None
223 logical_sector_size:
224 description:
225 - Logical sector size in bytes for Ultra disks.
226 type: int
227 returned: always
228 sample: None
229 last_ownership_update_time:
230 description:
231 - The UTC time when the ownership state of the disk was last changed.
232 - The time the disk was last attached or detached from a VM or the time when the VM to which the disk was attached was deallocated or started.
233 returned: always
234 type: str
235 sample: "2025-06-27T01:55:10.239311+00:00"
236 source_resource_id:
237 description:
238 - This is the ARM id of the source snapshot or disk.
239 type: str
240 returned: always
241 sample: None
242 security_profile:
243 description:
244 - The security related information for the resource.
245 type: complex
246 contains:
247 security_type:
248 description:
249 - Specifies the SecurityType of the VM.
250 type: str
251 returned: when-used
252 sample: TrustedLaunch
253 secure_vm_disk_encryption_set_id:
254 description:
255 - ResourceId of the disk encryption set associated to Confidential VM supported disk encrypted with customer managed key.
256 type: str
257 returned: when-used
258 sample: None
259
260 '''
261
262 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
263
264 try:
265 from azure.core.exceptions import ResourceNotFoundError
266 except Exception:
267 # handled in azure_rm_common
268 pass
269
270
271 class AzureRMManagedDiskInfo(AzureRMModuleBase):
272 """Utility class to get managed disk facts"""
273
274 def __init__(self):
275 self.module_arg_spec = dict(
276 resource_group=dict(type='str'),
277 name=dict(type='str'),
278 tags=dict(type='list', elements='str'),
279 managed_by=dict(type='str')
280 )
281
282 self.results = dict(
283 ansible_info=dict(
284 azure_managed_disk=[]
285 )
286 )
287
288 self.resource_group = None
289 self.name = None
290 self.tags = None
291 self.managed_by = None
292
293 super(AzureRMManagedDiskInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
294 supports_check_mode=True,
295 facts_module=True,
296 supports_tags=False)
297
298 def exec_module(self, **kwargs):
299 for key in self.module_arg_spec:
300 setattr(self, key, kwargs[key])
301
302 if self.name and not self.resource_group:
303 self.fail('Parameter Error: name requires that resource_group also be set.')
304
305 if self.name:
306 self.results['ansible_info']['azure_managed_disk'] = self.get_disk()
307 elif self.resource_group:
308 self.results['ansible_info']['azure_managed_disk'] = self.list_disks_by_resource_group()
309 else:
310 self.results['ansible_info']['azure_managed_disk'] = self.list_disks()
311
312 return self.results
313
314 def get_disk(self):
315 """Get a single managed disk"""
316 results = []
317
318 try:
319 results = [self.disk_client.disks.get(self.resource_group,
320 self.name)]
321 if self.managed_by:
322 results = [disk for disk in results if disk.managed_by == self.managed_by]
323 if self.tags:
324 results = [disk for disk in results if self.has_tags(disk.tags, self.tags)]
325 results = [self.managed_disk_to_dict(disk) for disk in results]
326 except ResourceNotFoundError:
327 self.log('Could not find disk {0} in resource group {1}'.format(self.name, self.resource_group))
328
329 return results
330
331 def list_disks(self):
332 """Get all managed disks"""
333 results = []
334
335 try:
336 results = self.disk_client.disks.list()
337 if self.managed_by:
338 results = [disk for disk in results if disk.managed_by == self.managed_by]
339 if self.tags:
340 results = [disk for disk in results if self.has_tags(disk.tags, self.tags)]
341 results = [self.managed_disk_to_dict(disk) for disk in results]
342 except ResourceNotFoundError as exc:
343 self.fail('Failed to list all items - {0}'.format(str(exc)))
344
345 return results
346
347 def list_disks_by_resource_group(self):
348 """Get managed disks in a resource group"""
349 results = []
350
351 try:
352 results = self.disk_client.disks.list_by_resource_group(resource_group_name=self.resource_group)
353 if self.managed_by:
354 results = [disk for disk in results if disk.managed_by == self.managed_by]
355 if self.tags:
356 results = [disk for disk in results if self.has_tags(disk.tags, self.tags)]
357 results = [self.managed_disk_to_dict(disk) for disk in results]
358 except ResourceNotFoundError as exc:
359 self.fail('Failed to list items by resource group - {0}'.format(str(exc)))
360
361 return results
362
363 def managed_disk_to_dict(self, managed_disk):
364 create_data = managed_disk.creation_data
365 return dict(
366 id=managed_disk.id,
367 name=managed_disk.name,
368 location=managed_disk.location,
369 tags=managed_disk.tags,
370 create_option=create_data.create_option.lower(),
371 source_uri=create_data.source_uri or create_data.source_resource_id,
372 disk_size_gb=managed_disk.disk_size_gb,
373 os_type=managed_disk.os_type.lower() if managed_disk.os_type else None,
374 storage_account_type=managed_disk.sku.name if managed_disk.sku else None,
375 managed_by=managed_disk.managed_by,
376 max_shares=managed_disk.max_shares,
377 managed_by_extended=managed_disk.managed_by_extended,
378 zone=managed_disk.zones[0] if managed_disk.zones and len(managed_disk.zones) > 0 else '',
379 time_created=managed_disk.time_created.isoformat() if managed_disk.time_created else None,
380 disk_iops_read_write=managed_disk.disk_iops_read_write,
381 disk_m_bps_read_write=managed_disk.disk_m_bps_read_write,
382 disk_iops_read_only=managed_disk.disk_iops_read_only,
383 disk_m_bps_read_only=managed_disk.disk_m_bps_read_only,
384 tier=managed_disk.tier,
385 network_access_policy=managed_disk.network_access_policy,
386 public_network_access=managed_disk.public_network_access,
387 disk_access_id=managed_disk.disk_access_id,
388 source_resource_id=create_data.source_resource_id,
389 storage_account_id=create_data.storage_account_id,
390 upload_size_bytes=create_data.upload_size_bytes,
391 logical_sector_size=create_data.logical_sector_size,
392 performance_plus=create_data.performance_plus,
393 last_ownership_update_time=managed_disk.last_ownership_update_time,
394 gallery_image_reference=dict(
395 id=create_data.gallery_image_reference.id,
396 shared_gallery_image_id=create_data.gallery_image_reference.shared_gallery_image_id,
397 community_gallery_image_id=create_data.gallery_image_reference.community_gallery_image_id
398 ) if create_data.gallery_image_reference is not None else None,
399 image_reference=dict(
400 id=create_data.image_reference.id,
401 shared_gallery_image_id=create_data.image_reference.shared_gallery_image_id,
402 community_gallery_image_id=create_data.image_reference.community_gallery_image_id
403 ) if create_data.image_reference is not None else None,
404 security_profile=dict(
405 security_type=managed_disk.security_profile.security_type,
406 secure_vm_disk_encryption_set_id=managed_disk.security_profile.secure_vm_disk_encryption_set_id
407 ) if managed_disk.security_profile is not None else None
408 )
409
410
411 def main():
412 """Main module execution code path"""
413 AzureRMManagedDiskInfo()
414
415
416 if __name__ == '__main__':
417 main()