| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = ''' |
| 9 |
--- |
| 10 |
module: azure_rm_ml_compute |
| 11 |
version_added: "3.15.0" |
| 12 |
short_description: Create, Update or Delete an Azure Machine Learning Compute |
| 13 |
description: |
| 14 |
- Create, Update or Delete an Azure Machine Learning Compute. |
| 15 |
options: |
| 16 |
name: |
| 17 |
description: |
| 18 |
- Name of the Azure ML compute. |
| 19 |
type: str |
| 20 |
required: true |
| 21 |
resource_group: |
| 22 |
description: |
| 23 |
- Name of resource group. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
ml_workspace: |
| 27 |
description: |
| 28 |
- Name of the Azure ML workspace. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
description: |
| 32 |
description: |
| 33 |
- Description of the compute target. |
| 34 |
type: str |
| 35 |
required: false |
| 36 |
resource_definition: |
| 37 |
description: |
| 38 |
- Compute definition in YAML |
| 39 |
type: str |
| 40 |
required: false |
| 41 |
identity_type: |
| 42 |
description: |
| 43 |
- The type of managed identity. |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- SystemAssigned |
| 47 |
- UserAssigned |
| 48 |
required: false |
| 49 |
size: |
| 50 |
description: |
| 51 |
- "VM size to use for the compute target. More details can be |
| 52 |
found here: https://aka.ms/azureml-vm-details." |
| 53 |
type: str |
| 54 |
required: false |
| 55 |
ssh_key_value: |
| 56 |
description: |
| 57 |
- SSH public key of the administrator user account. |
| 58 |
type: str |
| 59 |
required: false |
| 60 |
ssh_public_access_enabled: |
| 61 |
description: |
| 62 |
- Indicates whether public SSH port is enabled. |
| 63 |
type: bool |
| 64 |
required: false |
| 65 |
subnet: |
| 66 |
description: |
| 67 |
- Name of the subnet. Can also reference a subnet in an |
| 68 |
existing vnet by ID instead of name. If subnet ID is |
| 69 |
specified then vnet_name will be ignored. Subnet ID can refer |
| 70 |
to a vnet/subnet in another RG by specifying the fully |
| 71 |
qualified subnet ID. Required when vnet name is specified. |
| 72 |
type: str |
| 73 |
required: false |
| 74 |
type: |
| 75 |
description: |
| 76 |
- The type of compute target. |
| 77 |
choices: |
| 78 |
- amlcompute |
| 79 |
- computeinstance |
| 80 |
type: str |
| 81 |
required: false |
| 82 |
user_assigned_identities: |
| 83 |
description: |
| 84 |
- User Assigned Identities. |
| 85 |
type: list |
| 86 |
elements: str |
| 87 |
vnet_name: |
| 88 |
description: |
| 89 |
- Name of the virtual network. |
| 90 |
type: str |
| 91 |
required: false |
| 92 |
admin_username: |
| 93 |
description: |
| 94 |
- Name of the administrator user account that can be used to |
| 95 |
SSH into the node(s). |
| 96 |
type: str |
| 97 |
required: false |
| 98 |
admin_password: |
| 99 |
description: |
| 100 |
- Password for the administrator user account. |
| 101 |
type: str |
| 102 |
required: false |
| 103 |
idle_time_before_scale_down: |
| 104 |
description: |
| 105 |
- Node idle time in seconds before scaling down the cluster. |
| 106 |
type: int |
| 107 |
required: false |
| 108 |
location: |
| 109 |
description: |
| 110 |
- The location to be used for the new compute. |
| 111 |
If not specified, defaults to the location of the workspace. |
| 112 |
type: str |
| 113 |
required: false |
| 114 |
max_instances: |
| 115 |
description: |
| 116 |
- The maximum number of nodes to use on the cluster. |
| 117 |
required: false |
| 118 |
type: int |
| 119 |
min_instances: |
| 120 |
description: |
| 121 |
- The minimum number of nodes to use on the cluster. |
| 122 |
required: false |
| 123 |
type: int |
| 124 |
tier: |
| 125 |
description: |
| 126 |
- VM priority tier. |
| 127 |
choices: |
| 128 |
- dedicated |
| 129 |
- low_priority |
| 130 |
type: str |
| 131 |
user_object_id: |
| 132 |
description: |
| 133 |
- AAD object ID of the assigned user. |
| 134 |
type: str |
| 135 |
required: false |
| 136 |
user_tenant_id: |
| 137 |
description: |
| 138 |
- AAD tenant ID of the assigned user. |
| 139 |
type: str |
| 140 |
required: false |
| 141 |
state: |
| 142 |
description: |
| 143 |
- State of the Compute. Use C(present) to create |
| 144 |
or update and C(absent) to delete. C(start) to |
| 145 |
start, C(stop) to stop and C(restart) to restart. |
| 146 |
default: present |
| 147 |
type: str |
| 148 |
choices: |
| 149 |
- absent |
| 150 |
- present |
| 151 |
- start |
| 152 |
- stop |
| 153 |
- restart |
| 154 |
|
| 155 |
extends_documentation_fragment: |
| 156 |
- azure.azcollection.azure |
| 157 |
- azure.azcollection.azure_tags |
| 158 |
|
| 159 |
author: |
| 160 |
- Bill Peck (@p3ck) |
| 161 |
''' |
| 162 |
|
| 163 |
EXAMPLES = ''' |
| 164 |
- name: Create ML Compute |
| 165 |
azure.azcollection.azure_rm_ml_compute: |
| 166 |
location: eastus |
| 167 |
name: MyCompute |
| 168 |
description: My Compute created by Ansible |
| 169 |
resource_group: myResourceGroup |
| 170 |
ml_workspace: myMLWorkspace |
| 171 |
type: amlcompute |
| 172 |
min_instances: 0 |
| 173 |
max_instances: 8 |
| 174 |
tags: |
| 175 |
createdByToolkit: Ansible |
| 176 |
''' |
| 177 |
|
| 178 |
RETURN = ''' |
| 179 |
changed: |
| 180 |
description: |
| 181 |
- Whether the resource is changed. |
| 182 |
returned: always |
| 183 |
type: bool |
| 184 |
sample: false |
| 185 |
ml_compute: |
| 186 |
description: |
| 187 |
- Compute that was just created or updated. |
| 188 |
returned: always |
| 189 |
type: dict |
| 190 |
sample: { |
| 191 |
"description": "Compute Integration Tests", |
| 192 |
"enable_node_public_ip": true, |
| 193 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-compute/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxx-xxx/computes/amlxxxxxxx-xxxcomp", |
| 194 |
"idle_time_before_scale_down": 120, |
| 195 |
"location": "eastus", |
| 196 |
"max_instances": 200, |
| 197 |
"min_instances": 0, |
| 198 |
"name": "amlxxxxxxx-xxxcomp", |
| 199 |
"network_settings": {}, |
| 200 |
"provisioning_state": "Succeeded", |
| 201 |
"size": "STANDARD_DS3_V2", |
| 202 |
"ssh_public_access_enabled": true, |
| 203 |
"tags": { |
| 204 |
"created_by": "Ansible" |
| 205 |
}, |
| 206 |
"tier": "dedicated", |
| 207 |
"type": "amlcompute" |
| 208 |
} |
| 209 |
''' |
| 210 |
|
| 211 |
|
| 212 |
try: |
| 213 |
import io |
| 214 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon |
| 215 |
from azure.core.exceptions import ResourceNotFoundError |
| 216 |
from azure.ai.ml.entities._load_functions import load_compute |
| 217 |
except ImportError: |
| 218 |
|
| 219 |
pass |
| 220 |
|
| 221 |
|
| 222 |
class AzureRMMLCompute(MLClientCommon): |
| 223 |
|
| 224 |
UPDATE_OVERRIDES = ['min_instances', |
| 225 |
'max_instances', |
| 226 |
'idle_time_before_scale_down', |
| 227 |
'identity_type', |
| 228 |
'user_assigned_identities', |
| 229 |
'tags' |
| 230 |
] |
| 231 |
|
| 232 |
PARAM_OVERRIDES = UPDATE_OVERRIDES + ['name', |
| 233 |
'type', |
| 234 |
'vnet_name', |
| 235 |
'subnet', |
| 236 |
'admin_username', |
| 237 |
'admin_password', |
| 238 |
'ssh_key_value', |
| 239 |
'size', |
| 240 |
'user_tenant_id', |
| 241 |
'user_object_id', |
| 242 |
'description', |
| 243 |
'enable_node_public_ip', |
| 244 |
'tier', |
| 245 |
'ssh_public_access_enabled', |
| 246 |
'location', |
| 247 |
] |
| 248 |
|
| 249 |
def __init__(self): |
| 250 |
|
| 251 |
self.module_arg_spec = dict( |
| 252 |
name=dict( |
| 253 |
type='str', |
| 254 |
required=True, |
| 255 |
), |
| 256 |
description=dict( |
| 257 |
type='str', |
| 258 |
required=False, |
| 259 |
), |
| 260 |
resource_definition=dict( |
| 261 |
type='str', |
| 262 |
required=False, |
| 263 |
), |
| 264 |
location=dict( |
| 265 |
type='str', |
| 266 |
required=False, |
| 267 |
), |
| 268 |
resource_group=dict( |
| 269 |
type='str', |
| 270 |
required=True, |
| 271 |
), |
| 272 |
ml_workspace=dict( |
| 273 |
type='str', |
| 274 |
required=True, |
| 275 |
), |
| 276 |
identity_type=dict( |
| 277 |
type='str', |
| 278 |
required=False, |
| 279 |
choices=['SystemAssigned', 'UserAssigned'], |
| 280 |
), |
| 281 |
size=dict( |
| 282 |
type='str', |
| 283 |
required=False, |
| 284 |
), |
| 285 |
ssh_key_value=dict( |
| 286 |
type='str', |
| 287 |
required=False, |
| 288 |
no_log=True, |
| 289 |
), |
| 290 |
ssh_public_access_enabled=dict( |
| 291 |
type='bool', |
| 292 |
required=False, |
| 293 |
), |
| 294 |
subnet=dict( |
| 295 |
type='str', |
| 296 |
required=False, |
| 297 |
), |
| 298 |
type=dict( |
| 299 |
choices=['amlcompute', 'computeinstance'], |
| 300 |
type='str', |
| 301 |
required=False, |
| 302 |
), |
| 303 |
user_assigned_identities=dict( |
| 304 |
type='list', |
| 305 |
elements='str', |
| 306 |
required=False, |
| 307 |
), |
| 308 |
vnet_name=dict( |
| 309 |
type='str', |
| 310 |
required=False, |
| 311 |
), |
| 312 |
admin_username=dict( |
| 313 |
type='str', |
| 314 |
required=False, |
| 315 |
), |
| 316 |
admin_password=dict( |
| 317 |
type='str', |
| 318 |
required=False, |
| 319 |
no_log=True, |
| 320 |
), |
| 321 |
idle_time_before_scale_down=dict( |
| 322 |
type='int', |
| 323 |
required=False, |
| 324 |
), |
| 325 |
max_instances=dict( |
| 326 |
type='int', |
| 327 |
required=False, |
| 328 |
), |
| 329 |
min_instances=dict( |
| 330 |
type='int', |
| 331 |
required=False, |
| 332 |
), |
| 333 |
tier=dict( |
| 334 |
choices=['dedicated', 'low_priority'], |
| 335 |
type='str', |
| 336 |
required=False, |
| 337 |
), |
| 338 |
user_object_id=dict( |
| 339 |
type='str', |
| 340 |
required=False, |
| 341 |
), |
| 342 |
user_tenant_id=dict( |
| 343 |
type='str', |
| 344 |
required=False, |
| 345 |
), |
| 346 |
state=dict( |
| 347 |
type='str', |
| 348 |
default='present', |
| 349 |
choices=['present', 'absent', 'start', 'stop', 'restart'], |
| 350 |
), |
| 351 |
) |
| 352 |
|
| 353 |
self._client = None |
| 354 |
self.ml_compute = None |
| 355 |
self.ml_registry = None |
| 356 |
self.state = None |
| 357 |
|
| 358 |
self.results = dict( |
| 359 |
changed=False, |
| 360 |
compare=[], |
| 361 |
ml_compute={} |
| 362 |
) |
| 363 |
|
| 364 |
super(AzureRMMLCompute, self).__init__(self.module_arg_spec, |
| 365 |
supports_check_mode=True, |
| 366 |
) |
| 367 |
|
| 368 |
def exec_module(self, **kwargs): |
| 369 |
|
| 370 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 371 |
setattr(self, key, kwargs[key]) |
| 372 |
|
| 373 |
if self.resource_definition: |
| 374 |
resource_definition = io.StringIO(self.resource_definition) |
| 375 |
else: |
| 376 |
resource_definition = None |
| 377 |
|
| 378 |
changed = False |
| 379 |
ml_compute_info = self.get(self.name) |
| 380 |
|
| 381 |
if self.state == 'present': |
| 382 |
if ml_compute_info: |
| 383 |
|
| 384 |
params_override = self.update_params(kwargs, |
| 385 |
self.UPDATE_OVERRIDES, |
| 386 |
ml_compute_info) |
| 387 |
|
| 388 |
|
| 389 |
ml_compute = load_compute(None, |
| 390 |
params_override=params_override) |
| 391 |
ml_compute_info = self.entity_to_dict(ml_compute_info) |
| 392 |
changed = not self.default_compare({}, |
| 393 |
self.entity_to_dict(ml_compute), |
| 394 |
ml_compute_info, |
| 395 |
'', |
| 396 |
self.results) |
| 397 |
if changed and not self.check_mode: |
| 398 |
response = self.client.compute.begin_update(ml_compute) |
| 399 |
ml_compute_object = self.get_poller_result(response) |
| 400 |
ml_compute_info = self.entity_to_dict(ml_compute_object) |
| 401 |
|
| 402 |
else: |
| 403 |
|
| 404 |
params_override = self.update_params(kwargs, self.PARAM_OVERRIDES) |
| 405 |
|
| 406 |
|
| 407 |
ml_compute = load_compute(resource_definition, |
| 408 |
params_override=params_override) |
| 409 |
changed = True |
| 410 |
if not self.check_mode: |
| 411 |
response = self.client.begin_create_or_update(ml_compute) |
| 412 |
ml_compute_object = self.get_poller_result(response) |
| 413 |
ml_compute_info = self.entity_to_dict(ml_compute_object) |
| 414 |
elif self.state == 'absent': |
| 415 |
|
| 416 |
if ml_compute_info: |
| 417 |
changed = True |
| 418 |
if not self.check_mode: |
| 419 |
response = self.client.compute.begin_delete(name=self.name) |
| 420 |
result = self.get_poller_result(response) |
| 421 |
ml_compute_info = self.get(self.name, as_dict=True) |
| 422 |
elif self.state == 'start': |
| 423 |
|
| 424 |
if hasattr(ml_compute_info, 'state'): |
| 425 |
if ml_compute_info.state != "Running": |
| 426 |
changed = True |
| 427 |
if not self.check_mode: |
| 428 |
self.start(self.name) |
| 429 |
ml_compute_info = self.get(self.name, as_dict=True) |
| 430 |
else: |
| 431 |
self.fail("Compute instance doesn't exist") |
| 432 |
elif self.state == 'stop': |
| 433 |
|
| 434 |
if hasattr(ml_compute_info, 'state'): |
| 435 |
if ml_compute_info.state != "Stopped": |
| 436 |
changed = True |
| 437 |
if not self.check_mode: |
| 438 |
self.stop(self.name) |
| 439 |
ml_compute_info = self.get(self.name, as_dict=True) |
| 440 |
else: |
| 441 |
self.fail("Compute instance doesn't exist") |
| 442 |
elif self.state == 'restart': |
| 443 |
|
| 444 |
if hasattr(ml_compute_info, 'state'): |
| 445 |
changed = True |
| 446 |
if not self.check_mode: |
| 447 |
self.restart(self.name) |
| 448 |
ml_compute_info = self.get(self.name, as_dict=True) |
| 449 |
else: |
| 450 |
self.fail("Compute instance doesn't exist") |
| 451 |
|
| 452 |
self.results['ml_compute'] = ml_compute_info |
| 453 |
self.results['changed'] = changed |
| 454 |
return self.results |
| 455 |
|
| 456 |
def update_params(self, kwargs, param_overrides, ml_compute=None): |
| 457 |
params_override = [] |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
if ml_compute: |
| 463 |
items = self.entity_to_dict(ml_compute).items() |
| 464 |
params_override = [{k: v} for k, v in items if k != "ssh_settings"] |
| 465 |
|
| 466 |
|
| 467 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 468 |
if kwargs[key] is not None and key in param_overrides: |
| 469 |
if key == 'vnet_name': |
| 470 |
params_override.append({"network_settings.vnet_name": |
| 471 |
kwargs[key]}) |
| 472 |
elif key == 'subnet': |
| 473 |
params_override.append({"network_settings.subnet": |
| 474 |
kwargs[key]}) |
| 475 |
elif key == 'admin_username': |
| 476 |
params_override.append({"ssh_settings.admin_username": |
| 477 |
kwargs[key]}) |
| 478 |
elif key == 'admin_password': |
| 479 |
params_override.append({"ssh_settings.admin_password": |
| 480 |
kwargs[key]}) |
| 481 |
elif key == 'ssh_key_value': |
| 482 |
params_override.append({"ssh_settings.ssh_key_value": |
| 483 |
kwargs[key]}) |
| 484 |
elif key == 'user_tenant_id': |
| 485 |
params_override.append({"create_on_behalf_of.user_tenant_id": |
| 486 |
kwargs[key]}) |
| 487 |
elif key == 'user_object_id': |
| 488 |
params_override.append({"create_on_behalf_of.user_object_id": |
| 489 |
kwargs[key]}) |
| 490 |
elif key == 'identity_type': |
| 491 |
params_override.append({"identity.type": kwargs[key]}) |
| 492 |
elif key == 'user_assigned_identities': |
| 493 |
identities = self._process_user_assigned_identities(kwargs[key]) |
| 494 |
params_override.append({"identity.user_assigned_identities": identities}) |
| 495 |
else: |
| 496 |
params_override.append({key: kwargs[key]}) |
| 497 |
return params_override |
| 498 |
|
| 499 |
|
| 500 |
def _process_user_assigned_identities(self, resource_ids): |
| 501 |
return [{"resource_id": resource_id} for resource_id in resource_ids] |
| 502 |
|
| 503 |
def get(self, name, as_dict=False): |
| 504 |
try: |
| 505 |
|
| 506 |
ml_compute_info = self.client.compute.get(name) |
| 507 |
if as_dict: |
| 508 |
ml_compute_info = self.entity_to_dict(ml_compute_info) |
| 509 |
except ResourceNotFoundError: |
| 510 |
ml_compute_info = None |
| 511 |
return ml_compute_info |
| 512 |
|
| 513 |
def start(self, name): |
| 514 |
try: |
| 515 |
response = self.client.compute.begin_start(name=name) |
| 516 |
result = self.get_poller_result(response) |
| 517 |
return result |
| 518 |
except Exception as e: |
| 519 |
self.fail("Error start the compute instance: {0}".format(str(e))) |
| 520 |
|
| 521 |
def stop(self, name): |
| 522 |
try: |
| 523 |
response = self.client.compute.begin_stop(name=name) |
| 524 |
result = self.get_poller_result(response) |
| 525 |
return result |
| 526 |
except Exception as e: |
| 527 |
self.fail("Error stop the compute instance: {0}".format(str(e))) |
| 528 |
|
| 529 |
def restart(self, name): |
| 530 |
try: |
| 531 |
response = self.client.compute.begin_restart(name=name) |
| 532 |
result = self.get_poller_result(response) |
| 533 |
return result |
| 534 |
except Exception as e: |
| 535 |
self.fail("Error restart the compute instance: {0}".format(str(e))) |
| 536 |
|
| 537 |
|
| 538 |
def main(): |
| 539 |
AzureRMMLCompute() |
| 540 |
|
| 541 |
|
| 542 |
if __name__ == '__main__': |
| 543 |
main() |