| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_containerinstance |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage an Azure Container Instance |
| 16 |
description: |
| 17 |
- Create, update and delete an Azure Container Instance. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- Name of resource group. |
| 23 |
type: str |
| 24 |
required: true |
| 25 |
name: |
| 26 |
description: |
| 27 |
- The name of the container group. |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
os_type: |
| 31 |
description: |
| 32 |
- The OS type of containers. |
| 33 |
type: str |
| 34 |
choices: |
| 35 |
- linux |
| 36 |
- windows |
| 37 |
- Linux |
| 38 |
- Windows |
| 39 |
default: linux |
| 40 |
state: |
| 41 |
description: |
| 42 |
- Assert the state of the container instance. Use C(present) to create or update an container instance and C(absent) to delete it. |
| 43 |
type: str |
| 44 |
default: present |
| 45 |
choices: |
| 46 |
- absent |
| 47 |
- present |
| 48 |
ip_address: |
| 49 |
description: |
| 50 |
- The IP address type of the container group. |
| 51 |
- Default is C(none) and creating an instance without public IP. |
| 52 |
type: str |
| 53 |
choices: |
| 54 |
- public |
| 55 |
- none |
| 56 |
- private |
| 57 |
default: 'none' |
| 58 |
dns_name_label: |
| 59 |
description: |
| 60 |
- The Dns name label for the IP. |
| 61 |
type: str |
| 62 |
ports: |
| 63 |
description: |
| 64 |
- List of ports exposed within the container group. |
| 65 |
- This option is deprecated, using I(ports) under I(containers)". |
| 66 |
type: list |
| 67 |
elements: int |
| 68 |
default: [] |
| 69 |
location: |
| 70 |
description: |
| 71 |
- Valid azure location. Defaults to location of the resource group. |
| 72 |
type: str |
| 73 |
registry_login_server: |
| 74 |
description: |
| 75 |
- The container image registry login server. |
| 76 |
type: str |
| 77 |
registry_username: |
| 78 |
description: |
| 79 |
- The username to log in container image registry server. |
| 80 |
type: str |
| 81 |
registry_password: |
| 82 |
description: |
| 83 |
- The password to log in container image registry server. |
| 84 |
type: str |
| 85 |
acr_identity: |
| 86 |
description: |
| 87 |
- The Identity to use for access to the registry server. |
| 88 |
type: str |
| 89 |
version_added: '2.5.0' |
| 90 |
containers: |
| 91 |
description: |
| 92 |
- List of containers. |
| 93 |
- Required when creation. |
| 94 |
type: list |
| 95 |
elements: dict |
| 96 |
suboptions: |
| 97 |
name: |
| 98 |
description: |
| 99 |
- The name of the container instance. |
| 100 |
type: str |
| 101 |
required: true |
| 102 |
image: |
| 103 |
description: |
| 104 |
- The container image name. |
| 105 |
type: str |
| 106 |
required: true |
| 107 |
memory: |
| 108 |
description: |
| 109 |
- The required memory of the containers in GB. |
| 110 |
type: float |
| 111 |
default: 1.5 |
| 112 |
cpu: |
| 113 |
description: |
| 114 |
- The required number of CPU cores of the containers. |
| 115 |
type: float |
| 116 |
default: 1 |
| 117 |
ports: |
| 118 |
description: |
| 119 |
- List of ports exposed within the container group. |
| 120 |
type: list |
| 121 |
elements: int |
| 122 |
environment_variables: |
| 123 |
description: |
| 124 |
- List of container environment variables. |
| 125 |
- When updating existing container all existing variables will be replaced by new ones. |
| 126 |
type: list |
| 127 |
elements: dict |
| 128 |
suboptions: |
| 129 |
name: |
| 130 |
description: |
| 131 |
- Environment variable name. |
| 132 |
type: str |
| 133 |
required: true |
| 134 |
value: |
| 135 |
description: |
| 136 |
- Environment variable value. |
| 137 |
type: str |
| 138 |
required: true |
| 139 |
is_secure: |
| 140 |
description: |
| 141 |
- Is variable secure. |
| 142 |
type: bool |
| 143 |
volume_mounts: |
| 144 |
description: |
| 145 |
- The volume mounts for the container instance |
| 146 |
type: list |
| 147 |
elements: dict |
| 148 |
suboptions: |
| 149 |
name: |
| 150 |
description: |
| 151 |
- The name of the volume mount |
| 152 |
required: true |
| 153 |
type: str |
| 154 |
mount_path: |
| 155 |
description: |
| 156 |
- The path within the container where the volume should be mounted |
| 157 |
required: true |
| 158 |
type: str |
| 159 |
read_only: |
| 160 |
description: |
| 161 |
- The flag indicating whether the volume mount is read-only |
| 162 |
type: bool |
| 163 |
commands: |
| 164 |
description: |
| 165 |
- List of commands to execute within the container instance in exec form. |
| 166 |
- When updating existing container all existing commands will be replaced by new ones. |
| 167 |
type: list |
| 168 |
elements: str |
| 169 |
restart_policy: |
| 170 |
description: |
| 171 |
- Restart policy for all containers within the container group. |
| 172 |
type: str |
| 173 |
choices: |
| 174 |
- always |
| 175 |
- on_failure |
| 176 |
- never |
| 177 |
subnet_ids: |
| 178 |
description: |
| 179 |
- The subnet resource IDs for a container group. |
| 180 |
- Multiple subnets are not yet supported. Only 1 subnet can be used. |
| 181 |
type: list |
| 182 |
elements: str |
| 183 |
volumes: |
| 184 |
description: |
| 185 |
- List of Volumes that can be mounted by containers in this container group. |
| 186 |
type: list |
| 187 |
elements: dict |
| 188 |
suboptions: |
| 189 |
name: |
| 190 |
description: |
| 191 |
- The name of the Volume |
| 192 |
required: true |
| 193 |
type: str |
| 194 |
azure_file: |
| 195 |
description: |
| 196 |
- The Azure File volume |
| 197 |
type: dict |
| 198 |
suboptions: |
| 199 |
share_name: |
| 200 |
description: |
| 201 |
- The name of the Azure File share to be mounted as a volume |
| 202 |
required: true |
| 203 |
type: str |
| 204 |
read_only: |
| 205 |
description: |
| 206 |
- The flag indicating whether the Azure File shared mounted as a volume is read-only |
| 207 |
type: bool |
| 208 |
storage_account_name: |
| 209 |
description: |
| 210 |
- The name of the storage account that contains the Azure File share |
| 211 |
required: true |
| 212 |
type: str |
| 213 |
storage_account_key: |
| 214 |
description: |
| 215 |
- The storage account access key used to access the Azure File share |
| 216 |
required: true |
| 217 |
type: str |
| 218 |
empty_dir: |
| 219 |
description: |
| 220 |
- The empty directory volume |
| 221 |
type: dict |
| 222 |
secret: |
| 223 |
description: |
| 224 |
- The secret volume |
| 225 |
type: dict |
| 226 |
git_repo: |
| 227 |
description: |
| 228 |
- The git repo volume |
| 229 |
type: dict |
| 230 |
suboptions: |
| 231 |
directory: |
| 232 |
description: |
| 233 |
- Target directory name |
| 234 |
type: str |
| 235 |
repository: |
| 236 |
description: |
| 237 |
- Repository URL |
| 238 |
required: true |
| 239 |
type: str |
| 240 |
revision: |
| 241 |
description: |
| 242 |
- Commit hash for the specified revision |
| 243 |
type: str |
| 244 |
identity: |
| 245 |
description: |
| 246 |
- Identity for the Server. |
| 247 |
type: dict |
| 248 |
version_added: '2.5.0' |
| 249 |
suboptions: |
| 250 |
type: |
| 251 |
description: |
| 252 |
- Type of the managed identity |
| 253 |
required: false |
| 254 |
choices: |
| 255 |
- SystemAssigned |
| 256 |
- SystemAssigned, UserAssigned |
| 257 |
- UserAssigned |
| 258 |
default: None |
| 259 |
type: str |
| 260 |
user_assigned_identities: |
| 261 |
description: |
| 262 |
- User Assigned Managed Identities and its options |
| 263 |
required: false |
| 264 |
type: dict |
| 265 |
default: {} |
| 266 |
suboptions: |
| 267 |
id: |
| 268 |
description: |
| 269 |
- List of the user assigned identities IDs associated to the VM |
| 270 |
required: false |
| 271 |
type: list |
| 272 |
elements: str |
| 273 |
default: [] |
| 274 |
force_update: |
| 275 |
description: |
| 276 |
- Force update of existing container instance. Any update will result in deletion and recreation of existing containers. |
| 277 |
type: bool |
| 278 |
default: 'no' |
| 279 |
|
| 280 |
extends_documentation_fragment: |
| 281 |
- azure.azcollection.azure |
| 282 |
- azure.azcollection.azure_tags |
| 283 |
|
| 284 |
author: |
| 285 |
- Zim Kalinowski (@zikalino) |
| 286 |
|
| 287 |
''' |
| 288 |
|
| 289 |
EXAMPLES = ''' |
| 290 |
- name: Create sample container group |
| 291 |
azure_rm_containerinstance: |
| 292 |
resource_group: myResourceGroup |
| 293 |
name: myContainerInstanceGroup |
| 294 |
os_type: linux |
| 295 |
ip_address: public |
| 296 |
containers: |
| 297 |
- name: myContainer1 |
| 298 |
image: httpd |
| 299 |
memory: 1.5 |
| 300 |
ports: |
| 301 |
- 80 |
| 302 |
- 81 |
| 303 |
|
| 304 |
- name: Create sample container group with azure file share volume |
| 305 |
azure_rm_containerinstance: |
| 306 |
resource_group: myResourceGroup |
| 307 |
name: myContainerInstanceGroupz |
| 308 |
os_type: linux |
| 309 |
ip_address: public |
| 310 |
containers: |
| 311 |
- name: mycontainer1 |
| 312 |
image: httpd |
| 313 |
memory: 1 |
| 314 |
volume_mounts: |
| 315 |
- name: filesharevolume |
| 316 |
mount_path: "/data/files" |
| 317 |
ports: |
| 318 |
- 80 |
| 319 |
- 81 |
| 320 |
volumes: |
| 321 |
- name: filesharevolume |
| 322 |
azure_file: |
| 323 |
storage_account_name: mystorageaccount |
| 324 |
share_name: acishare |
| 325 |
storage_account_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 326 |
|
| 327 |
- name: Create sample container group with git repo volume |
| 328 |
azure_rm_containerinstance: |
| 329 |
resource_group: myResourceGroup |
| 330 |
name: myContainerInstanceGroup |
| 331 |
os_type: linux |
| 332 |
ip_address: public |
| 333 |
containers: |
| 334 |
- name: mycontainer1 |
| 335 |
image: httpd |
| 336 |
memory: 1 |
| 337 |
volume_mounts: |
| 338 |
- name: myvolume1 |
| 339 |
mount_path: "/mnt/test" |
| 340 |
ports: |
| 341 |
- 80 |
| 342 |
- 81 |
| 343 |
volumes: |
| 344 |
- name: myvolume1 |
| 345 |
git_repo: |
| 346 |
repository: "https://github.com/Azure-Samples/aci-helloworld.git" |
| 347 |
|
| 348 |
- name: Create sample container instance with subnet |
| 349 |
azure_rm_containerinstance: |
| 350 |
resource_group: myResourceGroup |
| 351 |
name: myContainerInstanceGroup |
| 352 |
os_type: linux |
| 353 |
ip_address: private |
| 354 |
location: eastus |
| 355 |
subnet_ids: |
| 356 |
- "{{ subnet_id }}" |
| 357 |
ports: |
| 358 |
- 80 |
| 359 |
containers: |
| 360 |
- name: mycontainer1 |
| 361 |
image: httpd |
| 362 |
memory: 1.5 |
| 363 |
ports: |
| 364 |
- 80 |
| 365 |
- 81 |
| 366 |
''' |
| 367 |
RETURN = ''' |
| 368 |
id: |
| 369 |
description: |
| 370 |
- Resource ID. |
| 371 |
returned: always |
| 372 |
type: str |
| 373 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/containerGroups/aci1b6dd89 |
| 374 |
provisioning_state: |
| 375 |
description: |
| 376 |
- Provisioning state of the container. |
| 377 |
returned: always |
| 378 |
type: str |
| 379 |
sample: Creating |
| 380 |
ip_address: |
| 381 |
description: |
| 382 |
- Public IP Address of created container group. |
| 383 |
returned: if address is public |
| 384 |
type: str |
| 385 |
sample: 175.12.233.11 |
| 386 |
containers: |
| 387 |
description: |
| 388 |
- The containers within the container group. |
| 389 |
returned: always |
| 390 |
type: list |
| 391 |
elements: dict |
| 392 |
sample: [ |
| 393 |
{ |
| 394 |
"commands": null, |
| 395 |
"cpu": 1.0, |
| 396 |
"environment_variables": null, |
| 397 |
"image": "httpd", |
| 398 |
"memory": 1.0, |
| 399 |
"name": "mycontainer1", |
| 400 |
"ports": [ |
| 401 |
80, |
| 402 |
81 |
| 403 |
], |
| 404 |
"volume_mounts": [ |
| 405 |
{ |
| 406 |
"mount_path": "/data/files", |
| 407 |
"name": "filesharevolume", |
| 408 |
"read_only": false |
| 409 |
} |
| 410 |
] |
| 411 |
} |
| 412 |
] |
| 413 |
volumes: |
| 414 |
description: |
| 415 |
- The list of volumes that mounted by containers in container group |
| 416 |
returned: if volumes specified |
| 417 |
type: list |
| 418 |
elements: dict |
| 419 |
contains: |
| 420 |
name: |
| 421 |
description: |
| 422 |
- The name of the Volume |
| 423 |
returned: always |
| 424 |
type: str |
| 425 |
sample: filesharevolume |
| 426 |
azure_file: |
| 427 |
description: |
| 428 |
- Azure file share volume details |
| 429 |
returned: If Azure file share type of volume requested |
| 430 |
type: dict |
| 431 |
sample: { |
| 432 |
"read_only": null, |
| 433 |
"share_name": "acishare", |
| 434 |
"storage_account_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 435 |
"storage_account_name": "mystorageaccount" |
| 436 |
} |
| 437 |
empty_dir: |
| 438 |
description: |
| 439 |
- Empty directory volume details |
| 440 |
returned: If Empty directory type of volume requested |
| 441 |
type: dict |
| 442 |
sample: {} |
| 443 |
secret: |
| 444 |
description: |
| 445 |
- Secret volume details |
| 446 |
returned: If Secret type of volume requested |
| 447 |
type: dict |
| 448 |
sample: {} |
| 449 |
git_repo: |
| 450 |
description: |
| 451 |
- Git Repo volume details |
| 452 |
returned: If Git repo type of volume requested |
| 453 |
type: dict |
| 454 |
sample: { |
| 455 |
"directory": null, |
| 456 |
"repository": "https://github.com/Azure-Samples/aci-helloworld.git", |
| 457 |
"revision": null |
| 458 |
} |
| 459 |
''' |
| 460 |
|
| 461 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 462 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel |
| 463 |
|
| 464 |
try: |
| 465 |
from azure.core.exceptions import ResourceNotFoundError |
| 466 |
from azure.core.polling import LROPoller |
| 467 |
from azure.mgmt.containerinstance.models import ( |
| 468 |
ContainerGroupIdentity, |
| 469 |
) |
| 470 |
except ImportError: |
| 471 |
|
| 472 |
pass |
| 473 |
|
| 474 |
|
| 475 |
def create_container_dict_from_obj(container): |
| 476 |
''' |
| 477 |
Create a dict from an instance of a Container. |
| 478 |
|
| 479 |
:param rule: Container |
| 480 |
:return: dict |
| 481 |
''' |
| 482 |
results = dict( |
| 483 |
name=container.name, |
| 484 |
image=container.image, |
| 485 |
memory=container.resources.requests.memory_in_gb, |
| 486 |
cpu=container.resources.requests.cpu |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
) |
| 493 |
|
| 494 |
if container.instance_view is not None: |
| 495 |
|
| 496 |
results["instance_restart_count"] = container.instance_view.restart_count |
| 497 |
if container.instance_view.current_state: |
| 498 |
results["instance_current_state"] = container.instance_view.current_state.state |
| 499 |
results["instance_current_start_time"] = container.instance_view.current_state.start_time |
| 500 |
results["instance_current_exit_code"] = container.instance_view.current_state.exit_code |
| 501 |
results["instance_current_finish_time"] = container.instance_view.current_state.finish_time |
| 502 |
results["instance_current_detail_status"] = container.instance_view.current_state.detail_status |
| 503 |
if container.instance_view.previous_state: |
| 504 |
results["instance_previous_state"] = container.instance_view.previous_state.state |
| 505 |
results["instance_previous_start_time"] = container.instance_view.previous_state.start_time |
| 506 |
results["instance_previous_exit_code"] = container.instance_view.previous_state.exit_code |
| 507 |
results["instance_previous_finish_time"] = container.instance_view.previous_state.finish_time |
| 508 |
results["instance_previous_detail_status"] = container.instance_view.previous_state.detail_status |
| 509 |
|
| 510 |
return results |
| 511 |
|
| 512 |
|
| 513 |
env_var_spec = dict( |
| 514 |
name=dict(type='str', required=True), |
| 515 |
value=dict(type='str', required=True), |
| 516 |
is_secure=dict(type='bool') |
| 517 |
) |
| 518 |
|
| 519 |
|
| 520 |
volume_mount_var_spec = dict( |
| 521 |
name=dict(type='str', required=True), |
| 522 |
mount_path=dict(type='str', required=True), |
| 523 |
read_only=dict(type='bool') |
| 524 |
) |
| 525 |
|
| 526 |
|
| 527 |
container_spec = dict( |
| 528 |
name=dict(type='str', required=True), |
| 529 |
image=dict(type='str', required=True), |
| 530 |
memory=dict(type='float', default=1.5), |
| 531 |
cpu=dict(type='float', default=1), |
| 532 |
ports=dict(type='list', elements='int'), |
| 533 |
commands=dict(type='list', elements='str'), |
| 534 |
environment_variables=dict(type='list', elements='dict', options=env_var_spec), |
| 535 |
volume_mounts=dict(type='list', elements='dict', options=volume_mount_var_spec) |
| 536 |
) |
| 537 |
|
| 538 |
|
| 539 |
git_repo_volume_spec = dict( |
| 540 |
directory=dict(type='str'), |
| 541 |
repository=dict(type='str', required=True), |
| 542 |
revision=dict(type='str') |
| 543 |
) |
| 544 |
|
| 545 |
|
| 546 |
azure_file_volume_spec = dict( |
| 547 |
share_name=dict(type='str', required=True), |
| 548 |
read_only=dict(type='bool'), |
| 549 |
storage_account_name=dict(type='str', required=True), |
| 550 |
storage_account_key=dict(type='str', required=True, no_log=True) |
| 551 |
) |
| 552 |
|
| 553 |
|
| 554 |
volumes_spec = dict( |
| 555 |
name=dict(type='str', required=True), |
| 556 |
azure_file=dict(type='dict', options=azure_file_volume_spec), |
| 557 |
empty_dir=dict(type='dict'), |
| 558 |
secret=dict(type='dict', no_log=True), |
| 559 |
git_repo=dict(type='dict', options=git_repo_volume_spec) |
| 560 |
) |
| 561 |
|
| 562 |
|
| 563 |
user_assigned_identities_spec = dict( |
| 564 |
id=dict( |
| 565 |
type='list', |
| 566 |
default=[], |
| 567 |
elements='str' |
| 568 |
), |
| 569 |
) |
| 570 |
|
| 571 |
managed_identity_spec = dict( |
| 572 |
type=dict( |
| 573 |
type='str', |
| 574 |
choices=['SystemAssigned', |
| 575 |
'UserAssigned', |
| 576 |
'SystemAssigned, UserAssigned'], |
| 577 |
default='None' |
| 578 |
), |
| 579 |
user_assigned_identities=dict( |
| 580 |
type='dict', |
| 581 |
options=user_assigned_identities_spec, |
| 582 |
default={} |
| 583 |
), |
| 584 |
) |
| 585 |
|
| 586 |
|
| 587 |
class AzureRMContainerInstance(AzureRMModuleBaseExt): |
| 588 |
"""Configuration class for an Azure RM container instance resource""" |
| 589 |
|
| 590 |
def __init__(self): |
| 591 |
self.module_arg_spec = dict( |
| 592 |
resource_group=dict( |
| 593 |
type='str', |
| 594 |
required=True |
| 595 |
), |
| 596 |
name=dict( |
| 597 |
type='str', |
| 598 |
required=True |
| 599 |
), |
| 600 |
os_type=dict( |
| 601 |
type='str', |
| 602 |
default='linux', |
| 603 |
choices=['linux', 'windows', 'Linux', 'Windows'] |
| 604 |
), |
| 605 |
state=dict( |
| 606 |
type='str', |
| 607 |
default='present', |
| 608 |
choices=['present', 'absent'] |
| 609 |
), |
| 610 |
location=dict( |
| 611 |
type='str', |
| 612 |
), |
| 613 |
ip_address=dict( |
| 614 |
type='str', |
| 615 |
default='none', |
| 616 |
choices=['public', 'none', 'private'] |
| 617 |
), |
| 618 |
dns_name_label=dict( |
| 619 |
type='str', |
| 620 |
), |
| 621 |
ports=dict( |
| 622 |
type='list', |
| 623 |
elements='int', |
| 624 |
default=[] |
| 625 |
), |
| 626 |
registry_login_server=dict( |
| 627 |
type='str', |
| 628 |
default=None |
| 629 |
), |
| 630 |
registry_username=dict( |
| 631 |
type='str', |
| 632 |
default=None |
| 633 |
), |
| 634 |
registry_password=dict( |
| 635 |
type='str', |
| 636 |
default=None, |
| 637 |
no_log=True |
| 638 |
), |
| 639 |
acr_identity=dict( |
| 640 |
type='str', |
| 641 |
default=None, |
| 642 |
), |
| 643 |
containers=dict( |
| 644 |
type='list', |
| 645 |
elements='dict', |
| 646 |
options=container_spec |
| 647 |
), |
| 648 |
restart_policy=dict( |
| 649 |
type='str', |
| 650 |
choices=['always', 'on_failure', 'never'] |
| 651 |
), |
| 652 |
force_update=dict( |
| 653 |
type='bool', |
| 654 |
default=False |
| 655 |
), |
| 656 |
volumes=dict( |
| 657 |
type='list', |
| 658 |
elements='dict', |
| 659 |
options=volumes_spec |
| 660 |
), |
| 661 |
subnet_ids=dict( |
| 662 |
type='list', |
| 663 |
elements='str', |
| 664 |
), |
| 665 |
identity=dict( |
| 666 |
type='dict', |
| 667 |
options=managed_identity_spec |
| 668 |
), |
| 669 |
) |
| 670 |
|
| 671 |
self.resource_group = None |
| 672 |
self.name = None |
| 673 |
self.location = None |
| 674 |
self.state = None |
| 675 |
self.ip_address = None |
| 676 |
self.dns_name_label = None |
| 677 |
self.containers = None |
| 678 |
self.restart_policy = None |
| 679 |
self.subnet_ids = None |
| 680 |
self.identity = None |
| 681 |
|
| 682 |
self.tags = None |
| 683 |
|
| 684 |
self.results = dict(changed=False, state=dict()) |
| 685 |
self.cgmodels = None |
| 686 |
self._managed_identity = None |
| 687 |
|
| 688 |
required_if = [ |
| 689 |
('state', 'present', ['containers']), ('ip_address', 'private', ['subnet_ids']) |
| 690 |
] |
| 691 |
|
| 692 |
super(AzureRMContainerInstance, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 693 |
supports_check_mode=True, |
| 694 |
supports_tags=True, |
| 695 |
required_if=required_if) |
| 696 |
|
| 697 |
@property |
| 698 |
def managed_identity(self): |
| 699 |
if not self._managed_identity: |
| 700 |
self._managed_identity = {"identity": ContainerGroupIdentity, |
| 701 |
"user_assigned": dict |
| 702 |
} |
| 703 |
return self._managed_identity |
| 704 |
|
| 705 |
def exec_module(self, **kwargs): |
| 706 |
"""Main module execution method""" |
| 707 |
|
| 708 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 709 |
setattr(self, key, kwargs[key]) |
| 710 |
|
| 711 |
resource_group = None |
| 712 |
response = None |
| 713 |
results = dict() |
| 714 |
|
| 715 |
|
| 716 |
self.cgmodels = self.containerinstance_client.container_groups.models |
| 717 |
|
| 718 |
resource_group = self.get_resource_group(self.resource_group) |
| 719 |
|
| 720 |
if not self.location: |
| 721 |
self.location = resource_group.location |
| 722 |
|
| 723 |
response = self.get_containerinstance() |
| 724 |
|
| 725 |
|
| 726 |
if self.identity: |
| 727 |
update_identity, self.identity = self.update_managed_identity(new_identity=self.identity) |
| 728 |
|
| 729 |
if not response: |
| 730 |
self.log("Container Group doesn't exist") |
| 731 |
|
| 732 |
if self.state == 'absent': |
| 733 |
self.log("Nothing to delete") |
| 734 |
else: |
| 735 |
self.force_update = True |
| 736 |
else: |
| 737 |
self.log("Container instance already exists") |
| 738 |
|
| 739 |
if self.state == 'absent': |
| 740 |
if not self.check_mode: |
| 741 |
self.delete_containerinstance() |
| 742 |
self.results['changed'] = True |
| 743 |
self.log("Container instance deleted") |
| 744 |
elif self.state == 'present': |
| 745 |
self.log("Need to check if container group has to be deleted or may be updated") |
| 746 |
update_tags, newtags = self.update_tags(response.get('tags', dict())) |
| 747 |
|
| 748 |
if self.force_update: |
| 749 |
self.log('Deleting container instance before update') |
| 750 |
if not self.check_mode: |
| 751 |
self.delete_containerinstance() |
| 752 |
elif update_tags: |
| 753 |
if not self.check_mode: |
| 754 |
self.tags = newtags |
| 755 |
self.results['changed'] = True |
| 756 |
response = self.update_containerinstance() |
| 757 |
|
| 758 |
if self.state == 'present': |
| 759 |
|
| 760 |
self.log("Need to Create / Update the container instance") |
| 761 |
|
| 762 |
if self.force_update: |
| 763 |
self.results['changed'] = True |
| 764 |
if self.check_mode: |
| 765 |
return self.results |
| 766 |
response = self.create_update_containerinstance() |
| 767 |
|
| 768 |
self.results['id'] = response['id'] |
| 769 |
self.results['provisioning_state'] = response['provisioning_state'] |
| 770 |
self.results['ip_address'] = response['ip_address']['ip'] if 'ip_address' in response else '' |
| 771 |
|
| 772 |
self.log("Creation / Update done") |
| 773 |
|
| 774 |
return self.results |
| 775 |
|
| 776 |
def update_containerinstance(self): |
| 777 |
''' |
| 778 |
Updates a container service with the specified configuration of orchestrator, masters, and agents. |
| 779 |
|
| 780 |
:return: deserialized container instance state dictionary |
| 781 |
''' |
| 782 |
try: |
| 783 |
response = self.containerinstance_client.container_groups.update(resource_group_name=self.resource_group, |
| 784 |
container_group_name=self.name, |
| 785 |
resource=dict(tags=self.tags)) |
| 786 |
if isinstance(response, LROPoller): |
| 787 |
response = self.get_poller_result(response) |
| 788 |
except Exception as exc: |
| 789 |
self.fail("Error when Updating ACI {0}: {1}".format(self.name, exc.message or str(exc))) |
| 790 |
|
| 791 |
return response.as_dict() |
| 792 |
|
| 793 |
def create_update_containerinstance(self): |
| 794 |
''' |
| 795 |
Creates or updates a container service with the specified configuration of orchestrator, masters, and agents. |
| 796 |
|
| 797 |
:return: deserialized container instance state dictionary |
| 798 |
''' |
| 799 |
self.log("Creating / Updating the container instance {0}".format(self.name)) |
| 800 |
|
| 801 |
registry_credentials = None |
| 802 |
|
| 803 |
if self.registry_login_server is not None: |
| 804 |
registry_credentials = [self.cgmodels.ImageRegistryCredential(server=self.registry_login_server, |
| 805 |
username=self.registry_username, |
| 806 |
password=self.registry_password, |
| 807 |
identity=self.acr_identity)] |
| 808 |
|
| 809 |
ip_address = None |
| 810 |
|
| 811 |
containers = [] |
| 812 |
all_ports = set([]) |
| 813 |
for container_def in self.containers: |
| 814 |
name = container_def.get("name") |
| 815 |
image = container_def.get("image") |
| 816 |
memory = container_def.get("memory") |
| 817 |
cpu = container_def.get("cpu") |
| 818 |
commands = container_def.get("commands") |
| 819 |
ports = [] |
| 820 |
variables = [] |
| 821 |
volume_mounts = [] |
| 822 |
|
| 823 |
port_list = container_def.get("ports") |
| 824 |
if port_list: |
| 825 |
for port in port_list: |
| 826 |
all_ports.add(port) |
| 827 |
ports.append(self.cgmodels.ContainerPort(port=port)) |
| 828 |
|
| 829 |
variable_list = container_def.get("environment_variables") |
| 830 |
if variable_list: |
| 831 |
for variable in variable_list: |
| 832 |
variables.append(self.cgmodels.EnvironmentVariable(name=variable.get('name'), |
| 833 |
value=variable.get('value') if not variable.get('is_secure') else None, |
| 834 |
secure_value=variable.get('value') if variable.get('is_secure') else None)) |
| 835 |
|
| 836 |
volume_mounts_list = container_def.get("volume_mounts") |
| 837 |
if volume_mounts_list: |
| 838 |
for volume_mount in volume_mounts_list: |
| 839 |
volume_mounts.append(self.cgmodels.VolumeMount(name=volume_mount.get('name'), |
| 840 |
mount_path=volume_mount.get('mount_path'), |
| 841 |
read_only=volume_mount.get('read_only'))) |
| 842 |
|
| 843 |
containers.append(self.cgmodels.Container(name=name, |
| 844 |
image=image, |
| 845 |
resources=self.cgmodels.ResourceRequirements( |
| 846 |
requests=self.cgmodels.ResourceRequests(memory_in_gb=memory, cpu=cpu) |
| 847 |
), |
| 848 |
ports=ports, |
| 849 |
command=commands, |
| 850 |
environment_variables=variables, |
| 851 |
volume_mounts=volume_mounts)) |
| 852 |
|
| 853 |
if self.ip_address is not None: |
| 854 |
|
| 855 |
if len(all_ports) > 0: |
| 856 |
ports = [] |
| 857 |
for port in all_ports: |
| 858 |
ports.append(self.cgmodels.Port(port=port, protocol="TCP")) |
| 859 |
ip_address = self.cgmodels.IpAddress(ports=ports, dns_name_label=self.dns_name_label, type=self.ip_address) |
| 860 |
|
| 861 |
subnet_ids = None |
| 862 |
if self.subnet_ids is not None: |
| 863 |
subnet_ids = [self.cgmodels.ContainerGroupSubnetId(id=item) for item in self.subnet_ids] |
| 864 |
|
| 865 |
parameters = self.cgmodels.ContainerGroup(location=self.location, |
| 866 |
identity=self.identity, |
| 867 |
containers=containers, |
| 868 |
image_registry_credentials=registry_credentials, |
| 869 |
restart_policy=_snake_to_camel(self.restart_policy, True) if self.restart_policy else None, |
| 870 |
ip_address=ip_address, |
| 871 |
os_type=self.os_type, |
| 872 |
subnet_ids=subnet_ids, |
| 873 |
volumes=self.volumes, |
| 874 |
tags=self.tags) |
| 875 |
|
| 876 |
try: |
| 877 |
response = self.containerinstance_client.container_groups.begin_create_or_update(resource_group_name=self.resource_group, |
| 878 |
container_group_name=self.name, |
| 879 |
container_group=parameters) |
| 880 |
if isinstance(response, LROPoller): |
| 881 |
response = self.get_poller_result(response) |
| 882 |
except Exception as exc: |
| 883 |
self.fail("Error when creating ACI {0}: {1}".format(self.name, exc.message or str(exc))) |
| 884 |
|
| 885 |
return response.as_dict() |
| 886 |
|
| 887 |
def delete_containerinstance(self): |
| 888 |
''' |
| 889 |
Deletes the specified container group instance in the specified subscription and resource group. |
| 890 |
|
| 891 |
:return: True |
| 892 |
''' |
| 893 |
self.log("Deleting the container instance {0}".format(self.name)) |
| 894 |
try: |
| 895 |
response = self.containerinstance_client.container_groups.begin_delete(resource_group_name=self.resource_group, container_group_name=self.name) |
| 896 |
return True |
| 897 |
except Exception as exc: |
| 898 |
self.fail('Error when deleting ACI {0}: {1}'.format(self.name, exc.message or str(exc))) |
| 899 |
return False |
| 900 |
|
| 901 |
def get_containerinstance(self): |
| 902 |
''' |
| 903 |
Gets the properties of the specified container service. |
| 904 |
|
| 905 |
:return: deserialized container instance state dictionary |
| 906 |
''' |
| 907 |
self.log("Checking if the container instance {0} is present".format(self.name)) |
| 908 |
found = False |
| 909 |
try: |
| 910 |
response = self.containerinstance_client.container_groups.get(resource_group_name=self.resource_group, container_group_name=self.name) |
| 911 |
found = True |
| 912 |
self.log("Response : {0}".format(response)) |
| 913 |
self.log("Container instance : {0} found".format(response.name)) |
| 914 |
except ResourceNotFoundError as e: |
| 915 |
self.log('Did not find the container instance.') |
| 916 |
if found is True: |
| 917 |
return response.as_dict() |
| 918 |
|
| 919 |
return False |
| 920 |
|
| 921 |
|
| 922 |
def main(): |
| 923 |
"""Main execution""" |
| 924 |
AzureRMContainerInstance() |
| 925 |
|
| 926 |
|
| 927 |
if __name__ == '__main__': |
| 928 |
main() |