| 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_online_deployment |
| 11 |
version_added: "3.17.0" |
| 12 |
short_description: Create, Delete for an Azure Machine Learning Online Deployment |
| 13 |
description: |
| 14 |
- Create, Delete for an Azure Machine Learning Online Deployment. |
| 15 |
options: |
| 16 |
name: |
| 17 |
description: |
| 18 |
- Name of the Azure ML online_deployment. |
| 19 |
type: str |
| 20 |
required: false |
| 21 |
endpoint_name: |
| 22 |
description: |
| 23 |
- Name of the online endpoint. If not specified here will need to |
| 24 |
be referenced in the I(resource_definition). |
| 25 |
type: str |
| 26 |
required: false |
| 27 |
resource_group: |
| 28 |
description: |
| 29 |
- Name of resource group. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
ml_workspace: |
| 33 |
description: |
| 34 |
- Name of the Azure ML workspace. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
resource_definition: |
| 38 |
description: |
| 39 |
- Online Deployment definition in YAML |
| 40 |
type: str |
| 41 |
required: false |
| 42 |
description: |
| 43 |
description: |
| 44 |
- Description of the online_deployment. |
| 45 |
type: str |
| 46 |
local: |
| 47 |
description: |
| 48 |
- Update local deployment. |
| 49 |
type: bool |
| 50 |
default: false |
| 51 |
local_enable_gpu: |
| 52 |
description: |
| 53 |
- Enable GPU for local deployment. |
| 54 |
type: bool |
| 55 |
default: false |
| 56 |
package_model: |
| 57 |
description: |
| 58 |
- Create packaged environment from the deployment yaml |
| 59 |
and use the packaged environment for the deployment. |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
all_traffic: |
| 63 |
description: |
| 64 |
- Sets endpoint traffic 100% to this deployment |
| 65 |
after successful creation. |
| 66 |
type: bool |
| 67 |
default: false |
| 68 |
state: |
| 69 |
description: |
| 70 |
- State of the Online Deployment. Use C(present) to create |
| 71 |
or update. |
| 72 |
default: present |
| 73 |
type: str |
| 74 |
choices: |
| 75 |
- present |
| 76 |
- absent |
| 77 |
|
| 78 |
extends_documentation_fragment: |
| 79 |
- azure.azcollection.azure |
| 80 |
- azure.azcollection.azure_tags |
| 81 |
|
| 82 |
author: |
| 83 |
- Bill Peck (@p3ck) |
| 84 |
''' |
| 85 |
|
| 86 |
EXAMPLES = ''' |
| 87 |
- name: Create ML Online Deployment |
| 88 |
azure.azcollection.azure_rm_ml_online_deployment: |
| 89 |
endpoint_name: endpoint-command-ansible |
| 90 |
resource_group: xxxxx-ml-online_deployment |
| 91 |
ml_workspace: workspace-xxxxxxxxxx |
| 92 |
resource_definition: | |
| 93 |
$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineDeployment.schema.json |
| 94 |
name: {{ online_deployment_name }} |
| 95 |
model: |
| 96 |
path: ./targets/azure_rm_ml_online_deployment/files/model/ |
| 97 |
code_configuration: |
| 98 |
code: ./targets/azure_rm_ml_online_deployment/files/onlinescoring/ |
| 99 |
scoring_script: score.py |
| 100 |
environment: |
| 101 |
conda_file: ./targets/azure_rm_ml_online_deployment/files/environment/conda.yaml |
| 102 |
image: mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu22.04:latest |
| 103 |
instance_type: Standard_DS3_v2 |
| 104 |
instance_count: 1 |
| 105 |
''' |
| 106 |
|
| 107 |
RETURN = ''' |
| 108 |
changed: |
| 109 |
description: |
| 110 |
- Whether the resource is changed. |
| 111 |
returned: always |
| 112 |
type: bool |
| 113 |
sample: false |
| 114 |
ml_online_deployment: |
| 115 |
description: |
| 116 |
- Online Deployment that was just created or updated. |
| 117 |
returned: always |
| 118 |
type: dict |
| 119 |
sample: { |
| 120 |
"app_insights_enabled": false, |
| 121 |
"code_configuration": { |
| 122 |
"code": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-online_deployment/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/codes/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/versions/1", |
| 123 |
"scoring_script": "score.py" |
| 124 |
}, |
| 125 |
"creation_context": { |
| 126 |
"created_at": "2026-04-17T20:39:38.864149+00:00", |
| 127 |
"created_by": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 128 |
"last_modified_at": "2026-04-17T20:39:38.864151+00:00" |
| 129 |
}, |
| 130 |
"egress_public_network_access": "enabled", |
| 131 |
"endpoint_name": "endpoint-command-ansible", |
| 132 |
"environment": "azureml:/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-online_deployment/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/environments/CliV2AnonymousEnvironment/versions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 133 |
"environment_variables": { |
| 134 |
"AML_APP_ROOT": "/var/azureml-app/onlinescoring", |
| 135 |
"AZUREML_ENTRY_SCRIPT": "score.py", |
| 136 |
"AZUREML_MODEL_DIR": "/var/azureml-app/azureml-models/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/1" |
| 137 |
}, |
| 138 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-online_deployment/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/onlineEndpoints/endpoint-command-ansible/deployments/deployment-command-xxxxxxxxxx", |
| 139 |
"instance_count": 1, |
| 140 |
"instance_type": "Standard_DS3_v2", |
| 141 |
"liveness_probe": { |
| 142 |
"failure_threshold": 30, |
| 143 |
"initial_delay": 10, |
| 144 |
"period": 10, |
| 145 |
"success_threshold": 1, |
| 146 |
"timeout": 2 |
| 147 |
}, |
| 148 |
"model": "azureml:/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-online_deployment/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/models/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/versions/1", |
| 149 |
"name": "deployment-command-e76e189747", |
| 150 |
"properties": { |
| 151 |
"AzureAsyncOperationUri": "https://management.azure.com/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.MachineLearningServices/locations/eastus/mfeOperationsStatus/odidp:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx?api-version=2023-04-01-preview" |
| 152 |
}, |
| 153 |
"provisioning_state": "Succeeded", |
| 154 |
"readiness_probe": { |
| 155 |
"failure_threshold": 30, |
| 156 |
"initial_delay": 10, |
| 157 |
"period": 10, |
| 158 |
"success_threshold": 1, |
| 159 |
"timeout": 2 |
| 160 |
}, |
| 161 |
"request_settings": { |
| 162 |
"max_concurrent_requests_per_instance": 1, |
| 163 |
"request_timeout_ms": 5000 |
| 164 |
}, |
| 165 |
"scale_settings": { |
| 166 |
"type": "default" |
| 167 |
}, |
| 168 |
"tags": {}, |
| 169 |
"type": "managed" |
| 170 |
} |
| 171 |
''' |
| 172 |
|
| 173 |
|
| 174 |
try: |
| 175 |
import io |
| 176 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon |
| 177 |
from azure.core.exceptions import ResourceNotFoundError |
| 178 |
from azure.ai.ml.entities._load_functions import load_online_deployment |
| 179 |
from azure.ai.ml.constants._common import REGISTRY_URI_FORMAT |
| 180 |
except ImportError: |
| 181 |
|
| 182 |
pass |
| 183 |
|
| 184 |
|
| 185 |
class AzureRMMLOnlineDeployment(MLClientCommon): |
| 186 |
|
| 187 |
UPDATE_OVERRIDES = ['description', |
| 188 |
'tags', |
| 189 |
'endpoint_name'] |
| 190 |
|
| 191 |
PARAM_OVERRIDES = UPDATE_OVERRIDES + ['name'] |
| 192 |
|
| 193 |
def __init__(self): |
| 194 |
|
| 195 |
self.module_arg_spec = dict( |
| 196 |
name=dict( |
| 197 |
type='str', |
| 198 |
required=False, |
| 199 |
), |
| 200 |
endpoint_name=dict( |
| 201 |
type='str', |
| 202 |
required=False, |
| 203 |
), |
| 204 |
resource_definition=dict( |
| 205 |
type='str', |
| 206 |
required=False, |
| 207 |
), |
| 208 |
resource_group=dict( |
| 209 |
type='str', |
| 210 |
required=True, |
| 211 |
), |
| 212 |
ml_workspace=dict( |
| 213 |
type='str', |
| 214 |
required=True, |
| 215 |
), |
| 216 |
local=dict( |
| 217 |
type='bool', |
| 218 |
default=False, |
| 219 |
), |
| 220 |
local_enable_gpu=dict( |
| 221 |
type='bool', |
| 222 |
default=False, |
| 223 |
), |
| 224 |
package_model=dict( |
| 225 |
type='bool', |
| 226 |
default=False, |
| 227 |
), |
| 228 |
description=dict( |
| 229 |
type='str', |
| 230 |
required=False, |
| 231 |
), |
| 232 |
all_traffic=dict( |
| 233 |
type='bool', |
| 234 |
default=False, |
| 235 |
), |
| 236 |
state=dict( |
| 237 |
type='str', |
| 238 |
default='present', |
| 239 |
choices=['present', 'absent'], |
| 240 |
), |
| 241 |
) |
| 242 |
|
| 243 |
self._client = None |
| 244 |
self.ml_registry = None |
| 245 |
self.state = None |
| 246 |
|
| 247 |
self.results = dict( |
| 248 |
changed=False, |
| 249 |
compare=[], |
| 250 |
ml_online_deployment={} |
| 251 |
) |
| 252 |
|
| 253 |
required_if = [ |
| 254 |
('state', 'present', ['resource_definition']), |
| 255 |
('resource_definition', None, ['name', 'endpoint_name']) |
| 256 |
] |
| 257 |
|
| 258 |
super(AzureRMMLOnlineDeployment, self).__init__(self.module_arg_spec, |
| 259 |
supports_check_mode=True, |
| 260 |
required_if=required_if, |
| 261 |
) |
| 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 |
changed = False |
| 269 |
|
| 270 |
if self.resource_definition: |
| 271 |
resource_definition = io.StringIO(self.resource_definition) |
| 272 |
params_override = self.update_params(kwargs, |
| 273 |
self.UPDATE_OVERRIDES) |
| 274 |
ml_online_deployment = load_online_deployment( |
| 275 |
resource_definition, |
| 276 |
params_override=params_override |
| 277 |
) |
| 278 |
deployment_name = ml_online_deployment.name |
| 279 |
endpoint_name = ml_online_deployment.endpoint_name |
| 280 |
else: |
| 281 |
deployment_name = self.name |
| 282 |
endpoint_name = self.endpoint_name |
| 283 |
|
| 284 |
ml_online_deployment_info = self.get(deployment_name, |
| 285 |
endpoint_name, |
| 286 |
local=self.local) |
| 287 |
|
| 288 |
if self.state == 'present': |
| 289 |
|
| 290 |
if ml_online_deployment_info: |
| 291 |
|
| 292 |
params_override = self.update_params(kwargs, |
| 293 |
self.UPDATE_OVERRIDES, |
| 294 |
ml_online_deployment_info) |
| 295 |
|
| 296 |
if self.resource_definition: |
| 297 |
resource_definition = io.StringIO(self.resource_definition) |
| 298 |
|
| 299 |
|
| 300 |
ml_online_deployment = load_online_deployment( |
| 301 |
resource_definition, |
| 302 |
params_override=params_override |
| 303 |
) |
| 304 |
ml_online_deployment_info = self.entity_to_dict(ml_online_deployment_info) |
| 305 |
changed = not self.default_compare( |
| 306 |
{}, |
| 307 |
self.entity_to_dict(ml_online_deployment), |
| 308 |
ml_online_deployment_info, |
| 309 |
'', |
| 310 |
self.results |
| 311 |
) |
| 312 |
if changed and not self.check_mode: |
| 313 |
response = self.client.begin_create_or_update( |
| 314 |
ml_online_deployment, |
| 315 |
local=self.local, |
| 316 |
local_enable_gpu=self.local_enable_gpu, |
| 317 |
) |
| 318 |
ml_online_deployment_object = self.get_poller_result(response) |
| 319 |
ml_online_deployment_info = self.entity_to_dict(ml_online_deployment_object) |
| 320 |
else: |
| 321 |
|
| 322 |
if isinstance(ml_online_deployment.model, str) and \ |
| 323 |
ml_online_deployment.model.startswith(REGISTRY_URI_FORMAT): |
| 324 |
self.ml_registry = ml_online_deployment.model.split("/")[3] |
| 325 |
|
| 326 |
changed = True |
| 327 |
if not self.check_mode: |
| 328 |
response = self.client.begin_create_or_update( |
| 329 |
ml_online_deployment, |
| 330 |
local=self.local, |
| 331 |
local_enable_gpu=self.local_enable_gpu, |
| 332 |
package_model=self.package_model |
| 333 |
) |
| 334 |
ml_online_deployment = self.get_poller_result(response) |
| 335 |
ml_online_deployment_info = self.entity_to_dict(ml_online_deployment) |
| 336 |
if not self.check_mode and self.all_traffic: |
| 337 |
self._all_traffic(ml_online_deployment, local=self.local) |
| 338 |
|
| 339 |
elif self.state == 'absent': |
| 340 |
if ml_online_deployment_info: |
| 341 |
|
| 342 |
changed = True |
| 343 |
if not self.check_mode: |
| 344 |
response = self.client.online_deployments.begin_delete( |
| 345 |
name=deployment_name, |
| 346 |
endpoint_name=endpoint_name, |
| 347 |
local=self.local |
| 348 |
) |
| 349 |
ml_online_deployment_info = self.get_poller_result(response) |
| 350 |
|
| 351 |
self.results['ml_online_deployment'] = ml_online_deployment_info |
| 352 |
self.results['changed'] = changed |
| 353 |
return self.results |
| 354 |
|
| 355 |
def update_params(self, kwargs, param_overrides, ml_online_deployment=None): |
| 356 |
params_override = [] |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
if ml_online_deployment: |
| 362 |
items = self.entity_to_dict(ml_online_deployment).items() |
| 363 |
params_override = [{k: v} for k, v in items] |
| 364 |
|
| 365 |
|
| 366 |
for key in list(self.module_arg_spec.keys()): |
| 367 |
if kwargs[key] is not None and key in param_overrides: |
| 368 |
params_override.append({key: kwargs[key]}) |
| 369 |
return params_override |
| 370 |
|
| 371 |
def get(self, name, endpoint_name, local=None, as_dict=False): |
| 372 |
try: |
| 373 |
|
| 374 |
ml_online_deployment_info = self.client.online_deployments.get( |
| 375 |
name=name, |
| 376 |
endpoint_name=endpoint_name, |
| 377 |
local=local |
| 378 |
) |
| 379 |
if as_dict: |
| 380 |
ml_online_deployment_info = self.entity_to_dict(ml_online_deployment_info) |
| 381 |
except ResourceNotFoundError: |
| 382 |
ml_online_deployment_info = None |
| 383 |
return ml_online_deployment_info |
| 384 |
|
| 385 |
def _all_traffic(self, ml_deployment, local=False): |
| 386 |
endpoint = self.client.online_endpoints.get( |
| 387 |
ml_deployment.endpoint_name, |
| 388 |
local=local |
| 389 |
) |
| 390 |
endpoint.traffic = {ml_deployment.name: 100} |
| 391 |
response = self.client.begin_create_or_update(endpoint, local=local) |
| 392 |
return self.get_poller_result(response) |
| 393 |
|
| 394 |
|
| 395 |
def main(): |
| 396 |
AzureRMMLOnlineDeployment() |
| 397 |
|
| 398 |
|
| 399 |
if __name__ == '__main__': |
| 400 |
main() |