| 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_environment |
| 11 |
version_added: "3.16.0" |
| 12 |
short_description: Create, Archive or Restore an Azure Machine Learning Environment |
| 13 |
description: |
| 14 |
- Create, Archive or Restore an Azure Machine Learning Environment. |
| 15 |
options: |
| 16 |
name: |
| 17 |
description: |
| 18 |
- Name of the Azure ML environment. |
| 19 |
type: str |
| 20 |
required: true |
| 21 |
resource_group: |
| 22 |
description: |
| 23 |
- Name of resource group. |
| 24 |
required: false |
| 25 |
type: str |
| 26 |
ml_workspace: |
| 27 |
description: |
| 28 |
- Name of the Azure ML workspace. |
| 29 |
required: false |
| 30 |
type: str |
| 31 |
ml_registry: |
| 32 |
description: |
| 33 |
- Name of the Azure ML registry. |
| 34 |
required: false |
| 35 |
type: str |
| 36 |
resource_definition: |
| 37 |
description: |
| 38 |
- Environment definition in YAML |
| 39 |
type: str |
| 40 |
required: false |
| 41 |
version: |
| 42 |
description: |
| 43 |
- Data Asset version. |
| 44 |
type: str |
| 45 |
build_context: |
| 46 |
description: |
| 47 |
- Local path to the directory to use as a Docker build context. |
| 48 |
C(build_context) and C(image) are mutually exclusive arguments. |
| 49 |
type: str |
| 50 |
conda_file: |
| 51 |
description: |
| 52 |
- Local path to a conda specification file. C(image) must |
| 53 |
also be specified if this argument is used. |
| 54 |
type: str |
| 55 |
datastore: |
| 56 |
description: |
| 57 |
- The datastore to upload the local artifact to. |
| 58 |
type: str |
| 59 |
description: |
| 60 |
description: |
| 61 |
- Description of the environment. |
| 62 |
type: str |
| 63 |
dockerfile_path: |
| 64 |
description: |
| 65 |
- Relative path to the Dockerfile within the directory specified by |
| 66 |
C(build_context). If omitted, './Dockerfile' is used. |
| 67 |
default: ./Dockerfile |
| 68 |
type: str |
| 69 |
image: |
| 70 |
description: |
| 71 |
- Docker image. C(image) and C(build_context) are mutually |
| 72 |
exclusive arguments. |
| 73 |
type: str |
| 74 |
os_type: |
| 75 |
description: |
| 76 |
- Type of operating system. |
| 77 |
default: linux |
| 78 |
type: str |
| 79 |
choices: |
| 80 |
- linux |
| 81 |
- windows |
| 82 |
state: |
| 83 |
description: |
| 84 |
- State of the Environment. Use C(present) to create |
| 85 |
or update. C(archive) to archive, C(restore) to |
| 86 |
restore. |
| 87 |
default: present |
| 88 |
type: str |
| 89 |
choices: |
| 90 |
- present |
| 91 |
- archive |
| 92 |
- restore |
| 93 |
|
| 94 |
extends_documentation_fragment: |
| 95 |
- azure.azcollection.azure |
| 96 |
- azure.azcollection.azure_tags |
| 97 |
|
| 98 |
author: |
| 99 |
- Bill Peck (@p3ck) |
| 100 |
''' |
| 101 |
|
| 102 |
EXAMPLES = ''' |
| 103 |
- name: Create ML Environment |
| 104 |
azure.azcollection.azure_rm_ml_environment: |
| 105 |
name: environmentxxxxxxxxxx |
| 106 |
resource_group: xxxxx-ml-environment |
| 107 |
ml_workspace: workspace-xxxxxxxxxx |
| 108 |
version: 2 |
| 109 |
resource_definition: | |
| 110 |
$schema: https://azuremlschemas.azureedge.net/latest/environment.schema.json |
| 111 |
image: mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04:20220303.v1 |
| 112 |
conda_file: ./files/python-aml-rai.yaml |
| 113 |
''' |
| 114 |
|
| 115 |
RETURN = ''' |
| 116 |
changed: |
| 117 |
description: |
| 118 |
- Whether the resource is changed. |
| 119 |
returned: always |
| 120 |
type: bool |
| 121 |
sample: false |
| 122 |
ml_environment: |
| 123 |
description: |
| 124 |
- Environment that was just created or updated. |
| 125 |
returned: always |
| 126 |
type: dict |
| 127 |
sample: { |
| 128 |
"conda_file": { |
| 129 |
"channels": [ |
| 130 |
"conda-forge" |
| 131 |
], |
| 132 |
"dependencies": [ |
| 133 |
"python=3.8", |
| 134 |
"pip=20.0.2", |
| 135 |
{ |
| 136 |
"pip": [ |
| 137 |
"numpy", |
| 138 |
"pandas", |
| 139 |
"pyarrow", |
| 140 |
"scikit-learn", |
| 141 |
"mlflow", |
| 142 |
"azureml-mlflow", |
| 143 |
"responsibleai~=0.17.0", |
| 144 |
"raiwidgets~=0.17.0", |
| 145 |
"markupsafe<=2.0.1", |
| 146 |
"itsdangerous==2.0.1", |
| 147 |
"azureml-dataset-runtime", |
| 148 |
"azureml-core" |
| 149 |
] |
| 150 |
} |
| 151 |
], |
| 152 |
"name": "aml-rai-environment" |
| 153 |
}, |
| 154 |
"creation_context": { |
| 155 |
"created_at": "2026-03-20T17:34:28.790877+00:00", |
| 156 |
"created_by": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 157 |
"created_by_type": "Application", |
| 158 |
"last_modified_at": "2026-03-20T17:34:28.790877+00:00", |
| 159 |
"last_modified_by": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 160 |
"last_modified_by_type": "Application" |
| 161 |
}, |
| 162 |
"id": "azureml:/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-environment/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/environments/environmentxxxxxxxxxx/versions/2", |
| 163 |
"image": "mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04:20220303.v1", |
| 164 |
"name": "environmentxxxxxxxxxx", |
| 165 |
"os_type": "linux", |
| 166 |
"tags": {}, |
| 167 |
"version": "2" |
| 168 |
} |
| 169 |
''' |
| 170 |
|
| 171 |
|
| 172 |
try: |
| 173 |
import io |
| 174 |
import time |
| 175 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon |
| 176 |
from azure.core.exceptions import ResourceNotFoundError |
| 177 |
from azure.ai.ml.entities._load_functions import load_environment |
| 178 |
except ImportError: |
| 179 |
|
| 180 |
pass |
| 181 |
|
| 182 |
|
| 183 |
class AzureRMMLEnvironment(MLClientCommon): |
| 184 |
|
| 185 |
UPDATE_OVERRIDES = [] |
| 186 |
|
| 187 |
PARAM_OVERRIDES = UPDATE_OVERRIDES + ['name', |
| 188 |
'version', |
| 189 |
'tags', |
| 190 |
'description', |
| 191 |
'image', |
| 192 |
'conda_file', |
| 193 |
'build_context', |
| 194 |
'os_type', |
| 195 |
'datastore'] |
| 196 |
|
| 197 |
def __init__(self): |
| 198 |
|
| 199 |
self.module_arg_spec = dict( |
| 200 |
name=dict( |
| 201 |
type='str', |
| 202 |
required=True, |
| 203 |
), |
| 204 |
resource_definition=dict( |
| 205 |
type='str', |
| 206 |
required=False, |
| 207 |
), |
| 208 |
resource_group=dict( |
| 209 |
type='str', |
| 210 |
required=False, |
| 211 |
), |
| 212 |
ml_workspace=dict( |
| 213 |
type='str', |
| 214 |
required=False, |
| 215 |
), |
| 216 |
ml_registry=dict( |
| 217 |
type='str', |
| 218 |
required=False, |
| 219 |
), |
| 220 |
version=dict( |
| 221 |
type='str', |
| 222 |
required=False, |
| 223 |
), |
| 224 |
build_context=dict( |
| 225 |
type='str', |
| 226 |
required=False, |
| 227 |
), |
| 228 |
conda_file=dict( |
| 229 |
type='str', |
| 230 |
required=False, |
| 231 |
), |
| 232 |
datastore=dict( |
| 233 |
type='str', |
| 234 |
required=False, |
| 235 |
), |
| 236 |
description=dict( |
| 237 |
type='str', |
| 238 |
required=False, |
| 239 |
), |
| 240 |
dockerfile_path=dict( |
| 241 |
type='str', |
| 242 |
default='./Dockerfile', |
| 243 |
), |
| 244 |
image=dict( |
| 245 |
type='str', |
| 246 |
required=False, |
| 247 |
), |
| 248 |
os_type=dict( |
| 249 |
type='str', |
| 250 |
default='linux', |
| 251 |
choices=['linux', 'windows'], |
| 252 |
), |
| 253 |
state=dict( |
| 254 |
type='str', |
| 255 |
default='present', |
| 256 |
choices=['present', 'archive', 'restore'], |
| 257 |
), |
| 258 |
) |
| 259 |
|
| 260 |
self._client = None |
| 261 |
self.ml_registry = None |
| 262 |
self.state = None |
| 263 |
|
| 264 |
self.results = dict( |
| 265 |
changed=False, |
| 266 |
compare=[], |
| 267 |
ml_environment={} |
| 268 |
) |
| 269 |
|
| 270 |
required_if = [ |
| 271 |
('state', 'present', ['resource_definition']) |
| 272 |
] |
| 273 |
required_one_of = [('ml_workspace', 'ml_registry')] |
| 274 |
mutually_exclusive = [('build_context', 'image'), |
| 275 |
('ml_workspace', 'ml_registry')] |
| 276 |
required_together = [('ml_workspace', 'resource_group'), |
| 277 |
('conda_file', 'image')] |
| 278 |
|
| 279 |
super(AzureRMMLEnvironment, self).__init__(self.module_arg_spec, |
| 280 |
supports_check_mode=True, |
| 281 |
required_one_of=required_one_of, |
| 282 |
mutually_exclusive=mutually_exclusive, |
| 283 |
required_together=required_together, |
| 284 |
required_if=required_if, |
| 285 |
) |
| 286 |
|
| 287 |
def exec_module(self, **kwargs): |
| 288 |
|
| 289 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 290 |
setattr(self, key, kwargs[key]) |
| 291 |
|
| 292 |
if self.resource_definition: |
| 293 |
resource_definition = io.StringIO(self.resource_definition) |
| 294 |
else: |
| 295 |
resource_definition = None |
| 296 |
|
| 297 |
changed = False |
| 298 |
|
| 299 |
if self.state == 'present': |
| 300 |
ml_environment_info = self.get(name=self.name, |
| 301 |
version=self.version) |
| 302 |
|
| 303 |
if ml_environment_info: |
| 304 |
|
| 305 |
|
| 306 |
changed = False |
| 307 |
ml_environment_info = self.entity_to_dict(ml_environment_info) |
| 308 |
else: |
| 309 |
|
| 310 |
params_override = self.update_params(kwargs, self.PARAM_OVERRIDES) |
| 311 |
|
| 312 |
|
| 313 |
ml_environment = load_environment(source=resource_definition, |
| 314 |
params_override=params_override) |
| 315 |
changed = True |
| 316 |
if not self.check_mode: |
| 317 |
response = self.client.create_or_update(ml_environment) |
| 318 |
ml_environment_info = self.entity_to_dict(response) |
| 319 |
elif self.state == 'archive': |
| 320 |
|
| 321 |
list_view_type = self.get_list_view_type('active') |
| 322 |
results = self.client.environments.list(list_view_type=list_view_type) |
| 323 |
ml_environments = [x.name for x in results] |
| 324 |
|
| 325 |
if self.name in ml_environments: |
| 326 |
changed = True |
| 327 |
if not self.check_mode: |
| 328 |
self.archive(name=self.name) |
| 329 |
ml_environment_info = None |
| 330 |
elif self.state == 'restore': |
| 331 |
|
| 332 |
list_view_type = self.get_list_view_type('archived') |
| 333 |
results = self.client.environments.list(list_view_type=list_view_type) |
| 334 |
ml_environments = [x.name for x in results] |
| 335 |
|
| 336 |
if self.name in ml_environments: |
| 337 |
changed = True |
| 338 |
if not self.check_mode: |
| 339 |
self.restore(name=self.name) |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
time.sleep(1) |
| 344 |
ml_environment_info = self.get(name=self.name, |
| 345 |
label='latest', |
| 346 |
as_dict=True) |
| 347 |
|
| 348 |
self.results['ml_environment'] = ml_environment_info |
| 349 |
self.results['changed'] = changed |
| 350 |
return self.results |
| 351 |
|
| 352 |
def update_params(self, kwargs, param_overrides, ml_environment=None): |
| 353 |
params_override = [] |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
if ml_environment: |
| 359 |
items = self.entity_to_dict(ml_environment).items() |
| 360 |
params_override = [{k: v} for k, v in items] |
| 361 |
|
| 362 |
|
| 363 |
for key in list(self.module_arg_spec.keys()): |
| 364 |
if kwargs[key] is not None and key in param_overrides: |
| 365 |
if key == 'build_context:': |
| 366 |
build = {"path": self.build_context, |
| 367 |
"dockerfile_path": self.dockerfile_path} |
| 368 |
params_override.append({"build": build}) |
| 369 |
else: |
| 370 |
params_override.append({key: kwargs[key]}) |
| 371 |
return params_override |
| 372 |
|
| 373 |
def get(self, name, version=None, label=None, as_dict=False): |
| 374 |
try: |
| 375 |
|
| 376 |
ml_environment_info = self.client.environments.get(name=name, |
| 377 |
version=version, |
| 378 |
label=label) |
| 379 |
if as_dict: |
| 380 |
ml_environment_info = self.entity_to_dict(ml_environment_info) |
| 381 |
except ResourceNotFoundError: |
| 382 |
ml_environment_info = None |
| 383 |
return ml_environment_info |
| 384 |
|
| 385 |
def archive(self, name=None): |
| 386 |
try: |
| 387 |
self.client.environments.archive(name=name) |
| 388 |
except Exception as e: |
| 389 |
self.fail("Error archive the environment instance: {0}".format(str(e))) |
| 390 |
|
| 391 |
def restore(self, name=None): |
| 392 |
try: |
| 393 |
self.client.environments.restore(name=name) |
| 394 |
except Exception as e: |
| 395 |
self.fail("Error restore the environment instance: {0}".format(str(e))) |
| 396 |
|
| 397 |
|
| 398 |
def main(): |
| 399 |
AzureRMMLEnvironment() |
| 400 |
|
| 401 |
|
| 402 |
if __name__ == '__main__': |
| 403 |
main() |