| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_vmsku_info |
| 15 |
|
| 16 |
version_added: "2.4.0" |
| 17 |
|
| 18 |
short_description: Get compute-related SKUs list |
| 19 |
|
| 20 |
description: |
| 21 |
- Get details for compute-related resource SKUs. |
| 22 |
|
| 23 |
options: |
| 24 |
location: |
| 25 |
description: |
| 26 |
- A region supported by current subscription. |
| 27 |
type: str |
| 28 |
resource_type: |
| 29 |
description: |
| 30 |
- Resource types e.g. "availabilitySets", "snapshots", "disks", etc. |
| 31 |
type: str |
| 32 |
size: |
| 33 |
description: |
| 34 |
- Size name, partial name is accepted. |
| 35 |
type: str |
| 36 |
zone: |
| 37 |
description: |
| 38 |
- Show skus supporting availability zones. |
| 39 |
type: bool |
| 40 |
default: False |
| 41 |
|
| 42 |
extends_documentation_fragment: |
| 43 |
- azure.azcollection.azure |
| 44 |
|
| 45 |
author: |
| 46 |
- Nir Argaman (@nirarg) |
| 47 |
|
| 48 |
''' |
| 49 |
|
| 50 |
EXAMPLES = ''' |
| 51 |
- name: Gather Resource Group info |
| 52 |
azure.azcollection.azure_rm_resourcegroup_info: |
| 53 |
name: "{{ resource_group }}" |
| 54 |
register: rg_info |
| 55 |
|
| 56 |
- name: List available VM SKUs |
| 57 |
azure.azcollection.azure_rm_vmsku_info: |
| 58 |
location: "{{ rg_info.resourcegroups.0.location }}" |
| 59 |
resource_type: "virtualMachines" |
| 60 |
size: "standard_B1" |
| 61 |
zone: true |
| 62 |
register: available_skus_result |
| 63 |
''' |
| 64 |
|
| 65 |
RETURN = ''' |
| 66 |
available_skus: |
| 67 |
description: |
| 68 |
- List of compute-related resource SKUs. |
| 69 |
returned: always |
| 70 |
type: complex |
| 71 |
contains: |
| 72 |
resource_type: |
| 73 |
description: |
| 74 |
- The type of resource the SKU applies to. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: "virtual_machine" |
| 78 |
name: |
| 79 |
description: |
| 80 |
- The name of SKU. |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
sample: "Standard_B1s" |
| 84 |
tier: |
| 85 |
description: |
| 86 |
- Specifies the tier of virtual machines in a scale set. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: "Standard" |
| 90 |
size: |
| 91 |
description: |
| 92 |
- The Size of the SKU. |
| 93 |
returned: always |
| 94 |
type: str |
| 95 |
sample: "B1s" |
| 96 |
family: |
| 97 |
description: |
| 98 |
- The Family of this particular SKU. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: "standardBSFamily" |
| 102 |
locations: |
| 103 |
description: |
| 104 |
- The set of locations that the SKU is available. |
| 105 |
returned: always |
| 106 |
type: list |
| 107 |
sample: ["eastus"] |
| 108 |
location_info: |
| 109 |
description: |
| 110 |
- A list of locations and availability zones in those locations where the SKU is available. |
| 111 |
returned: always |
| 112 |
type: complex |
| 113 |
contains: |
| 114 |
location: |
| 115 |
description: |
| 116 |
- Location of the SKU. |
| 117 |
type: str |
| 118 |
returned: always |
| 119 |
sample: "eastus" |
| 120 |
zones: |
| 121 |
description: |
| 122 |
- List of availability zones where the SKU is supported. |
| 123 |
type: list |
| 124 |
returned: always |
| 125 |
sample: ["1", "2", "3"] |
| 126 |
zone_details: |
| 127 |
description: |
| 128 |
- Details of capabilities available to a SKU in specific zones. |
| 129 |
returned: always |
| 130 |
type: complex |
| 131 |
contains: |
| 132 |
capabilities: |
| 133 |
description: |
| 134 |
- A list of capabilities that are available for the SKU in the specified list of zones. |
| 135 |
type: complex |
| 136 |
returned: always |
| 137 |
contains: |
| 138 |
name: |
| 139 |
description: |
| 140 |
- An invariant to describe the feature. |
| 141 |
type: str |
| 142 |
returned: always |
| 143 |
sample: "ultrassdavailable" |
| 144 |
value: |
| 145 |
description: |
| 146 |
- An invariant if the feature is measured by quantity. |
| 147 |
type: str |
| 148 |
returned: always |
| 149 |
sample: "True" |
| 150 |
capabilities: |
| 151 |
description: |
| 152 |
- A name value pair to describe the capability. |
| 153 |
returned: always |
| 154 |
type: complex |
| 155 |
contains: |
| 156 |
name: |
| 157 |
description: |
| 158 |
- An invariant to describe the feature. |
| 159 |
type: str |
| 160 |
returned: always |
| 161 |
sample: "ultrassdavailable" |
| 162 |
value: |
| 163 |
description: |
| 164 |
- An invariant if the feature is measured by quantity. |
| 165 |
type: str |
| 166 |
returned: always |
| 167 |
sample: "True" |
| 168 |
restrictions: |
| 169 |
description: |
| 170 |
- The restrictions because of which SKU cannot be used. This is empty if there are no restrictions. |
| 171 |
returned: always |
| 172 |
type: complex |
| 173 |
contains: |
| 174 |
type: |
| 175 |
description: |
| 176 |
- The type of restrictions. |
| 177 |
type: str |
| 178 |
returned: always |
| 179 |
sample: "location" |
| 180 |
restriction_values: |
| 181 |
description: |
| 182 |
- The value of restrictions. If the restriction type is set to location. |
| 183 |
This would be different locations where the SKU is restricted. |
| 184 |
type: list |
| 185 |
returned: always |
| 186 |
sample: ["eastus"] |
| 187 |
values: |
| 188 |
description: |
| 189 |
- The value of restrictions. If the restriction type is set to location. |
| 190 |
This would be different locations where the SKU is restricted. |
| 191 |
- This return value has been deprecated and will be removed in a release after |
| 192 |
2027-05-01. Use C(restriction_values) instead. |
| 193 |
type: list |
| 194 |
returned: always |
| 195 |
sample: ["eastus"] |
| 196 |
restriction_info: |
| 197 |
description: |
| 198 |
- The information about the restriction where the SKU cannot be used. |
| 199 |
returned: always |
| 200 |
type: complex |
| 201 |
contains: |
| 202 |
locations: |
| 203 |
description: |
| 204 |
- Locations where the SKU is restricted. |
| 205 |
type: list |
| 206 |
sample: ["location"] |
| 207 |
zones: |
| 208 |
description: |
| 209 |
- List of availability zones where the SKU is restricted. |
| 210 |
type: list |
| 211 |
sample: ["1", "2"] |
| 212 |
reason_code: |
| 213 |
description: |
| 214 |
- The reason for restriction. |
| 215 |
type: str |
| 216 |
sample: "QuotaId" |
| 217 |
''' |
| 218 |
|
| 219 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 220 |
|
| 221 |
try: |
| 222 |
from azure.mgmt.compute import ComputeManagementClient |
| 223 |
from azure.core.exceptions import HttpResponseError |
| 224 |
except ImportError: |
| 225 |
|
| 226 |
pass |
| 227 |
|
| 228 |
|
| 229 |
class AzureRMVmskuInfo(AzureRMModuleBase): |
| 230 |
def __init__(self): |
| 231 |
|
| 232 |
self.module_arg_spec = dict( |
| 233 |
location=dict(type='str'), |
| 234 |
resource_type=dict(type='str'), |
| 235 |
size=dict(type='str'), |
| 236 |
zone=dict(type='bool', default=False) |
| 237 |
) |
| 238 |
|
| 239 |
self.results = dict( |
| 240 |
available_skus=[], |
| 241 |
count=0 |
| 242 |
) |
| 243 |
self.location = None |
| 244 |
self.resource_type = None |
| 245 |
self.size = None |
| 246 |
self.zone = False |
| 247 |
|
| 248 |
super(AzureRMVmskuInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 249 |
supports_check_mode=True, |
| 250 |
supports_tags=False) |
| 251 |
|
| 252 |
def list_skus(self): |
| 253 |
try: |
| 254 |
compute_client = self.get_mgmt_svc_client(ComputeManagementClient, |
| 255 |
base_url=self._cloud_environment.endpoints.resource_manager, |
| 256 |
api_version='2021-07-01') |
| 257 |
skus_result = compute_client.resource_skus.list() |
| 258 |
available_skus = [] |
| 259 |
for sku_info in skus_result: |
| 260 |
if self.location and not _match_location(self.location, sku_info.locations): |
| 261 |
continue |
| 262 |
if not _is_sku_available(sku_info, self.zone): |
| 263 |
continue |
| 264 |
if self.resource_type and not sku_info.resource_type.lower() == self.resource_type.lower(): |
| 265 |
continue |
| 266 |
if self.size and not (sku_info.resource_type == 'virtualMachines' and self.size.lower() in sku_info.name.lower()): |
| 267 |
continue |
| 268 |
if self.zone and not (sku_info.location_info and sku_info.location_info[0].zones): |
| 269 |
continue |
| 270 |
sku_dict = sku_info.as_dict() |
| 271 |
for restriction in sku_dict.get('restrictions', []): |
| 272 |
if 'values' in restriction: |
| 273 |
restriction['restriction_values'] = restriction['values'] |
| 274 |
available_skus.append(sku_dict) |
| 275 |
return available_skus |
| 276 |
except HttpResponseError as e: |
| 277 |
|
| 278 |
raise e |
| 279 |
|
| 280 |
def exec_module(self, **kwargs): |
| 281 |
for key in self.module_arg_spec: |
| 282 |
setattr(self, key, kwargs[key]) |
| 283 |
|
| 284 |
available_skus = self.list_skus() |
| 285 |
self.results['available_skus'] = available_skus |
| 286 |
self.results['count'] = len(available_skus) |
| 287 |
self.module.deprecate( |
| 288 |
"The 'values' return key in 'restrictions' is deprecated. Use 'restriction_values' instead.", |
| 289 |
date="2027-05-01", |
| 290 |
collection_name="azure.azcollection", |
| 291 |
) |
| 292 |
return self.results |
| 293 |
|
| 294 |
|
| 295 |
def _match_location(loc, locations): |
| 296 |
return next((x for x in locations if x.lower() == loc.lower()), None) |
| 297 |
|
| 298 |
|
| 299 |
def _is_sku_available(sku_info, zone): |
| 300 |
""" |
| 301 |
The SKU is unavailable in the following cases: |
| 302 |
1. regional restriction and the region is restricted |
| 303 |
2. parameter "zone" is input which indicates only showing skus with availability zones. |
| 304 |
Meanwhile, zonal restriction and all zones are restricted |
| 305 |
""" |
| 306 |
is_available = True |
| 307 |
is_restrict_zone = False |
| 308 |
is_restrict_location = False |
| 309 |
if not sku_info.restrictions: |
| 310 |
return is_available |
| 311 |
for restriction in sku_info.restrictions: |
| 312 |
if restriction.reason_code == 'NotAvailableForSubscription': |
| 313 |
if restriction.type == 'Zone' and not ( |
| 314 |
set(sku_info.location_info[0].zones or []) - set(restriction.restriction_info.zones or [])): |
| 315 |
is_restrict_zone = True |
| 316 |
if restriction.type == 'Location' and ( |
| 317 |
sku_info.location_info[0].location in (restriction.restriction_info.locations or [])): |
| 318 |
is_restrict_location = True |
| 319 |
if is_restrict_location or (is_restrict_zone and zone): |
| 320 |
is_available = False |
| 321 |
break |
| 322 |
return is_available |
| 323 |
|
| 324 |
|
| 325 |
def main(): |
| 326 |
AzureRMVmskuInfo() |
| 327 |
|
| 328 |
|
| 329 |
if __name__ == '__main__': |
| 330 |
main() |