| 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_model |
| 11 |
version_added: "3.17.0" |
| 12 |
short_description: Create, Archive or Restore an Azure Machine Learning Model |
| 13 |
description: |
| 14 |
- Create, Archive or Restore an Azure Machine Learning Model. |
| 15 |
options: |
| 16 |
name: |
| 17 |
description: |
| 18 |
- Name of the Azure ML model. |
| 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 |
- Model definition in YAML |
| 39 |
type: str |
| 40 |
required: false |
| 41 |
version: |
| 42 |
description: |
| 43 |
- Data Asset version. |
| 44 |
type: str |
| 45 |
path: |
| 46 |
description: |
| 47 |
- Path to the model file(s). This can be either a local |
| 48 |
or a remote location. If specified, name and version |
| 49 |
must also be provided. |
| 50 |
type: str |
| 51 |
stage: |
| 52 |
description: |
| 53 |
- Stage of the model. |
| 54 |
type: str |
| 55 |
choices: |
| 56 |
- Staging |
| 57 |
- Production |
| 58 |
- Archived |
| 59 |
- Logged |
| 60 |
- Development |
| 61 |
- Deprecated |
| 62 |
- Retired |
| 63 |
datastore: |
| 64 |
description: |
| 65 |
- The datastore to upload the local artifact to. |
| 66 |
type: str |
| 67 |
description: |
| 68 |
description: |
| 69 |
- Description of the model. |
| 70 |
type: str |
| 71 |
type: |
| 72 |
description: |
| 73 |
- Type of the model. |
| 74 |
default: custom_model |
| 75 |
type: str |
| 76 |
choices: |
| 77 |
- custom_model |
| 78 |
- mlflow_model |
| 79 |
- triton_model |
| 80 |
state: |
| 81 |
description: |
| 82 |
- State of the Model. Use C(present) to create |
| 83 |
or update. C(archive) to archive, C(restore) to |
| 84 |
restore. |
| 85 |
default: present |
| 86 |
type: str |
| 87 |
choices: |
| 88 |
- present |
| 89 |
- archive |
| 90 |
- restore |
| 91 |
|
| 92 |
extends_documentation_fragment: |
| 93 |
- azure.azcollection.azure |
| 94 |
- azure.azcollection.azure_tags |
| 95 |
|
| 96 |
author: |
| 97 |
- Bill Peck (@p3ck) |
| 98 |
''' |
| 99 |
|
| 100 |
EXAMPLES = ''' |
| 101 |
- name: Create ML Model |
| 102 |
azure.azcollection.azure_rm_ml_model: |
| 103 |
name: modelxxxxxxxxxx |
| 104 |
resource_group: xxxxx-ml-model |
| 105 |
ml_workspace: workspace-xxxxxxxxxx |
| 106 |
version: 2 |
| 107 |
stage: Production |
| 108 |
resource_definition: | |
| 109 |
$schema: https://azuremlschemas.azureedge.net/latest/model.schema.json |
| 110 |
path: ./files/mlflow-model/model.pkl |
| 111 |
description: Model created from local file. |
| 112 |
''' |
| 113 |
|
| 114 |
RETURN = ''' |
| 115 |
changed: |
| 116 |
description: |
| 117 |
- Whether the resource is changed. |
| 118 |
returned: always |
| 119 |
type: bool |
| 120 |
sample: false |
| 121 |
ml_model: |
| 122 |
description: |
| 123 |
- Model that was just created or updated. |
| 124 |
returned: always |
| 125 |
type: dict |
| 126 |
sample: { |
| 127 |
"creation_context": { |
| 128 |
"created_at": "2026-03-25T17:48:05.464195+00:00", |
| 129 |
"created_by": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 130 |
"created_by_type": "Application", |
| 131 |
"last_modified_at": "2026-03-25T17:48:12.822846+00:00", |
| 132 |
"last_modified_by": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 133 |
"last_modified_by_type": "Application" |
| 134 |
}, |
| 135 |
"description": "Model created from local file.", |
| 136 |
"id": "azureml:/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-model/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/models/modelxxxxxxxxxx/versions/2", |
| 137 |
"name": "modelxxxxxxxxxx", |
| 138 |
"path": "azureml://subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-model/workspaces/workspace-xxxxxxxxxx/datastores/workspaceblobstore/paths/LocalUpload/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/model.pkl", |
| 139 |
"properties": {}, |
| 140 |
"stage": "Production", |
| 141 |
"tags": {}, |
| 142 |
"type": "custom_model", |
| 143 |
"version": "2" |
| 144 |
} |
| 145 |
''' |
| 146 |
|
| 147 |
|
| 148 |
try: |
| 149 |
import io |
| 150 |
import time |
| 151 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon |
| 152 |
from azure.core.exceptions import ResourceNotFoundError |
| 153 |
from azure.ai.ml.entities._load_functions import load_model |
| 154 |
from azure.ai.ml.entities._assets import Model |
| 155 |
except ImportError: |
| 156 |
|
| 157 |
pass |
| 158 |
|
| 159 |
|
| 160 |
class AzureRMMLModel(MLClientCommon): |
| 161 |
|
| 162 |
UPDATE_OVERRIDES = ['stage'] |
| 163 |
|
| 164 |
PARAM_OVERRIDES = UPDATE_OVERRIDES + ['name', |
| 165 |
'version', |
| 166 |
'path', |
| 167 |
'type', |
| 168 |
'description', |
| 169 |
'tags', |
| 170 |
'datastore'] |
| 171 |
|
| 172 |
def __init__(self): |
| 173 |
|
| 174 |
self.module_arg_spec = dict( |
| 175 |
name=dict( |
| 176 |
type='str', |
| 177 |
required=True, |
| 178 |
), |
| 179 |
resource_definition=dict( |
| 180 |
type='str', |
| 181 |
required=False, |
| 182 |
), |
| 183 |
resource_group=dict( |
| 184 |
type='str', |
| 185 |
required=False, |
| 186 |
), |
| 187 |
ml_workspace=dict( |
| 188 |
type='str', |
| 189 |
required=False, |
| 190 |
), |
| 191 |
ml_registry=dict( |
| 192 |
type='str', |
| 193 |
required=False, |
| 194 |
), |
| 195 |
version=dict( |
| 196 |
type='str', |
| 197 |
required=False, |
| 198 |
), |
| 199 |
path=dict( |
| 200 |
type='str', |
| 201 |
required=False, |
| 202 |
), |
| 203 |
stage=dict( |
| 204 |
type='str', |
| 205 |
required=False, |
| 206 |
choices=['Staging', 'Production', 'Archived', |
| 207 |
'Logged', 'Development', 'Deprecated', 'Retired'], |
| 208 |
), |
| 209 |
datastore=dict( |
| 210 |
type='str', |
| 211 |
required=False, |
| 212 |
), |
| 213 |
description=dict( |
| 214 |
type='str', |
| 215 |
required=False, |
| 216 |
), |
| 217 |
type=dict( |
| 218 |
type='str', |
| 219 |
default='custom_model', |
| 220 |
choices=['custom_model', 'mlflow_model', 'triton_model'], |
| 221 |
), |
| 222 |
state=dict( |
| 223 |
type='str', |
| 224 |
default='present', |
| 225 |
choices=['present', 'archive', 'restore'], |
| 226 |
), |
| 227 |
) |
| 228 |
|
| 229 |
self._client = None |
| 230 |
self.ml_registry = None |
| 231 |
self.state = None |
| 232 |
|
| 233 |
self.results = dict( |
| 234 |
changed=False, |
| 235 |
compare=[], |
| 236 |
ml_model={} |
| 237 |
) |
| 238 |
|
| 239 |
required_one_of = [('ml_workspace', 'ml_registry')] |
| 240 |
mutually_exclusive = [('ml_workspace', 'ml_registry')] |
| 241 |
required_together = [('ml_workspace', 'resource_group')] |
| 242 |
|
| 243 |
super(AzureRMMLModel, self).__init__(self.module_arg_spec, |
| 244 |
supports_check_mode=True, |
| 245 |
required_one_of=required_one_of, |
| 246 |
mutually_exclusive=mutually_exclusive, |
| 247 |
required_together=required_together, |
| 248 |
) |
| 249 |
|
| 250 |
def exec_module(self, **kwargs): |
| 251 |
|
| 252 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 253 |
setattr(self, key, kwargs[key]) |
| 254 |
|
| 255 |
changed = False |
| 256 |
|
| 257 |
if self.state == 'present': |
| 258 |
ml_model_info = self.get(name=self.name, |
| 259 |
version=self.version) |
| 260 |
|
| 261 |
if ml_model_info: |
| 262 |
|
| 263 |
params_override = self.update_params(kwargs, |
| 264 |
self.UPDATE_OVERRIDES, |
| 265 |
ml_model_info) |
| 266 |
|
| 267 |
|
| 268 |
ml_model = Model._load(data=[], |
| 269 |
params_override=params_override) |
| 270 |
|
| 271 |
ml_model = load_model(None, params_override=params_override) |
| 272 |
ml_model_info = self.entity_to_dict(ml_model_info) |
| 273 |
changed = not self.default_compare({}, |
| 274 |
self.entity_to_dict(ml_model), |
| 275 |
ml_model_info, |
| 276 |
'', |
| 277 |
self.results) |
| 278 |
if changed and not self.check_mode: |
| 279 |
response = self.client.create_or_update(ml_model) |
| 280 |
ml_model_info = self.entity_to_dict(response) |
| 281 |
else: |
| 282 |
|
| 283 |
params_override = self.update_params(kwargs, self.PARAM_OVERRIDES) |
| 284 |
|
| 285 |
|
| 286 |
if self.resource_definition: |
| 287 |
resource_definition = io.StringIO(self.resource_definition) |
| 288 |
ml_model = load_model(source=resource_definition, |
| 289 |
params_override=params_override) |
| 290 |
else: |
| 291 |
ml_model = Model( |
| 292 |
name=self.name, |
| 293 |
version=self.version, |
| 294 |
path=self.path, |
| 295 |
description=self.description, |
| 296 |
tags=self.tags, |
| 297 |
stage=self.stage, |
| 298 |
type=self.type, |
| 299 |
datastore=self.datastore, |
| 300 |
) |
| 301 |
|
| 302 |
changed = True |
| 303 |
if not self.check_mode: |
| 304 |
response = self.client.create_or_update(ml_model) |
| 305 |
ml_model_info = self.entity_to_dict(response) |
| 306 |
elif self.state == 'archive': |
| 307 |
|
| 308 |
list_view_type = self.get_list_view_type('active') |
| 309 |
results = self.client.models.list(list_view_type=list_view_type) |
| 310 |
ml_models = [x.name for x in results] |
| 311 |
|
| 312 |
if self.name in ml_models: |
| 313 |
changed = True |
| 314 |
if not self.check_mode: |
| 315 |
self.archive(name=self.name) |
| 316 |
ml_model_info = None |
| 317 |
elif self.state == 'restore': |
| 318 |
|
| 319 |
list_view_type = self.get_list_view_type('archived') |
| 320 |
results = self.client.models.list(list_view_type=list_view_type) |
| 321 |
ml_models = [x.name for x in results] |
| 322 |
|
| 323 |
if self.name in ml_models: |
| 324 |
changed = True |
| 325 |
if not self.check_mode: |
| 326 |
self.restore(name=self.name) |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
time.sleep(1) |
| 331 |
ml_model_info = self.get(name=self.name, |
| 332 |
label='latest', |
| 333 |
as_dict=True) |
| 334 |
|
| 335 |
self.results['ml_model'] = ml_model_info |
| 336 |
self.results['changed'] = changed |
| 337 |
return self.results |
| 338 |
|
| 339 |
def update_params(self, kwargs, param_overrides, ml_model=None): |
| 340 |
params_override = [] |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
if ml_model: |
| 346 |
items = self.entity_to_dict(ml_model).items() |
| 347 |
params_override = [{k: v} for k, v in items] |
| 348 |
|
| 349 |
|
| 350 |
for key in list(self.module_arg_spec.keys()): |
| 351 |
if kwargs[key] is not None and key in param_overrides: |
| 352 |
params_override.append({key: kwargs[key]}) |
| 353 |
return params_override |
| 354 |
|
| 355 |
def get(self, name, version=None, label=None, as_dict=False): |
| 356 |
try: |
| 357 |
|
| 358 |
ml_model_info = self.client.models.get(name=name, |
| 359 |
version=version, |
| 360 |
label=label) |
| 361 |
if as_dict: |
| 362 |
ml_model_info = self.entity_to_dict(ml_model_info) |
| 363 |
except ResourceNotFoundError: |
| 364 |
ml_model_info = None |
| 365 |
return ml_model_info |
| 366 |
|
| 367 |
def archive(self, name=None): |
| 368 |
try: |
| 369 |
self.client.models.archive(name=name) |
| 370 |
except Exception as e: |
| 371 |
self.fail("Error archive the model instance: {0}".format(str(e))) |
| 372 |
|
| 373 |
def restore(self, name=None): |
| 374 |
try: |
| 375 |
self.client.models.restore(name=name) |
| 376 |
except Exception as e: |
| 377 |
self.fail("Error restore the model instance: {0}".format(str(e))) |
| 378 |
|
| 379 |
|
| 380 |
def main(): |
| 381 |
AzureRMMLModel() |
| 382 |
|
| 383 |
|
| 384 |
if __name__ == '__main__': |
| 385 |
main() |