| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_virtualmachineimage_info |
| 15 |
|
| 16 |
version_added: "0.1.2" |
| 17 |
|
| 18 |
short_description: Get virtual machine image facts |
| 19 |
|
| 20 |
description: |
| 21 |
- Get facts for virtual machine images. |
| 22 |
|
| 23 |
options: |
| 24 |
location: |
| 25 |
description: |
| 26 |
- Azure location value, for example C(westus), C(eastus), C(eastus2), C(northcentralus), etc. |
| 27 |
- Supplying only a location value will yield a list of available publishers for the location. |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
publisher: |
| 31 |
description: |
| 32 |
- Name of an image publisher. List image offerings associated with a particular publisher. |
| 33 |
type: str |
| 34 |
offer: |
| 35 |
description: |
| 36 |
- Name of an image offering. Combine with SKU to see a list of available image versions. |
| 37 |
type: str |
| 38 |
sku: |
| 39 |
description: |
| 40 |
- Image offering SKU. Combine with offer to see a list of available versions. |
| 41 |
type: str |
| 42 |
version: |
| 43 |
description: |
| 44 |
- Specific version number of an image. |
| 45 |
type: str |
| 46 |
|
| 47 |
extends_documentation_fragment: |
| 48 |
- azure.azcollection.azure |
| 49 |
|
| 50 |
author: |
| 51 |
- Chris Houseknecht (@chouseknecht) |
| 52 |
- Matt Davis (@nitzmahone) |
| 53 |
|
| 54 |
''' |
| 55 |
|
| 56 |
EXAMPLES = ''' |
| 57 |
- name: Get facts for a specific image |
| 58 |
azure_rm_virtualmachineimage_info: |
| 59 |
location: eastus |
| 60 |
publisher: OpenLogic |
| 61 |
offer: CentOS |
| 62 |
sku: '7.1' |
| 63 |
version: '7.1.20160308' |
| 64 |
|
| 65 |
- name: List available versions |
| 66 |
azure_rm_virtualmachineimage_info: |
| 67 |
location: eastus |
| 68 |
publisher: OpenLogic |
| 69 |
offer: CentOS |
| 70 |
sku: '7.1' |
| 71 |
|
| 72 |
- name: List available offers |
| 73 |
azure_rm_virtualmachineimage_info: |
| 74 |
location: eastus |
| 75 |
publisher: OpenLogic |
| 76 |
|
| 77 |
- name: List available publishers |
| 78 |
azure_rm_virtualmachineimage_info: |
| 79 |
location: eastus |
| 80 |
''' |
| 81 |
|
| 82 |
RETURN = ''' |
| 83 |
azure_vmimages: |
| 84 |
description: |
| 85 |
- List of image dicts. |
| 86 |
returned: always |
| 87 |
type: list |
| 88 |
example: [ { |
| 89 |
"id": "/Subscriptions/xxx...xxx/Providers/Microsoft.Compute/Locations/eastus/ |
| 90 |
Publishers/OpenLogic/ArtifactTypes/VMImage/Offers/CentOS/Skus/7.1/Versions/7.1.20150410", |
| 91 |
"location": "eastus", |
| 92 |
"name": "7.1.20150410" |
| 93 |
}, |
| 94 |
{ |
| 95 |
"id": "/Subscriptions/xxx...xxx/Providers/Microsoft.Compute/Locations/eastus/ |
| 96 |
Publishers/OpenLogic/ArtifactTypes/VMImage/Offers/CentOS/Skus/7.1/Versions/7.1.20150605", |
| 97 |
"location": "eastus", |
| 98 |
"name": "7.1.20150605" |
| 99 |
}, |
| 100 |
{ |
| 101 |
"id": "/Subscriptions/xxx...xxx/Providers/Microsoft.Compute/Locations/eastus/ |
| 102 |
Publishers/OpenLogic/ArtifactTypes/VMImage/Offers/CentOS/Skus/7.1/Versions/7.1.20150731", |
| 103 |
"location": "eastus", |
| 104 |
"name": "7.1.20150731" |
| 105 |
}, |
| 106 |
{ |
| 107 |
"id": "/Subscriptions/xxx...xxx/Providers/Microsoft.Compute/Locations/eastus/ |
| 108 |
Publishers/OpenLogic/ArtifactTypes/VMImage/Offers/CentOS/Skus/7.1/Versions/7.1.20160308", |
| 109 |
"location": "eastus", |
| 110 |
"name": "7.1.20160308" |
| 111 |
} |
| 112 |
] |
| 113 |
''' |
| 114 |
|
| 115 |
try: |
| 116 |
from azure.core.exceptions import ResourceNotFoundError |
| 117 |
except Exception: |
| 118 |
|
| 119 |
pass |
| 120 |
|
| 121 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 122 |
|
| 123 |
|
| 124 |
AZURE_ENUM_MODULES = ['azure.mgmt.compute.models'] |
| 125 |
|
| 126 |
|
| 127 |
class AzureRMVirtualMachineImageInfo(AzureRMModuleBase): |
| 128 |
|
| 129 |
def __init__(self, **kwargs): |
| 130 |
|
| 131 |
self.module_arg_spec = dict( |
| 132 |
location=dict(type='str', required=True), |
| 133 |
publisher=dict(type='str'), |
| 134 |
offer=dict(type='str'), |
| 135 |
sku=dict(type='str'), |
| 136 |
version=dict(type='str') |
| 137 |
) |
| 138 |
|
| 139 |
self.results = dict( |
| 140 |
changed=False, |
| 141 |
) |
| 142 |
|
| 143 |
self.location = None |
| 144 |
self.publisher = None |
| 145 |
self.offer = None |
| 146 |
self.sku = None |
| 147 |
self.version = None |
| 148 |
|
| 149 |
super(AzureRMVirtualMachineImageInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) |
| 150 |
|
| 151 |
def exec_module(self, **kwargs): |
| 152 |
|
| 153 |
for key in self.module_arg_spec: |
| 154 |
setattr(self, key, kwargs[key]) |
| 155 |
|
| 156 |
if self.location and self.publisher and self.offer and self.sku and self.version: |
| 157 |
self.results['vmimages'] = self.get_item() |
| 158 |
elif self.location and self.publisher and self.offer and self.sku: |
| 159 |
self.results['vmimages'] = self.list_images() |
| 160 |
elif self.location and self.publisher: |
| 161 |
self.results['vmimages'] = self.list_offers() |
| 162 |
elif self.location: |
| 163 |
self.results['vmimages'] = self.list_publishers() |
| 164 |
|
| 165 |
return self.results |
| 166 |
|
| 167 |
def get_item(self): |
| 168 |
item = None |
| 169 |
result = [] |
| 170 |
versions = None |
| 171 |
|
| 172 |
try: |
| 173 |
versions = self.compute_client.virtual_machine_images.list(self.location, |
| 174 |
self.publisher, |
| 175 |
self.offer, |
| 176 |
self.sku, |
| 177 |
top=1, |
| 178 |
orderby='name desc') |
| 179 |
except ResourceNotFoundError: |
| 180 |
pass |
| 181 |
|
| 182 |
if self.version == 'latest': |
| 183 |
item = versions[-1] |
| 184 |
else: |
| 185 |
for version in versions: |
| 186 |
if version.name == self.version: |
| 187 |
item = version |
| 188 |
|
| 189 |
if item: |
| 190 |
result = [self.serialize_obj(item, 'VirtualMachineImage', enum_modules=AZURE_ENUM_MODULES)] |
| 191 |
|
| 192 |
return result |
| 193 |
|
| 194 |
def list_images(self): |
| 195 |
response = None |
| 196 |
results = [] |
| 197 |
try: |
| 198 |
response = self.compute_client.virtual_machine_images.list(self.location, |
| 199 |
self.publisher, |
| 200 |
self.offer, |
| 201 |
self.sku,) |
| 202 |
except ResourceNotFoundError as exc: |
| 203 |
self.fail("Failed to list images: {0}".format(str(exc))) |
| 204 |
|
| 205 |
if response: |
| 206 |
for item in response: |
| 207 |
results.append(self.serialize_obj(item, 'VirtualMachineImageResource', |
| 208 |
enum_modules=AZURE_ENUM_MODULES)) |
| 209 |
return results |
| 210 |
|
| 211 |
def list_offers(self): |
| 212 |
response = None |
| 213 |
results = [] |
| 214 |
try: |
| 215 |
response = self.compute_client.virtual_machine_images.list_offers(self.location, |
| 216 |
self.publisher) |
| 217 |
except ResourceNotFoundError as exc: |
| 218 |
self.fail("Failed to list offers: {0}".format(str(exc))) |
| 219 |
|
| 220 |
if response: |
| 221 |
for item in response: |
| 222 |
results.append(self.serialize_obj(item, 'VirtualMachineImageResource', |
| 223 |
enum_modules=AZURE_ENUM_MODULES)) |
| 224 |
return results |
| 225 |
|
| 226 |
def list_publishers(self): |
| 227 |
response = None |
| 228 |
results = [] |
| 229 |
try: |
| 230 |
response = self.compute_client.virtual_machine_images.list_publishers(self.location) |
| 231 |
except ResourceNotFoundError as exc: |
| 232 |
self.fail("Failed to list publishers: {0}".format(str(exc))) |
| 233 |
|
| 234 |
if response: |
| 235 |
for item in response: |
| 236 |
results.append(self.serialize_obj(item, 'VirtualMachineImageResource', |
| 237 |
enum_modules=AZURE_ENUM_MODULES)) |
| 238 |
return results |
| 239 |
|
| 240 |
|
| 241 |
def main(): |
| 242 |
AzureRMVirtualMachineImageInfo() |
| 243 |
|
| 244 |
|
| 245 |
if __name__ == '__main__': |
| 246 |
main() |