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