| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = ''' |
| 9 |
--- |
| 10 |
module: azure_rm_dedicatedhost |
| 11 |
|
| 12 |
version_added: "3.8.0" |
| 13 |
|
| 14 |
short_description: Create, delete and update a dedicated host |
| 15 |
|
| 16 |
description: |
| 17 |
- Creates, deletes, and updates a dedicated host. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- Name of resource group. |
| 23 |
required: true |
| 24 |
type: str |
| 25 |
host_group_name: |
| 26 |
description: |
| 27 |
- The name of the host group. |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of the dedicated host. |
| 33 |
required: true |
| 34 |
type: str |
| 35 |
location: |
| 36 |
description: |
| 37 |
- Valid Azure location for dedicated host. Defaults to location of resource group. |
| 38 |
type: str |
| 39 |
platform_fault_domain: |
| 40 |
description: |
| 41 |
- Fault domain of the dedicated host within a dedicated host group. |
| 42 |
type: int |
| 43 |
choices: |
| 44 |
- 0 |
| 45 |
sku: |
| 46 |
description: |
| 47 |
- SKU of the dedicated host for Hardware Generation and VM family. |
| 48 |
- Only C(name) is required to be set |
| 49 |
type: dict |
| 50 |
suboptions: |
| 51 |
name: |
| 52 |
description: |
| 53 |
- The sku name. |
| 54 |
type: str |
| 55 |
auto_replace_on_failure: |
| 56 |
description: |
| 57 |
- Specifies whether the dedicated host should be replaced automatically in case of a failure. |
| 58 |
- The value is defaulted to C(true) when not provided. |
| 59 |
default: true |
| 60 |
type: bool |
| 61 |
license_type: |
| 62 |
description: |
| 63 |
- Specifies the software license type that will be applied to the VMs deployed on the dedicated host. |
| 64 |
type: str |
| 65 |
choices: |
| 66 |
- 'None' |
| 67 |
- 'Windows_Server_Hybrid' |
| 68 |
- 'Windows_Server_Perpetual' |
| 69 |
is_restart: |
| 70 |
description: |
| 71 |
- Wether to restart the dedicated host. |
| 72 |
type: bool |
| 73 |
default: false |
| 74 |
state: |
| 75 |
description: |
| 76 |
- Assert the state of the host group. Use C(present) to create or update and C(absent) to delete. |
| 77 |
default: present |
| 78 |
type: str |
| 79 |
choices: |
| 80 |
- absent |
| 81 |
- present |
| 82 |
|
| 83 |
extends_documentation_fragment: |
| 84 |
- azure.azcollection.azure |
| 85 |
- azure.azcollection.azure_tags |
| 86 |
|
| 87 |
author: |
| 88 |
- magodo (@magodo) |
| 89 |
- xuzhang3 (@xuzhang3) |
| 90 |
- Fred Sun (@Fred-sun) |
| 91 |
''' |
| 92 |
|
| 93 |
EXAMPLES = ''' |
| 94 |
- name: Create a dedicated host |
| 95 |
azure_rm_dedicatedhost: |
| 96 |
resource_group: myAzureResourceGroup |
| 97 |
name: mydedicatedhost |
| 98 |
location: eastus |
| 99 |
platform_fault_domain: 1 |
| 100 |
sku: |
| 101 |
name: DSv3-Type1 |
| 102 |
state: present |
| 103 |
|
| 104 |
- name: Delete a dedicated host |
| 105 |
azure_rm_dedicatedhost: |
| 106 |
resource_group: myAzureResourceGroup |
| 107 |
host_group_name: myhostgroup |
| 108 |
name: mydedicatedhost |
| 109 |
state: absent |
| 110 |
''' |
| 111 |
|
| 112 |
RETURN = ''' |
| 113 |
state: |
| 114 |
description: |
| 115 |
- Gets a list of dedicated host. |
| 116 |
returned: always |
| 117 |
type: complex |
| 118 |
contains: |
| 119 |
host_group_name: |
| 120 |
description: |
| 121 |
- The name of the host group. |
| 122 |
type: str |
| 123 |
returned: always |
| 124 |
sample: myDedicatedHostGroup |
| 125 |
resource_group: |
| 126 |
description: |
| 127 |
- The name of the resource group. |
| 128 |
type: str |
| 129 |
returned: always |
| 130 |
sample: myResourceGroup |
| 131 |
name: |
| 132 |
description: |
| 133 |
- The dedicate hsot name. |
| 134 |
type: str |
| 135 |
returned: always |
| 136 |
sample: myHost |
| 137 |
id: |
| 138 |
description: |
| 139 |
- The dedicate host ID. |
| 140 |
type: str |
| 141 |
returned: always |
| 142 |
sample: "/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/HostGroups/myDedicatedHostGroup/hosts/myHost" |
| 143 |
location: |
| 144 |
description: |
| 145 |
- Resource location. |
| 146 |
type: str |
| 147 |
returned: always |
| 148 |
sample: eastus |
| 149 |
tags: |
| 150 |
description: |
| 151 |
- Resource tags. |
| 152 |
type: dict |
| 153 |
returned: always |
| 154 |
sample: {'key1': 'value1'} |
| 155 |
sku: |
| 156 |
description: |
| 157 |
- SKU of the dedicated host for Hardware Generation and VM family. |
| 158 |
type: dict |
| 159 |
returned: always |
| 160 |
sample: {'name': 'DSv3-Type4'} |
| 161 |
auto_replace_on_failure: |
| 162 |
description: |
| 163 |
- Specifies whether the dedicated host should be replaced automatically in case of a failure. |
| 164 |
type: bool |
| 165 |
returned: always |
| 166 |
sample: true |
| 167 |
license_type: |
| 168 |
description: |
| 169 |
- Specifies the software license type that will be applied to the VMs deployed on the dedicated host. |
| 170 |
type: str |
| 171 |
returned: always |
| 172 |
sample: Windows_Server_Hybrid |
| 173 |
provisioning_state: |
| 174 |
description: |
| 175 |
- The provisioning state, which only appears in the response. |
| 176 |
type: str |
| 177 |
returned: always |
| 178 |
sample: Succeeded |
| 179 |
platform_fault_domain: |
| 180 |
description: |
| 181 |
- Fault domain of the dedicated host within a dedicated host. |
| 182 |
type: str |
| 183 |
returned: always |
| 184 |
sample: 1 |
| 185 |
virtual_machines: |
| 186 |
description: |
| 187 |
- A list of references to all virtual machines in the Dedicated Host. |
| 188 |
type: str |
| 189 |
returned: always |
| 190 |
sample: [{"id": "/subscriptions/subId/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/vm1"}] |
| 191 |
''' |
| 192 |
|
| 193 |
from ansible.module_utils.basic import _load_params |
| 194 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, \ |
| 195 |
normalize_location_name |
| 196 |
|
| 197 |
try: |
| 198 |
from azure.core.exceptions import ResourceNotFoundError |
| 199 |
from azure.core.polling import LROPoller |
| 200 |
except ImportError: |
| 201 |
|
| 202 |
pass |
| 203 |
|
| 204 |
|
| 205 |
class AzureRMDedicatedHost(AzureRMModuleBase): |
| 206 |
|
| 207 |
def __init__(self): |
| 208 |
|
| 209 |
_load_params() |
| 210 |
|
| 211 |
self.module_arg_spec = dict( |
| 212 |
resource_group=dict(type='str', required=True), |
| 213 |
host_group_name=dict(type='str', required=True), |
| 214 |
name=dict(type='str', required=True), |
| 215 |
location=dict(type='str'), |
| 216 |
sku=dict( |
| 217 |
type='dict', |
| 218 |
options=dict( |
| 219 |
name=dict(type='str') |
| 220 |
) |
| 221 |
), |
| 222 |
platform_fault_domain=dict(type='int', choices=[0]), |
| 223 |
auto_replace_on_failure=dict(type='bool', default=True), |
| 224 |
license_type=dict(type='str', choices=['None', 'Windows_Server_Hybrid', 'Windows_Server_Perpetual']), |
| 225 |
state=dict(choices=['present', 'absent'], default='present', type='str'), |
| 226 |
is_restart=dict(type='bool', default=False) |
| 227 |
) |
| 228 |
|
| 229 |
self.results = dict( |
| 230 |
changed=False, |
| 231 |
state=dict() |
| 232 |
) |
| 233 |
|
| 234 |
self.resource_group = None |
| 235 |
self.host_group_name = None |
| 236 |
self.name = None |
| 237 |
self.state = None |
| 238 |
self.location = None |
| 239 |
self.tags = None |
| 240 |
self.sku = None |
| 241 |
self.platform_fault_domain = None |
| 242 |
self.auto_replace_on_failure = None |
| 243 |
self.license_type = None |
| 244 |
self.is_restart = None |
| 245 |
|
| 246 |
super(AzureRMDedicatedHost, self).__init__(self.module_arg_spec, |
| 247 |
supports_tags=True, |
| 248 |
supports_check_mode=True) |
| 249 |
|
| 250 |
def exec_module(self, **kwargs): |
| 251 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 252 |
setattr(self, key, kwargs[key]) |
| 253 |
|
| 254 |
changed = False |
| 255 |
|
| 256 |
|
| 257 |
resource_group = self.get_resource_group(self.resource_group) |
| 258 |
if not self.location: |
| 259 |
|
| 260 |
self.location = resource_group.location |
| 261 |
|
| 262 |
self.location = normalize_location_name(self.location) |
| 263 |
results = self.get_resource() |
| 264 |
|
| 265 |
if results is not None: |
| 266 |
if self.state == 'present': |
| 267 |
update_tags, self.tags = self.update_tags(results['tags']) |
| 268 |
if update_tags: |
| 269 |
changed = True |
| 270 |
elif self.auto_replace_on_failure is not None and bool(self.auto_replace_on_failure) != bool(results['auto_replace_on_failure']): |
| 271 |
changed = True |
| 272 |
elif self.platform_fault_domain and self.platform_fault_domain != results['platform_fault_domain']: |
| 273 |
changed = True |
| 274 |
elif self.sku and self.sku['name'] != results['sku']['name']: |
| 275 |
changed = True |
| 276 |
elif self.license_type and self.license_type != results['license_type']: |
| 277 |
changed = True |
| 278 |
if not self.check_mode and changed: |
| 279 |
results = self.update_dedicatedhost() |
| 280 |
else: |
| 281 |
changed = True |
| 282 |
if not self.check_mode: |
| 283 |
results = self.delete_dedicatedhost() |
| 284 |
else: |
| 285 |
if self.state == 'present': |
| 286 |
changed = True |
| 287 |
if not self.check_mode: |
| 288 |
results = self.create_dedicatedhost() |
| 289 |
else: |
| 290 |
self.log("The dedecated host not exist") |
| 291 |
changed = False |
| 292 |
|
| 293 |
if results is not None and self.is_restart: |
| 294 |
self.log("Restart the dedicated host. The operation will complete successfully once the dedicated host has restarted and is running.") |
| 295 |
changed = True |
| 296 |
if not self.check_mode: |
| 297 |
self.restart_dedicatedhost() |
| 298 |
|
| 299 |
self.results['changed'] = changed |
| 300 |
self.results['state'] = results |
| 301 |
return self.results |
| 302 |
|
| 303 |
def get_resource(self): |
| 304 |
self.log('Get host facts for {0}'.format(self.name)) |
| 305 |
|
| 306 |
try: |
| 307 |
response = self.compute_client.dedicated_hosts.get(self.resource_group, self.host_group_name, self.name) |
| 308 |
except ResourceNotFoundError: |
| 309 |
return None |
| 310 |
|
| 311 |
return self.host_to_dict(response) |
| 312 |
|
| 313 |
def create_dedicatedhost(self): |
| 314 |
try: |
| 315 |
|
| 316 |
response = self.compute_client.dedicated_hosts.begin_create_or_update(resource_group_name=self.resource_group, |
| 317 |
host_group_name=self.host_group_name, |
| 318 |
host_name=self.name, |
| 319 |
parameters=dict(location=self.location, |
| 320 |
sku=self.sku, |
| 321 |
tags=self.tags, |
| 322 |
platform_fault_domain=self.platform_fault_domain, |
| 323 |
auto_replace_on_failure=self.auto_replace_on_failure, |
| 324 |
license_type=self.license_type)) |
| 325 |
if isinstance(response, LROPoller): |
| 326 |
return self.host_to_dict(self.get_poller_result(response)) |
| 327 |
except Exception as exc: |
| 328 |
self.fail("Error creating or updating host {0} - {1}".format(self.name, str(exc))) |
| 329 |
|
| 330 |
def update_dedicatedhost(self): |
| 331 |
try: |
| 332 |
|
| 333 |
response = self.compute_client.dedicated_hosts.begin_update(resource_group_name=self.resource_group, |
| 334 |
host_group_name=self.host_group_name, |
| 335 |
host_name=self.name, |
| 336 |
parameters=dict(sku=self.sku, |
| 337 |
tags=self.tags, |
| 338 |
platform_fault_domain=self.platform_fault_domain, |
| 339 |
auto_replace_on_failure=self.auto_replace_on_failure, |
| 340 |
license_type=self.license_type)) |
| 341 |
if isinstance(response, LROPoller): |
| 342 |
return self.host_to_dict(self.get_poller_result(response)) |
| 343 |
except Exception as exc: |
| 344 |
self.fail("Error creating or updating host {0} - {1}".format(self.name, str(exc))) |
| 345 |
|
| 346 |
def restart_dedicatedhost(self): |
| 347 |
try: |
| 348 |
|
| 349 |
response = self.compute_client.dedicated_hosts.begin_restart(resource_group_name=self.resource_group, |
| 350 |
host_group_name=self.host_group_name, |
| 351 |
host_name=self.name) |
| 352 |
if isinstance(response, LROPoller): |
| 353 |
return self.get_poller_result(response) |
| 354 |
except Exception as exc: |
| 355 |
self.fail("Error restarting host {0} - {1}".format(self.name, str(exc))) |
| 356 |
|
| 357 |
def delete_dedicatedhost(self): |
| 358 |
try: |
| 359 |
|
| 360 |
response = self.compute_client.dedicated_hosts.begin_delete(resource_group_name=self.resource_group, |
| 361 |
host_group_name=self.host_group_name, |
| 362 |
host_name=self.name) |
| 363 |
if isinstance(response, LROPoller): |
| 364 |
return self.get_poller_result(response) |
| 365 |
|
| 366 |
except Exception as exc: |
| 367 |
self.fail("Error deleting host {0} - {1}".format(self.name, str(exc))) |
| 368 |
|
| 369 |
def host_to_dict(self, host): |
| 370 |
result = dict( |
| 371 |
resource_group=self.resource_group, |
| 372 |
host_group_name=self.host_group_name, |
| 373 |
id=host.id, |
| 374 |
name=host.name, |
| 375 |
location=host.location, |
| 376 |
tags=host.tags, |
| 377 |
sku=dict(), |
| 378 |
auto_replace_on_failure=host.auto_replace_on_failure, |
| 379 |
license_type=host.license_type, |
| 380 |
provisioning_state=host.provisioning_state, |
| 381 |
platform_fault_domain=host.platform_fault_domain, |
| 382 |
virtual_machines=[] |
| 383 |
) |
| 384 |
if host.virtual_machines: |
| 385 |
result['virtual_machines'] = [dict(id=item.id) for item in host.virtual_machines] |
| 386 |
|
| 387 |
if host.sku: |
| 388 |
result['sku']['name'] = host.sku.name |
| 389 |
return result |
| 390 |
|
| 391 |
|
| 392 |
def main(): |
| 393 |
AzureRMDedicatedHost() |
| 394 |
|
| 395 |
|
| 396 |
if __name__ == '__main__': |
| 397 |
main() |