| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: azure_rm_image_info |
| 12 |
|
| 13 |
version_added: "0.0.1" |
| 14 |
|
| 15 |
short_description: Get facts about azure custom images |
| 16 |
|
| 17 |
description: |
| 18 |
- List azure custom images. The images can be listed where scope of listing can be based on subscription, resource group, name or tags. |
| 19 |
|
| 20 |
options: |
| 21 |
resource_group: |
| 22 |
description: |
| 23 |
- Name of resource group. |
| 24 |
type: str |
| 25 |
name: |
| 26 |
description: |
| 27 |
- Name of the image to filter from existing images. |
| 28 |
type: str |
| 29 |
tags: |
| 30 |
description: |
| 31 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
|
| 35 |
extends_documentation_fragment: |
| 36 |
- azure.azcollection.azure |
| 37 |
|
| 38 |
author: |
| 39 |
- Madhura Naniwadekar (@Madhura-CSI) |
| 40 |
''' |
| 41 |
|
| 42 |
|
| 43 |
EXAMPLES = ''' |
| 44 |
- name: List images with name |
| 45 |
azure_rm_image_info: |
| 46 |
name: test-image |
| 47 |
resource_group: myResourceGroup |
| 48 |
|
| 49 |
- name: List images by resource group |
| 50 |
azure_rm_image_info: |
| 51 |
resource_group: myResourceGroup |
| 52 |
tags: |
| 53 |
- testing |
| 54 |
- foo:bar |
| 55 |
|
| 56 |
- name: List all available images under current subscription |
| 57 |
azure_rm_image_info: |
| 58 |
''' |
| 59 |
|
| 60 |
|
| 61 |
RETURN = ''' |
| 62 |
images: |
| 63 |
description: |
| 64 |
- List of image dicts. |
| 65 |
returned: always |
| 66 |
type: complex |
| 67 |
contains: |
| 68 |
id: |
| 69 |
description: |
| 70 |
- Id of the image. |
| 71 |
returned: always |
| 72 |
type: str |
| 73 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/xx |
| 74 |
name: |
| 75 |
description: |
| 76 |
- Name of the image. |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
sample: foo |
| 80 |
resource_group: |
| 81 |
description: |
| 82 |
- Resource group of the image. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: myResourceGroup |
| 86 |
location: |
| 87 |
description: |
| 88 |
- Location of the image. |
| 89 |
returned: always |
| 90 |
type: str |
| 91 |
os_disk: |
| 92 |
description: |
| 93 |
- Id of os disk for image. |
| 94 |
type: str |
| 95 |
returned: always |
| 96 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/disks/xx |
| 97 |
os_disk_caching: |
| 98 |
description: |
| 99 |
- Specifies caching requirements for the image. |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
os_state: |
| 103 |
description: |
| 104 |
- Specifies image operating system state. Possible values are C(Generalized) or C(Specialized). |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: Generalized |
| 108 |
os_storage_account_type: |
| 109 |
description: |
| 110 |
- Specifies the storage account type for the managed disk. |
| 111 |
type: str |
| 112 |
returned: always |
| 113 |
sample: Standard_LRS |
| 114 |
os_disk_encryption_set: |
| 115 |
description: |
| 116 |
- Specifies the disk encryption set resource id for the managed disk. |
| 117 |
type: str |
| 118 |
returned: always |
| 119 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/diskEncryptionSets/xx |
| 120 |
os_type: |
| 121 |
description: |
| 122 |
- Type of OS for image. |
| 123 |
returned: always |
| 124 |
type: str |
| 125 |
sample: Linux |
| 126 |
provisioning_state: |
| 127 |
description: |
| 128 |
- State of image. |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: Succeeded |
| 132 |
source: |
| 133 |
description: |
| 134 |
- Resource id of source VM from which the image is created. |
| 135 |
type: str |
| 136 |
returned: always |
| 137 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/xx |
| 138 |
tags: |
| 139 |
description: |
| 140 |
- Dictionary of tags associated with the image. |
| 141 |
type: dict |
| 142 |
returned: always |
| 143 |
sample: {"key1":"value1"} |
| 144 |
hyper_v_generation: |
| 145 |
description: |
| 146 |
- The hypervisor generation of the Virtual Machine created from the image. |
| 147 |
type: str |
| 148 |
returned: always |
| 149 |
sample: 'V1' |
| 150 |
data_disks: |
| 151 |
description: |
| 152 |
- List of data disks associated with the image. |
| 153 |
type: complex |
| 154 |
returned: always |
| 155 |
contains: |
| 156 |
caching: |
| 157 |
description: |
| 158 |
- Type of caching of data disk. |
| 159 |
type: str |
| 160 |
returned: always |
| 161 |
sample: read_only |
| 162 |
disk_size_gb: |
| 163 |
description: |
| 164 |
- Specifies the size of empty data disks in gigabytes. |
| 165 |
returned: always |
| 166 |
type: int |
| 167 |
sample: 50 |
| 168 |
lun: |
| 169 |
description: |
| 170 |
- Specifies the logical unit number of the data disk. |
| 171 |
returned: always |
| 172 |
type: int |
| 173 |
sample: 0 |
| 174 |
storage_account_type: |
| 175 |
description: |
| 176 |
- Specifies the storage account type for the managed disk data disk. |
| 177 |
type: str |
| 178 |
returned: always |
| 179 |
sample: Standard_LRS |
| 180 |
managed_disk_id: |
| 181 |
description: |
| 182 |
- Id of managed disk. |
| 183 |
type: str |
| 184 |
returned: always |
| 185 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/disks/xx |
| 186 |
blob_uri: |
| 187 |
description: |
| 188 |
- The virtual hard disk. |
| 189 |
type: str |
| 190 |
returned: always |
| 191 |
sample: null |
| 192 |
''' |
| 193 |
|
| 194 |
|
| 195 |
try: |
| 196 |
from azure.core.exceptions import ResourceNotFoundError |
| 197 |
except Exception: |
| 198 |
|
| 199 |
pass |
| 200 |
|
| 201 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 202 |
|
| 203 |
|
| 204 |
AZURE_ENUM_MODULES = ['azure.mgmt.compute.models'] |
| 205 |
|
| 206 |
|
| 207 |
class AzureRMImageInfo(AzureRMModuleBase): |
| 208 |
|
| 209 |
def __init__(self, **kwargs): |
| 210 |
|
| 211 |
self.module_arg_spec = dict( |
| 212 |
resource_group=dict(type='str'), |
| 213 |
name=dict(type='str'), |
| 214 |
tags=dict(type='list', elements='str') |
| 215 |
) |
| 216 |
|
| 217 |
self.results = dict( |
| 218 |
changed=False |
| 219 |
) |
| 220 |
|
| 221 |
self.resource_group = None |
| 222 |
self.name = None |
| 223 |
self.format = None |
| 224 |
self.tags = None |
| 225 |
|
| 226 |
super(AzureRMImageInfo, self).__init__( |
| 227 |
derived_arg_spec=self.module_arg_spec, |
| 228 |
supports_check_mode=True, |
| 229 |
supports_tags=False, |
| 230 |
facts_module=True |
| 231 |
) |
| 232 |
|
| 233 |
def exec_module(self, **kwargs): |
| 234 |
|
| 235 |
for key in self.module_arg_spec: |
| 236 |
setattr(self, key, kwargs[key]) |
| 237 |
|
| 238 |
if self.name and self.resource_group: |
| 239 |
self.results['images'] = self.get_image(self.resource_group, self.name) |
| 240 |
elif self.name and not self.resource_group: |
| 241 |
self.results['images'] = self.list_images(self.name) |
| 242 |
elif not self.name and self.resource_group: |
| 243 |
self.results['images'] = self.list_images_by_resource_group(self.resource_group) |
| 244 |
elif not self.name and not self.resource_group: |
| 245 |
self.results['images'] = self.list_images() |
| 246 |
return self.results |
| 247 |
|
| 248 |
def get_image(self, resource_group, image_name): |
| 249 |
''' |
| 250 |
Returns image details based on its name |
| 251 |
''' |
| 252 |
|
| 253 |
self.log('Get properties for {0}'.format(self.name)) |
| 254 |
|
| 255 |
result = [] |
| 256 |
item = None |
| 257 |
try: |
| 258 |
item = self.image_client.images.get(resource_group, image_name) |
| 259 |
except ResourceNotFoundError as exc: |
| 260 |
self.fail('Failed to list images - {0}'.format(str(exc))) |
| 261 |
|
| 262 |
result = [self.format_item(item)] |
| 263 |
return result |
| 264 |
|
| 265 |
def list_images_by_resource_group(self, resource_group): |
| 266 |
''' |
| 267 |
Returns image details based on its resource group |
| 268 |
''' |
| 269 |
|
| 270 |
self.log('List images filtered by resource group') |
| 271 |
response = None |
| 272 |
try: |
| 273 |
response = self.image_client.images.list_by_resource_group(resource_group) |
| 274 |
except ResourceNotFoundError as exc: |
| 275 |
self.fail("Failed to list images: {0}".format(str(exc))) |
| 276 |
|
| 277 |
return [self.format_item(x) for x in response if self.has_tags(x.tags, self.tags)] if response else [] |
| 278 |
|
| 279 |
def list_images(self, image_name=None): |
| 280 |
''' |
| 281 |
Returns image details in current subscription |
| 282 |
''' |
| 283 |
|
| 284 |
self.log('List images within current subscription') |
| 285 |
response = None |
| 286 |
results = [] |
| 287 |
try: |
| 288 |
response = self.image_client.images.list() |
| 289 |
except ResourceNotFoundError as exc: |
| 290 |
self.fail("Failed to list all images: {0}".format(str(exc))) |
| 291 |
|
| 292 |
results = [self.format_item(x) for x in response if self.has_tags(x.tags, self.tags)] if response else [] |
| 293 |
if image_name: |
| 294 |
results = [result for result in results if result['name'] == image_name] |
| 295 |
return results |
| 296 |
|
| 297 |
def format_item(self, item): |
| 298 |
d = item.as_dict() |
| 299 |
|
| 300 |
for data_disk in d['storage_profile']['data_disks']: |
| 301 |
if 'managed_disk' in data_disk.keys(): |
| 302 |
data_disk['managed_disk_id'] = data_disk['managed_disk']['id'] |
| 303 |
data_disk.pop('managed_disk', None) |
| 304 |
|
| 305 |
d = { |
| 306 |
'id': d['id'], |
| 307 |
'resource_group': d['id'].split('/')[4], |
| 308 |
'name': d['name'], |
| 309 |
'location': d['location'], |
| 310 |
'tags': d.get('tags'), |
| 311 |
'source': d['source_virtual_machine']['id'] if 'source_virtual_machine' in d.keys() else None, |
| 312 |
'os_type': d['storage_profile']['os_disk']['os_type'], |
| 313 |
'os_state': d['storage_profile']['os_disk']['os_state'], |
| 314 |
'os_disk_caching': d['storage_profile']['os_disk']['caching'], |
| 315 |
'os_storage_account_type': d['storage_profile']['os_disk']['storage_account_type'], |
| 316 |
'os_disk_encryption_set': d['storage_profile']['os_disk'].get('disk_encryption_set'), |
| 317 |
'os_disk': d['storage_profile']['os_disk']['managed_disk']['id'] if 'managed_disk' in d['storage_profile']['os_disk'].keys() else None, |
| 318 |
'os_blob_uri': d['storage_profile']['os_disk']['blob_uri'] if 'blob_uri' in d['storage_profile']['os_disk'].keys() else None, |
| 319 |
'provisioning_state': d['provisioning_state'], |
| 320 |
'data_disks': d['storage_profile']['data_disks'], |
| 321 |
'hyper_v_generation': d.get('hyper_v_generation') |
| 322 |
} |
| 323 |
return d |
| 324 |
|
| 325 |
|
| 326 |
def main(): |
| 327 |
AzureRMImageInfo() |
| 328 |
|
| 329 |
|
| 330 |
if __name__ == '__main__': |
| 331 |
main() |