| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_availabilityset |
| 13 |
|
| 14 |
version_added: "0.1.2" |
| 15 |
|
| 16 |
short_description: Manage Azure Availability Set |
| 17 |
|
| 18 |
description: |
| 19 |
- Create, update and delete Azure Availability Set. |
| 20 |
- An availability set cannot be updated, you will have to recreate one instead. |
| 21 |
- The only update operation will be for the tags. |
| 22 |
|
| 23 |
options: |
| 24 |
resource_group: |
| 25 |
description: |
| 26 |
- Name of a resource group where the availability set exists or will be created. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
name: |
| 30 |
description: |
| 31 |
- Name of the availability set. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
state: |
| 35 |
description: |
| 36 |
- Assert the state of the availability set. |
| 37 |
- Use C(present) to create or update a availability set and C(absent) to delete a availability set. |
| 38 |
default: present |
| 39 |
type: str |
| 40 |
choices: |
| 41 |
- absent |
| 42 |
- present |
| 43 |
location: |
| 44 |
description: |
| 45 |
- Valid Azure location. Defaults to location of the resource group. |
| 46 |
type: str |
| 47 |
platform_update_domain_count: |
| 48 |
description: |
| 49 |
- Update domains indicate groups of virtual machines and underlying physical hardware that can be rebooted at the same time. |
| 50 |
type: int |
| 51 |
default: 5 |
| 52 |
platform_fault_domain_count: |
| 53 |
description: |
| 54 |
- Fault domains define the group of virtual machines that share a common power source and network switch. |
| 55 |
- Should be between C(1) and C(3). |
| 56 |
type: int |
| 57 |
default: 3 |
| 58 |
proximity_placement_group: |
| 59 |
description: |
| 60 |
- The proximity placement group that the availability set should be assigned to. |
| 61 |
type: str |
| 62 |
sku: |
| 63 |
description: |
| 64 |
- Define if the availability set supports managed disks. |
| 65 |
default: Classic |
| 66 |
type: str |
| 67 |
choices: |
| 68 |
- Classic |
| 69 |
- Aligned |
| 70 |
extends_documentation_fragment: |
| 71 |
- azure.azcollection.azure |
| 72 |
- azure.azcollection.azure_tags |
| 73 |
|
| 74 |
author: |
| 75 |
- Julien Stroheker (@julienstroheker) |
| 76 |
''' |
| 77 |
|
| 78 |
EXAMPLES = ''' |
| 79 |
- name: Create an availability set with default options |
| 80 |
azure_rm_availabilityset: |
| 81 |
name: myAvailabilitySet |
| 82 |
location: eastus |
| 83 |
resource_group: myResourceGroup |
| 84 |
|
| 85 |
- name: Create an availability set with advanced options |
| 86 |
azure_rm_availabilityset: |
| 87 |
name: myAvailabilitySet |
| 88 |
location: eastus |
| 89 |
resource_group: myResourceGroup |
| 90 |
platform_update_domain_count: 5 |
| 91 |
platform_fault_domain_count: 3 |
| 92 |
proximity_placement_group: myProximityPlacementGroup |
| 93 |
sku: Aligned |
| 94 |
|
| 95 |
- name: Delete an availability set |
| 96 |
azure_rm_availabilityset: |
| 97 |
name: myAvailabilitySet |
| 98 |
location: eastus |
| 99 |
resource_group: myResourceGroup |
| 100 |
state: absent |
| 101 |
''' |
| 102 |
|
| 103 |
RETURN = ''' |
| 104 |
state: |
| 105 |
description: Current state of the availability set. |
| 106 |
returned: always |
| 107 |
type: complex |
| 108 |
contains: |
| 109 |
id: |
| 110 |
description: |
| 111 |
- Resource ID. |
| 112 |
type: str |
| 113 |
sample: "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/v-xisuRG/providers/Microsoft.Compute/availabilitySets/myavailabilityset2" |
| 114 |
location: |
| 115 |
description: |
| 116 |
- Location where the resource lives. |
| 117 |
type: str |
| 118 |
sample: eastus |
| 119 |
name: |
| 120 |
description: |
| 121 |
- Resource name. |
| 122 |
type: str |
| 123 |
sample: myavailabilityset2 |
| 124 |
platform_fault_domain_count: |
| 125 |
description: |
| 126 |
- Fault domains values. |
| 127 |
type: int |
| 128 |
sample: 2 |
| 129 |
platform_update_domain_count: |
| 130 |
description: |
| 131 |
- Update domains values. |
| 132 |
type: int |
| 133 |
sample: 5 |
| 134 |
proximity_placement_group: |
| 135 |
description: |
| 136 |
- The proximity placement group that the availability is assigned to. |
| 137 |
type: str |
| 138 |
sample: myProximityPlacementGroup |
| 139 |
sku: |
| 140 |
description: |
| 141 |
- The availability set supports managed disks. |
| 142 |
type: str |
| 143 |
sample: Aligned |
| 144 |
tags: |
| 145 |
description: |
| 146 |
- Resource tags. |
| 147 |
type: dict |
| 148 |
sample: {env: sandbox} |
| 149 |
|
| 150 |
changed: |
| 151 |
description: Whether or not the resource has changed |
| 152 |
returned: always |
| 153 |
type: bool |
| 154 |
sample: true |
| 155 |
''' |
| 156 |
|
| 157 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 158 |
|
| 159 |
try: |
| 160 |
from azure.mgmt.core.tools import parse_resource_id |
| 161 |
from azure.core.exceptions import ResourceNotFoundError |
| 162 |
except ImportError: |
| 163 |
|
| 164 |
pass |
| 165 |
|
| 166 |
|
| 167 |
def availability_set_to_dict(avaset): |
| 168 |
''' |
| 169 |
Serializing the availability set from the API to Dict |
| 170 |
:return: dict |
| 171 |
''' |
| 172 |
return dict( |
| 173 |
id=avaset.id, |
| 174 |
name=avaset.name, |
| 175 |
location=avaset.location, |
| 176 |
platform_update_domain_count=avaset.platform_update_domain_count, |
| 177 |
platform_fault_domain_count=avaset.platform_fault_domain_count, |
| 178 |
proximity_placement_group=avaset.proximity_placement_group.id if avaset.proximity_placement_group else None, |
| 179 |
tags=avaset.tags, |
| 180 |
sku=avaset.sku.name |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
class AzureRMAvailabilitySet(AzureRMModuleBase): |
| 185 |
"""Configuration class for an Azure RM availability set resource""" |
| 186 |
|
| 187 |
def __init__(self): |
| 188 |
self.module_arg_spec = dict( |
| 189 |
resource_group=dict( |
| 190 |
type='str', |
| 191 |
required=True |
| 192 |
), |
| 193 |
name=dict( |
| 194 |
type='str', |
| 195 |
required=True |
| 196 |
), |
| 197 |
state=dict( |
| 198 |
type='str', |
| 199 |
default='present', |
| 200 |
choices=['present', 'absent'] |
| 201 |
), |
| 202 |
location=dict( |
| 203 |
type='str' |
| 204 |
), |
| 205 |
platform_update_domain_count=dict( |
| 206 |
type='int', |
| 207 |
default=5 |
| 208 |
), |
| 209 |
platform_fault_domain_count=dict( |
| 210 |
type='int', |
| 211 |
default=3 |
| 212 |
), |
| 213 |
proximity_placement_group=dict( |
| 214 |
type='str', |
| 215 |
required=False |
| 216 |
), |
| 217 |
sku=dict( |
| 218 |
type='str', |
| 219 |
default='Classic', |
| 220 |
choices=['Classic', 'Aligned'] |
| 221 |
) |
| 222 |
) |
| 223 |
|
| 224 |
self.resource_group = None |
| 225 |
self.name = None |
| 226 |
self.location = None |
| 227 |
self.tags = None |
| 228 |
self.platform_update_domain_count = None |
| 229 |
self.platform_fault_domain_count = None |
| 230 |
self.proximity_placement_group = None |
| 231 |
self.proximity_placement_group_resource = None |
| 232 |
self.sku = None |
| 233 |
self.state = None |
| 234 |
self.warning = False |
| 235 |
|
| 236 |
self.results = dict(changed=False, state=dict()) |
| 237 |
|
| 238 |
super(AzureRMAvailabilitySet, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 239 |
supports_check_mode=True, |
| 240 |
supports_tags=True) |
| 241 |
|
| 242 |
def exec_module(self, **kwargs): |
| 243 |
"""Main module execution method""" |
| 244 |
|
| 245 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 246 |
setattr(self, key, kwargs[key]) |
| 247 |
|
| 248 |
resource_group = None |
| 249 |
response = None |
| 250 |
to_be_updated = False |
| 251 |
proximity_placement_group_id = None |
| 252 |
|
| 253 |
resource_group = self.get_resource_group(self.resource_group) |
| 254 |
if not self.location: |
| 255 |
self.location = resource_group.location |
| 256 |
|
| 257 |
|
| 258 |
if self.state == 'present': |
| 259 |
response = self.get_availabilityset() |
| 260 |
self.results['state'] = response |
| 261 |
|
| 262 |
if self.proximity_placement_group is not None: |
| 263 |
parsed_proximity_placement_group = parse_resource_id(self.proximity_placement_group) |
| 264 |
proximity_placement_group = self.get_proximity_placement_group(parsed_proximity_placement_group.get('resource_group', self.resource_group), |
| 265 |
parsed_proximity_placement_group.get('name')) |
| 266 |
self.proximity_placement_group_resource = self.compute_models.SubResource(id=proximity_placement_group.id) |
| 267 |
proximity_placement_group_id = proximity_placement_group.id.lower() |
| 268 |
|
| 269 |
if not response: |
| 270 |
to_be_updated = True |
| 271 |
else: |
| 272 |
update_tags, response['tags'] = self.update_tags(response['tags']) |
| 273 |
response_proximity_placement_group = ( |
| 274 |
response['proximity_placement_group'].lower() if response.get('proximity_placement_group') is not None else None |
| 275 |
) |
| 276 |
|
| 277 |
if update_tags: |
| 278 |
self.log("Tags has to be updated") |
| 279 |
to_be_updated = True |
| 280 |
|
| 281 |
if response['platform_update_domain_count'] != self.platform_update_domain_count: |
| 282 |
self.faildeploy('platform_update_domain_count') |
| 283 |
|
| 284 |
if response['platform_fault_domain_count'] != self.platform_fault_domain_count: |
| 285 |
self.faildeploy('platform_fault_domain_count') |
| 286 |
|
| 287 |
if response_proximity_placement_group != proximity_placement_group_id: |
| 288 |
self.faildeploy('proximity_placement_group') |
| 289 |
|
| 290 |
if response['sku'] != self.sku: |
| 291 |
self.faildeploy('sku') |
| 292 |
|
| 293 |
if self.check_mode: |
| 294 |
self.results['changed'] = to_be_updated |
| 295 |
return self.results |
| 296 |
|
| 297 |
if to_be_updated: |
| 298 |
self.results['state'] = self.create_or_update_availabilityset() |
| 299 |
self.results['changed'] = True |
| 300 |
|
| 301 |
elif self.state == 'absent': |
| 302 |
response = self.get_availabilityset() |
| 303 |
if response: |
| 304 |
if not self.check_mode: |
| 305 |
self.delete_availabilityset() |
| 306 |
self.results['changed'] = True |
| 307 |
|
| 308 |
return self.results |
| 309 |
|
| 310 |
def faildeploy(self, param): |
| 311 |
''' |
| 312 |
Helper method to push fail message in the console. |
| 313 |
Useful to notify that the users cannot change some values in a Availability Set |
| 314 |
|
| 315 |
:param: variable's name impacted |
| 316 |
:return: void |
| 317 |
''' |
| 318 |
self.fail("You tried to change {0} but is was unsuccessful. An Availability Set is immutable, except tags".format(str(param))) |
| 319 |
|
| 320 |
def create_or_update_availabilityset(self): |
| 321 |
''' |
| 322 |
Method calling the Azure SDK to create or update the AS. |
| 323 |
:return: void |
| 324 |
''' |
| 325 |
self.log("Creating availabilityset {0}".format(self.name)) |
| 326 |
try: |
| 327 |
params_sku = self.compute_models.Sku( |
| 328 |
name=self.sku |
| 329 |
) |
| 330 |
params = self.compute_models.AvailabilitySet( |
| 331 |
location=self.location, |
| 332 |
tags=self.tags, |
| 333 |
platform_update_domain_count=self.platform_update_domain_count, |
| 334 |
platform_fault_domain_count=self.platform_fault_domain_count, |
| 335 |
proximity_placement_group=self.proximity_placement_group_resource, |
| 336 |
sku=params_sku |
| 337 |
) |
| 338 |
response = self.compute_client.availability_sets.create_or_update(self.resource_group, self.name, params) |
| 339 |
except Exception as e: |
| 340 |
self.log('Error attempting to create the availability set.') |
| 341 |
self.fail("Error creating the availability set: {0}".format(str(e))) |
| 342 |
|
| 343 |
return availability_set_to_dict(response) |
| 344 |
|
| 345 |
def delete_availabilityset(self): |
| 346 |
''' |
| 347 |
Method calling the Azure SDK to delete the AS. |
| 348 |
:return: void |
| 349 |
''' |
| 350 |
self.log("Deleting availabilityset {0}".format(self.name)) |
| 351 |
try: |
| 352 |
response = self.compute_client.availability_sets.delete(self.resource_group, self.name) |
| 353 |
except Exception as e: |
| 354 |
self.log('Error attempting to delete the availability set.') |
| 355 |
self.fail("Error deleting the availability set: {0}".format(str(e))) |
| 356 |
|
| 357 |
return True |
| 358 |
|
| 359 |
def get_availabilityset(self): |
| 360 |
''' |
| 361 |
Method calling the Azure SDK to get an AS. |
| 362 |
:return: void |
| 363 |
''' |
| 364 |
self.log("Checking if the availabilityset {0} is present".format(self.name)) |
| 365 |
found = False |
| 366 |
try: |
| 367 |
response = self.compute_client.availability_sets.get(self.resource_group, self.name) |
| 368 |
found = True |
| 369 |
except ResourceNotFoundError as e: |
| 370 |
self.log('Did not find the Availability set.') |
| 371 |
if found is True: |
| 372 |
return availability_set_to_dict(response) |
| 373 |
else: |
| 374 |
return False |
| 375 |
|
| 376 |
def get_proximity_placement_group(self, resource_group, name): |
| 377 |
try: |
| 378 |
return self.compute_client.proximity_placement_groups.get(resource_group, name) |
| 379 |
except ResourceNotFoundError as exc: |
| 380 |
self.fail("Error fetching proximity placement group {0} - {1}".format(name, str(exc))) |
| 381 |
|
| 382 |
|
| 383 |
def main(): |
| 384 |
"""Main execution""" |
| 385 |
AzureRMAvailabilitySet() |
| 386 |
|
| 387 |
|
| 388 |
if __name__ == '__main__': |
| 389 |
main() |