| 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_info |
| 11 |
version_added: "3.17.0" |
| 12 |
short_description: List ML Model resources |
| 13 |
description: |
| 14 |
- List ML Model resources. |
| 15 |
options: |
| 16 |
name: |
| 17 |
description: |
| 18 |
- Name of the Azure ML model. |
| 19 |
type: str |
| 20 |
required: false |
| 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 |
ml_registry: |
| 32 |
description: |
| 33 |
- If provided, the command will target the registry |
| 34 |
instead of a workspace. Hence resource group and |
| 35 |
workspace won't be required. Must be provided if |
| 36 |
ml_workspace and resource_group are not provided. |
| 37 |
required: false |
| 38 |
type: str |
| 39 |
version: |
| 40 |
description: |
| 41 |
- Version of the model. Must be provided, if label |
| 42 |
is not provided. Mutually exclusive with label. |
| 43 |
required: false |
| 44 |
type: str |
| 45 |
label: |
| 46 |
description: |
| 47 |
- Label of the model. Must be provided, if version |
| 48 |
is not provided. Mutually exclusive with version. |
| 49 |
required: false |
| 50 |
type: str |
| 51 |
stage: |
| 52 |
description: |
| 53 |
- Stage of the model. |
| 54 |
required: false |
| 55 |
type: str |
| 56 |
list_type: |
| 57 |
description: |
| 58 |
- Specify if you want active, archived or all models. |
| 59 |
type: str |
| 60 |
choices: |
| 61 |
- active |
| 62 |
- archived |
| 63 |
- all |
| 64 |
|
| 65 |
extends_documentation_fragment: |
| 66 |
- azure.azcollection.azure |
| 67 |
|
| 68 |
author: |
| 69 |
- Bill Peck (@p3ck) |
| 70 |
''' |
| 71 |
|
| 72 |
EXAMPLES = ''' |
| 73 |
- name: List ML Model by name |
| 74 |
azure.azcollection.azure_rm_ml_model_info: |
| 75 |
name: MyModel |
| 76 |
resource_group: myResourceGroup |
| 77 |
ml_workspace: myMLWorkspace |
| 78 |
|
| 79 |
- name: List Archived ML Models |
| 80 |
azure.azcollection.azure_rm_ml_model_info: |
| 81 |
resource_group: myResourceGroup |
| 82 |
ml_workspace: myMLWorkspace |
| 83 |
list_type: archived |
| 84 |
''' |
| 85 |
|
| 86 |
RETURN = ''' |
| 87 |
ml_models: |
| 88 |
description: |
| 89 |
- Models that match the query. |
| 90 |
returned: always |
| 91 |
type: dict |
| 92 |
sample: [ |
| 93 |
{ |
| 94 |
"creation_context": { |
| 95 |
"created_at": "2026-03-25T17:48:05.464195+00:00", |
| 96 |
"created_by": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 97 |
"created_by_type": "Application", |
| 98 |
"last_modified_at": "2026-03-25T17:48:12.822846+00:00", |
| 99 |
"last_modified_by": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 100 |
"last_modified_by_type": "Application" |
| 101 |
}, |
| 102 |
"description": "Model created from local file.", |
| 103 |
"id": "azureml:/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-model/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/models/modelxxxxxxxxxx/versions/2", |
| 104 |
"name": "modelxxxxxxxxxx", |
| 105 |
"path": "azureml://subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-model/workspaces/workspace-xxxxxxxxxx/datastores/workspaceblobstore/paths/LocalUpload/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/model.pkl", |
| 106 |
"properties": {}, |
| 107 |
"stage": "Production", |
| 108 |
"tags": {}, |
| 109 |
"type": "custom_model", |
| 110 |
"version": "2" |
| 111 |
} |
| 112 |
] |
| 113 |
''' |
| 114 |
|
| 115 |
|
| 116 |
try: |
| 117 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon |
| 118 |
from azure.core.exceptions import ResourceNotFoundError |
| 119 |
except ImportError: |
| 120 |
|
| 121 |
pass |
| 122 |
|
| 123 |
|
| 124 |
class AzureRMMLModelInfo(MLClientCommon): |
| 125 |
def __init__(self): |
| 126 |
|
| 127 |
self.module_arg_spec = dict( |
| 128 |
name=dict( |
| 129 |
type='str', |
| 130 |
required=False, |
| 131 |
), |
| 132 |
resource_group=dict( |
| 133 |
type='str', |
| 134 |
required=True, |
| 135 |
), |
| 136 |
ml_workspace=dict( |
| 137 |
type='str', |
| 138 |
required=True, |
| 139 |
), |
| 140 |
ml_registry=dict( |
| 141 |
type='str', |
| 142 |
), |
| 143 |
version=dict( |
| 144 |
type='str', |
| 145 |
), |
| 146 |
label=dict( |
| 147 |
type='str', |
| 148 |
), |
| 149 |
stage=dict( |
| 150 |
type='str', |
| 151 |
), |
| 152 |
list_type=dict( |
| 153 |
type='str', |
| 154 |
choices=['archived', 'active', 'all'], |
| 155 |
), |
| 156 |
) |
| 157 |
|
| 158 |
self._client = None |
| 159 |
|
| 160 |
self.results = dict( |
| 161 |
ml_models=[] |
| 162 |
) |
| 163 |
|
| 164 |
required_one_of = [('ml_workspace', 'ml_registry')] |
| 165 |
mutually_exclusive = [('version', 'label'), |
| 166 |
('ml_workspace', 'ml_registry'), |
| 167 |
('name', 'list_type'), |
| 168 |
('name', 'stage')] |
| 169 |
required_together = [('ml_workspace', 'resource_group')] |
| 170 |
|
| 171 |
super(AzureRMMLModelInfo, self).__init__(self.module_arg_spec, |
| 172 |
supports_tags=False, |
| 173 |
supports_check_mode=True, |
| 174 |
facts_module=True, |
| 175 |
required_one_of=required_one_of, |
| 176 |
mutually_exclusive=mutually_exclusive, |
| 177 |
required_together=required_together |
| 178 |
) |
| 179 |
|
| 180 |
def exec_module(self, **kwargs): |
| 181 |
|
| 182 |
for key in list(self.module_arg_spec.keys()): |
| 183 |
setattr(self, key, kwargs[key]) |
| 184 |
|
| 185 |
if self.name: |
| 186 |
try: |
| 187 |
result = self.client.models.get(name=self.name, |
| 188 |
version=self.version, |
| 189 |
label=self.label) |
| 190 |
ml_models = [self.entity_to_dict(result)] |
| 191 |
except ResourceNotFoundError: |
| 192 |
ml_models = [] |
| 193 |
else: |
| 194 |
list_view_type = self.get_list_view_type(self.list_type) |
| 195 |
results = self.client.models.list(name=self.name, |
| 196 |
stage=self.stage, |
| 197 |
list_view_type=list_view_type) |
| 198 |
ml_models = [self.entity_to_dict(self.client.models.get(x.name, version=x.latest_version)) for x in results] |
| 199 |
|
| 200 |
self.results['ml_models'] = ml_models |
| 201 |
return self.results |
| 202 |
|
| 203 |
|
| 204 |
def main(): |
| 205 |
AzureRMMLModelInfo() |
| 206 |
|
| 207 |
|
| 208 |
if __name__ == '__main__': |
| 209 |
main() |