| 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_endpoint |
| 11 |
version_added: "3.17.0" |
| 12 |
short_description: Create, Delete or regenerate keys for an Azure Machine Learning Online Endpoint |
| 13 |
description: |
| 14 |
- Create, Delete or regenerate keys for an Azure Machine Learning Online Endpoint. |
| 15 |
options: |
| 16 |
name: |
| 17 |
description: |
| 18 |
- Name of the Azure ML online_endpoint. |
| 19 |
type: str |
| 20 |
required: true |
| 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 |
resource_definition: |
| 32 |
description: |
| 33 |
- Online Endpoint definition in YAML |
| 34 |
type: str |
| 35 |
required: false |
| 36 |
description: |
| 37 |
description: |
| 38 |
- Description of the online_endpoint. |
| 39 |
type: str |
| 40 |
local: |
| 41 |
description: |
| 42 |
- Update local endpoint. |
| 43 |
type: bool |
| 44 |
default: false |
| 45 |
auth_mode: |
| 46 |
description: |
| 47 |
- Authentication method for the endpoint. |
| 48 |
default: key |
| 49 |
type: str |
| 50 |
choices: |
| 51 |
- key |
| 52 |
- aml_token |
| 53 |
mirror_traffic: |
| 54 |
description: |
| 55 |
- Key and Numeric value to Direct a duplicate percentage |
| 56 |
of live traffic to a train a deployment. |
| 57 |
required: false |
| 58 |
type: dict |
| 59 |
traffic: |
| 60 |
description: |
| 61 |
- Key and Numeric value for traffic settings of this endpoint. |
| 62 |
required: false |
| 63 |
type: dict |
| 64 |
key_type: |
| 65 |
description: |
| 66 |
- The type of key to regenerate. |
| 67 |
type: str |
| 68 |
default: primary |
| 69 |
choices: |
| 70 |
- primary |
| 71 |
- secondary |
| 72 |
state: |
| 73 |
description: |
| 74 |
- State of the Online Endpoint. Use C(present) to create |
| 75 |
or update. C(regenerate_keys) to regenerate keys. |
| 76 |
default: present |
| 77 |
type: str |
| 78 |
choices: |
| 79 |
- present |
| 80 |
- absent |
| 81 |
- regenerate_keys |
| 82 |
|
| 83 |
extends_documentation_fragment: |
| 84 |
- azure.azcollection.azure |
| 85 |
- azure.azcollection.azure_tags |
| 86 |
|
| 87 |
author: |
| 88 |
- Bill Peck (@p3ck) |
| 89 |
''' |
| 90 |
|
| 91 |
EXAMPLES = ''' |
| 92 |
- name: Create ML Online Endpoint |
| 93 |
azure.azcollection.azure_rm_ml_online_endpoint: |
| 94 |
name: onlineendpointxxxxxxxxxx |
| 95 |
resource_group: xxxxx-ml-online_endpoint |
| 96 |
ml_workspace: workspace-xxxxxxxxxx |
| 97 |
resource_definition: | |
| 98 |
''' |
| 99 |
|
| 100 |
RETURN = ''' |
| 101 |
changed: |
| 102 |
description: |
| 103 |
- Whether the resource is changed. |
| 104 |
returned: always |
| 105 |
type: bool |
| 106 |
sample: false |
| 107 |
ml_online_endpoint: |
| 108 |
description: |
| 109 |
- Online Endpoint that was just created or updated. |
| 110 |
returned: always |
| 111 |
type: dict |
| 112 |
sample: { |
| 113 |
"auth_mode": "key", |
| 114 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-online_endpoint/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/onlineEndpoints/endpoint-command-xxxxxxxxxx", |
| 115 |
"identity": { |
| 116 |
"principal_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 117 |
"tenant_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 118 |
"type": "system_assigned" |
| 119 |
}, |
| 120 |
"kind": "Managed", |
| 121 |
"location": "eastus", |
| 122 |
"mirror_traffic": {}, |
| 123 |
"name": "endpoint-command-xxxxxxxxxx", |
| 124 |
"openapi_uri": "https://endpoint-command-xxxxxxxxxx.eastus.inference.ml.azure.com/swagger.json", |
| 125 |
"properties": { |
| 126 |
"AzureAsyncOperationUri": "https://management.azure.com/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.MachineLearningServices/locations/eastus/mfeOperationsStatus/oeidp:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx?api-version=2022-02-01-preview", |
| 127 |
"azureml.onlineendpointid": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/xxxxx-ml-online_endpoint/providers/microsoft.machinelearningservices/workspaces/workspace-xxxxxxxxxx/onlineendpoints/endpoint-command-xxxxxxxxxx", |
| 128 |
"createdAt": "2026-04-09T15:04:48.831114+0000", |
| 129 |
"createdBy": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 130 |
"lastModifiedAt": "2026-04-09T15:04:48.831114+0000" |
| 131 |
}, |
| 132 |
"provisioning_state": "Succeeded", |
| 133 |
"public_network_access": "enabled", |
| 134 |
"scoring_uri": "https://endpoint-command-xxxxxxxxxx.eastus.inference.ml.azure.com/score", |
| 135 |
"tags": {}, |
| 136 |
"traffic": {} |
| 137 |
} |
| 138 |
''' |
| 139 |
|
| 140 |
|
| 141 |
try: |
| 142 |
import io |
| 143 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon |
| 144 |
from azure.core.exceptions import ResourceNotFoundError |
| 145 |
from azure.ai.ml.entities._load_functions import load_online_endpoint |
| 146 |
from azure.ai.ml.constants._endpoint import EndpointKeyType |
| 147 |
except ImportError: |
| 148 |
|
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
class AzureRMMLOnlineEndpoint(MLClientCommon): |
| 153 |
|
| 154 |
UPDATE_OVERRIDES = ['description', |
| 155 |
'tags', |
| 156 |
'mirror_traffic', |
| 157 |
'traffic'] |
| 158 |
|
| 159 |
PARAM_OVERRIDES = UPDATE_OVERRIDES + ['name', |
| 160 |
'auth_mode'] |
| 161 |
|
| 162 |
def __init__(self): |
| 163 |
|
| 164 |
self.module_arg_spec = dict( |
| 165 |
name=dict( |
| 166 |
type='str', |
| 167 |
required=True, |
| 168 |
), |
| 169 |
resource_definition=dict( |
| 170 |
type='str', |
| 171 |
required=False, |
| 172 |
), |
| 173 |
resource_group=dict( |
| 174 |
type='str', |
| 175 |
required=True, |
| 176 |
), |
| 177 |
ml_workspace=dict( |
| 178 |
type='str', |
| 179 |
required=True, |
| 180 |
), |
| 181 |
local=dict( |
| 182 |
type='bool', |
| 183 |
default=False, |
| 184 |
), |
| 185 |
description=dict( |
| 186 |
type='str', |
| 187 |
required=False, |
| 188 |
), |
| 189 |
auth_mode=dict( |
| 190 |
type='str', |
| 191 |
default='key', |
| 192 |
choices=['key', 'aml_token'], |
| 193 |
), |
| 194 |
mirror_traffic=dict( |
| 195 |
type='dict', |
| 196 |
required=False, |
| 197 |
), |
| 198 |
traffic=dict( |
| 199 |
type='dict', |
| 200 |
required=False, |
| 201 |
), |
| 202 |
key_type=dict( |
| 203 |
type='str', |
| 204 |
default='primary', |
| 205 |
choices=['primary', 'secondary'], |
| 206 |
), |
| 207 |
state=dict( |
| 208 |
type='str', |
| 209 |
default='present', |
| 210 |
choices=['present', 'absent', 'regenerate_keys'], |
| 211 |
), |
| 212 |
) |
| 213 |
|
| 214 |
self._client = None |
| 215 |
self.ml_registry = None |
| 216 |
self.state = None |
| 217 |
|
| 218 |
self.results = dict( |
| 219 |
changed=False, |
| 220 |
compare=[], |
| 221 |
ml_online_endpoint={} |
| 222 |
) |
| 223 |
|
| 224 |
required_if = [ |
| 225 |
('state', 'present', ['resource_definition']) |
| 226 |
] |
| 227 |
|
| 228 |
super(AzureRMMLOnlineEndpoint, self).__init__(self.module_arg_spec, |
| 229 |
supports_check_mode=True, |
| 230 |
required_if=required_if, |
| 231 |
) |
| 232 |
|
| 233 |
def exec_module(self, **kwargs): |
| 234 |
|
| 235 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 236 |
setattr(self, key, kwargs[key]) |
| 237 |
|
| 238 |
changed = False |
| 239 |
|
| 240 |
ml_online_endpoint_info = self.get(name=self.name, |
| 241 |
local=self.local) |
| 242 |
|
| 243 |
if self.state == 'present': |
| 244 |
if self.resource_definition: |
| 245 |
resource_definition = io.StringIO(self.resource_definition) |
| 246 |
|
| 247 |
if ml_online_endpoint_info: |
| 248 |
|
| 249 |
params_override = self.update_params(kwargs, |
| 250 |
self.UPDATE_OVERRIDES, |
| 251 |
ml_online_endpoint_info) |
| 252 |
|
| 253 |
|
| 254 |
ml_online_endpoint = load_online_endpoint(resource_definition, |
| 255 |
params_override=params_override) |
| 256 |
ml_online_endpoint_info = self.entity_to_dict(ml_online_endpoint_info) |
| 257 |
changed = not self.default_compare({}, |
| 258 |
self.entity_to_dict(ml_online_endpoint), |
| 259 |
ml_online_endpoint_info, |
| 260 |
'', |
| 261 |
self.results) |
| 262 |
if changed and not self.check_mode: |
| 263 |
response = self.client.begin_create_or_update(ml_online_endpoint, |
| 264 |
local=self.local) |
| 265 |
ml_online_endpoint_object = self.get_poller_result(response) |
| 266 |
ml_online_endpoint_info = self.entity_to_dict(ml_online_endpoint_object) |
| 267 |
else: |
| 268 |
|
| 269 |
params_override = self.update_params(kwargs, self.PARAM_OVERRIDES) |
| 270 |
|
| 271 |
|
| 272 |
ml_online_endpoint = load_online_endpoint(source=resource_definition, |
| 273 |
params_override=params_override) |
| 274 |
|
| 275 |
changed = True |
| 276 |
if not self.check_mode: |
| 277 |
response = self.client.begin_create_or_update(ml_online_endpoint, |
| 278 |
local=self.local) |
| 279 |
ml_online_endpoint_object = self.get_poller_result(response) |
| 280 |
ml_online_endpoint_info = self.entity_to_dict(ml_online_endpoint_object) |
| 281 |
elif self.state == 'absent': |
| 282 |
if ml_online_endpoint_info: |
| 283 |
|
| 284 |
changed = True |
| 285 |
if not self.check_mode: |
| 286 |
response = self.client.online_endpoints.begin_delete(name=self.name, |
| 287 |
local=self.local) |
| 288 |
ml_online_endpoint_info = self.get_poller_result(response) |
| 289 |
elif self.state == 'regenerate_keys': |
| 290 |
if ml_online_endpoint_info: |
| 291 |
changed = True |
| 292 |
if not self.check_mode: |
| 293 |
key_type = self.get_key_type(self.key_type) |
| 294 |
self.client.online_endpoints.begin_regenerate_keys(name=self.name, |
| 295 |
key_type=key_type) |
| 296 |
ml_online_endpoint_info = self.entity_to_dict(ml_online_endpoint_info) |
| 297 |
else: |
| 298 |
|
| 299 |
self.fail("Online Endpoint doesn't exist") |
| 300 |
|
| 301 |
self.results['ml_online_endpoint'] = ml_online_endpoint_info |
| 302 |
self.results['changed'] = changed |
| 303 |
return self.results |
| 304 |
|
| 305 |
def update_params(self, kwargs, param_overrides, ml_online_endpoint=None): |
| 306 |
params_override = [] |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
if ml_online_endpoint: |
| 312 |
items = self.entity_to_dict(ml_online_endpoint).items() |
| 313 |
params_override = [{k: v} for k, v in items] |
| 314 |
|
| 315 |
|
| 316 |
for key in list(self.module_arg_spec.keys()): |
| 317 |
if kwargs[key] is not None and key in param_overrides: |
| 318 |
params_override.append({key: kwargs[key]}) |
| 319 |
return params_override |
| 320 |
|
| 321 |
def get(self, name, local=None, as_dict=False): |
| 322 |
try: |
| 323 |
|
| 324 |
ml_online_endpoint_info = self.client.online_endpoints.get(name=name, |
| 325 |
local=local) |
| 326 |
if as_dict: |
| 327 |
ml_online_endpoint_info = self.entity_to_dict(ml_online_endpoint_info) |
| 328 |
except ResourceNotFoundError: |
| 329 |
ml_online_endpoint_info = None |
| 330 |
return ml_online_endpoint_info |
| 331 |
|
| 332 |
def get_key_type(self, key_type): |
| 333 |
key_types = dict(primary=EndpointKeyType.PRIMARY_KEY_TYPE, |
| 334 |
secondary=EndpointKeyType.SECONDARY_KEY_TYPE) |
| 335 |
return key_types.get(key_type, EndpointKeyType.PRIMARY_KEY_TYPE) |
| 336 |
|
| 337 |
|
| 338 |
def main(): |
| 339 |
AzureRMMLOnlineEndpoint() |
| 340 |
|
| 341 |
|
| 342 |
if __name__ == '__main__': |
| 343 |
main() |