| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_virtualmachinescaleset_info |
| 13 |
|
| 14 |
version_added: "0.1.2" |
| 15 |
|
| 16 |
short_description: Get Virtual Machine Scale Set facts |
| 17 |
|
| 18 |
description: |
| 19 |
- Get facts for a virtual machine scale set. |
| 20 |
- Note that this module was called M(azure.azcollection.azure_rm_virtualmachine_scaleset_facts) before Ansible 2.8. The usage did not change. |
| 21 |
|
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Limit results to a specific virtual machine scale set. |
| 26 |
type: str |
| 27 |
resource_group: |
| 28 |
description: |
| 29 |
- The resource group to search for the desired virtual machine scale set. |
| 30 |
type: str |
| 31 |
tags: |
| 32 |
description: |
| 33 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
format: |
| 37 |
description: |
| 38 |
- Format of the data returned. |
| 39 |
- If C(raw) is selected information will be returned in raw format from Azure Python SDK. |
| 40 |
- If C(curated) is selected the structure will be identical to input parameters of M(azure.azcollection.azure_rm_virtualmachinescaleset) module. |
| 41 |
- In Ansible 2.5 and lower facts are always returned in raw format. |
| 42 |
- Please note that this option will be deprecated in 2.10 when curated format will become the only supported format. |
| 43 |
default: 'raw' |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- 'curated' |
| 47 |
- 'raw' |
| 48 |
|
| 49 |
extends_documentation_fragment: |
| 50 |
- azure.azcollection.azure |
| 51 |
|
| 52 |
author: |
| 53 |
- Sertac Ozercan (@sozercan) |
| 54 |
''' |
| 55 |
|
| 56 |
EXAMPLES = ''' |
| 57 |
- name: Get facts for a virtual machine scale set |
| 58 |
azure_rm_virtualmachinescaleset_info: |
| 59 |
resource_group: myResourceGroup |
| 60 |
name: testvmss001 |
| 61 |
format: curated |
| 62 |
|
| 63 |
- name: Get facts for all virtual networks |
| 64 |
azure_rm_virtualmachinescaleset_info: |
| 65 |
resource_group: myResourceGroup |
| 66 |
|
| 67 |
- name: Get facts by tags |
| 68 |
azure_rm_virtualmachinescaleset_info: |
| 69 |
resource_group: myResourceGroup |
| 70 |
tags: |
| 71 |
- testing |
| 72 |
''' |
| 73 |
|
| 74 |
RETURN = ''' |
| 75 |
vmss: |
| 76 |
description: |
| 77 |
- List of virtual machine scale sets. |
| 78 |
returned: always |
| 79 |
type: list |
| 80 |
sample: [{ |
| 81 |
"constrained_maximum_capacity": false, |
| 82 |
"etag": "3", |
| 83 |
"id": "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG02/providers/Microsoft.Compute/virtualMachineScaleSets/testVMSStestvmss", |
| 84 |
"location": "eastus", |
| 85 |
"name": "testVMSStestvmss", |
| 86 |
"orchestration_mode": "Flexible", |
| 87 |
"platform_fault_domain_count": 1, |
| 88 |
"provisioning_state": "Succeeded", |
| 89 |
"single_placement_group": false, |
| 90 |
"sku": { |
| 91 |
"capacity": 1, |
| 92 |
"name": "Standard_A1_v2", |
| 93 |
"tier": "Standard" |
| 94 |
}, |
| 95 |
"tags": { |
| 96 |
"key2": "value2", |
| 97 |
"key3": "value3" |
| 98 |
}, |
| 99 |
"time_created": "2025-04-22T08:03:29.427822Z", |
| 100 |
"type": "Microsoft.Compute/virtualMachineScaleSets", |
| 101 |
"unique_id": "da4393f5-6060-4a0e-8ae5-7616316402b8", |
| 102 |
"upgrade_policy": { |
| 103 |
"mode": "Manual" |
| 104 |
}, |
| 105 |
"virtual_machine_profile": { |
| 106 |
"network_profile": { |
| 107 |
"network_api_version": "2020-11-01", |
| 108 |
"network_interface_configurations": [ |
| 109 |
{ |
| 110 |
"auxiliary_mode": "None", |
| 111 |
"auxiliary_sku": "None", |
| 112 |
"delete_option": "Delete", |
| 113 |
"disable_tcp_state_tracking": false, |
| 114 |
"dns_settings": { |
| 115 |
"dns_servers": [] |
| 116 |
}, |
| 117 |
"enable_ip_forwarding": false, |
| 118 |
"ip_configurations": [ |
| 119 |
{ |
| 120 |
"application_gateway_backend_address_pools": [], |
| 121 |
"application_security_groups": [ |
| 122 |
{ |
| 123 |
"id": "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG02/providers/Microsoft.Network/applicationSecurityGroups/apptestvmss02" |
| 124 |
}, |
| 125 |
{ |
| 126 |
"id": "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG02/providers/Microsoft.Network/applicationSecurityGroups/apptestvmss" |
| 127 |
} |
| 128 |
], |
| 129 |
"load_balancer_backend_address_pools": [], |
| 130 |
"name": "default", |
| 131 |
"primary": true, |
| 132 |
"private_ip_address_version": "IPv4", |
| 133 |
"public_ip_address_configuration": { |
| 134 |
"idle_timeout_in_minutes": 4, |
| 135 |
"ip_tags": [], |
| 136 |
"name": "instancepublicip", |
| 137 |
"public_ip_address_version": "IPv4" |
| 138 |
}, |
| 139 |
"subnet": { |
| 140 |
"id": "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG02/providers/Microsoft.Network/virtualNetworks/VMSStestVnet/subnets/VMSStestSubnet" |
| 141 |
} |
| 142 |
} |
| 143 |
], |
| 144 |
"name": "testVMSStestvmss", |
| 145 |
"primary": true |
| 146 |
} |
| 147 |
] |
| 148 |
}, |
| 149 |
"os_profile": { |
| 150 |
"admin_username": "testuser", |
| 151 |
"allow_extension_operations": true, |
| 152 |
"computer_name_prefix": "testVMSStestvmss", |
| 153 |
"linux_configuration": { |
| 154 |
"disable_password_authentication": true, |
| 155 |
"patch_settings": { |
| 156 |
"assessment_mode": "ImageDefault", |
| 157 |
"patch_mode": "ImageDefault" |
| 158 |
}, |
| 159 |
"provision_vm_agent": true, |
| 160 |
"ssh": { |
| 161 |
"public_keys": [ |
| 162 |
{ |
| 163 |
"key_data": "ssh-rsa xxxxxxxxxx [email protected]", |
| 164 |
"path": "/home/testuser/.ssh/authorized_keys" |
| 165 |
} |
| 166 |
] |
| 167 |
} |
| 168 |
}, |
| 169 |
"require_guest_provision_signal": true, |
| 170 |
"secrets": [] |
| 171 |
}, |
| 172 |
"storage_profile": { |
| 173 |
"data_disks": [ |
| 174 |
{ |
| 175 |
"caching": "ReadWrite", |
| 176 |
"create_option": "Empty", |
| 177 |
"delete_option": "Delete", |
| 178 |
"disk_size_gb": 64, |
| 179 |
"lun": 0, |
| 180 |
"managed_disk": { |
| 181 |
"storage_account_type": "Standard_LRS" |
| 182 |
} |
| 183 |
} |
| 184 |
], |
| 185 |
"image_reference": { |
| 186 |
"offer": "0001-com-ubuntu-server-focal", |
| 187 |
"publisher": "Canonical", |
| 188 |
"sku": "20_04-lts", |
| 189 |
"version": "20.04.202504030" |
| 190 |
}, |
| 191 |
"os_disk": { |
| 192 |
"caching": "ReadWrite", |
| 193 |
"create_option": "FromImage", |
| 194 |
"delete_option": "Delete", |
| 195 |
"disk_size_gb": 30, |
| 196 |
"managed_disk": { |
| 197 |
"storage_account_type": "Standard_LRS" |
| 198 |
}, |
| 199 |
"os_type": "Linux" |
| 200 |
} |
| 201 |
}, |
| 202 |
"time_created": "2025-04-22T08:53:17.201851Z" |
| 203 |
} |
| 204 |
}] |
| 205 |
''' |
| 206 |
|
| 207 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 208 |
import re |
| 209 |
|
| 210 |
try: |
| 211 |
from azure.core.exceptions import ResourceNotFoundError |
| 212 |
except Exception: |
| 213 |
|
| 214 |
pass |
| 215 |
|
| 216 |
AZURE_OBJECT_CLASS = 'VirtualMachineScaleSet' |
| 217 |
|
| 218 |
AZURE_ENUM_MODULES = ['azure.mgmt.compute.models'] |
| 219 |
|
| 220 |
|
| 221 |
class AzureRMVirtualMachineScaleSetInfo(AzureRMModuleBase): |
| 222 |
"""Utility class to get virtual machine scale set facts""" |
| 223 |
|
| 224 |
def __init__(self): |
| 225 |
|
| 226 |
self.module_args = dict( |
| 227 |
name=dict(type='str'), |
| 228 |
resource_group=dict(type='str'), |
| 229 |
tags=dict(type='list', elements='str'), |
| 230 |
format=dict( |
| 231 |
type='str', |
| 232 |
choices=['curated', |
| 233 |
'raw'], |
| 234 |
default='raw' |
| 235 |
) |
| 236 |
) |
| 237 |
|
| 238 |
self.results = dict( |
| 239 |
changed=False, |
| 240 |
) |
| 241 |
|
| 242 |
self.name = None |
| 243 |
self.resource_group = None |
| 244 |
self.format = None |
| 245 |
self.tags = None |
| 246 |
|
| 247 |
super(AzureRMVirtualMachineScaleSetInfo, self).__init__( |
| 248 |
derived_arg_spec=self.module_args, |
| 249 |
supports_check_mode=True, |
| 250 |
supports_tags=False, |
| 251 |
facts_module=True |
| 252 |
) |
| 253 |
|
| 254 |
def exec_module(self, **kwargs): |
| 255 |
|
| 256 |
for key in self.module_args: |
| 257 |
setattr(self, key, kwargs[key]) |
| 258 |
|
| 259 |
if self.name and not self.resource_group: |
| 260 |
self.fail("Parameter error: resource group required when filtering by name.") |
| 261 |
|
| 262 |
if self.name: |
| 263 |
result = self.get_item() |
| 264 |
else: |
| 265 |
result = self.list_items() |
| 266 |
|
| 267 |
if self.format == 'curated': |
| 268 |
for index in range(len(result)): |
| 269 |
vmss = result[index] |
| 270 |
subnet_name = None |
| 271 |
load_balancer_name = None |
| 272 |
virtual_network_name = None |
| 273 |
ssh_password_enabled = False |
| 274 |
|
| 275 |
try: |
| 276 |
subnet_id = (vmss['virtual_machine_profile']['network_profile']['network_interface_configurations'][0] |
| 277 |
['ipConfigurations'][0]['subnet']['id']) |
| 278 |
subnet_name = re.sub('.*subnets\\/', '', subnet_id) |
| 279 |
except Exception: |
| 280 |
self.log('Could not extract subnet name') |
| 281 |
|
| 282 |
try: |
| 283 |
backend_address_pool_id = (vmss['virtual_machine_profile']['network_profile']['network_interface_configurations'][0] |
| 284 |
['ip_configurations'][0]['load_balancer_backend_address_pools'][0]['id']) |
| 285 |
load_balancer_name = re.sub('\\/backendAddressPools.*', '', re.sub('.*loadBalancers\\/', '', backend_address_pool_id)) |
| 286 |
virtual_network_name = re.sub('.*virtualNetworks\\/', '', re.sub('\\/subnets.*', '', subnet_id)) |
| 287 |
except Exception: |
| 288 |
self.log('Could not extract load balancer / virtual network name') |
| 289 |
|
| 290 |
try: |
| 291 |
ssh_password_enabled = (not vmss['virtual_machine_profile']['os_profile'] |
| 292 |
['linux_configuration']['disable_password_authentication']) |
| 293 |
except Exception: |
| 294 |
self.log('Could not extract SSH password enabled') |
| 295 |
|
| 296 |
data_disks = vmss['virtual_machine_profile']['storage_profile'].get('data_disks', []) |
| 297 |
|
| 298 |
for disk_index in range(len(data_disks)): |
| 299 |
old_disk = data_disks[disk_index] |
| 300 |
new_disk = { |
| 301 |
'lun': old_disk['lun'], |
| 302 |
'disk_size_gb': old_disk['disk_size_gb'], |
| 303 |
'managed_disk_type': old_disk['managed_disk']['storage_account_type'], |
| 304 |
'caching': old_disk['caching'] |
| 305 |
} |
| 306 |
data_disks[disk_index] = new_disk |
| 307 |
|
| 308 |
updated = { |
| 309 |
'id': vmss['id'], |
| 310 |
'resource_group': self.resource_group, |
| 311 |
'name': vmss['name'], |
| 312 |
'state': 'present', |
| 313 |
'location': vmss['location'], |
| 314 |
'vm_size': vmss['sku']['name'], |
| 315 |
'capacity': vmss['sku']['capacity'], |
| 316 |
'tier': vmss['sku']['tier'], |
| 317 |
'upgrade_policy': vmss.get('upgrade_policy'), |
| 318 |
'orchestrationMode': vmss.get('orchestration_mode'), |
| 319 |
'platformFaultDomainCount': vmss.get('platform_fault_domain_count'), |
| 320 |
'admin_username': vmss['virtual_machine_profile']['os_profile']['admin_username'], |
| 321 |
'admin_password': vmss['virtual_machine_profile']['os_profile'].get('admin_password'), |
| 322 |
'ssh_password_enabled': ssh_password_enabled, |
| 323 |
'image': vmss['virtual_machine_profile']['storage_profile']['image_reference'], |
| 324 |
'os_disk_caching': vmss['virtual_machine_profile']['storage_profile']['os_disk']['caching'], |
| 325 |
'os_type': 'Linux' if (vmss['virtual_machine_profile']['os_profile'].get('linux_configuration') is not None) else 'Windows', |
| 326 |
'overprovision': vmss.get('overprovision'), |
| 327 |
'managed_disk_type': vmss['virtual_machine_profile']['storage_profile']['os_disk']['managed_disk']['storage_account_type'], |
| 328 |
'data_disks': data_disks, |
| 329 |
'virtual_network_name': virtual_network_name, |
| 330 |
'subnet_name': subnet_name, |
| 331 |
'load_balancer': load_balancer_name, |
| 332 |
'identity': vmss.get('identity', None), |
| 333 |
'tags': vmss.get('tags') |
| 334 |
} |
| 335 |
|
| 336 |
result[index] = updated |
| 337 |
|
| 338 |
self.results['vmss'] = result |
| 339 |
|
| 340 |
return self.results |
| 341 |
|
| 342 |
def get_item(self): |
| 343 |
"""Get a single virtual machine scale set""" |
| 344 |
|
| 345 |
self.log('Get properties for {0}'.format(self.name)) |
| 346 |
|
| 347 |
item = None |
| 348 |
results = [] |
| 349 |
|
| 350 |
try: |
| 351 |
item = self.compute_client.virtual_machine_scale_sets.get(self.resource_group, self.name) |
| 352 |
except ResourceNotFoundError: |
| 353 |
pass |
| 354 |
|
| 355 |
if item and self.has_tags(item.tags, self.tags): |
| 356 |
results = [self.serialize_obj(item, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES)] |
| 357 |
|
| 358 |
return results |
| 359 |
|
| 360 |
def list_items(self): |
| 361 |
"""Get all virtual machine scale sets""" |
| 362 |
|
| 363 |
self.log('List all virtual machine scale sets') |
| 364 |
|
| 365 |
try: |
| 366 |
response = self.compute_client.virtual_machine_scale_sets.list(self.resource_group) |
| 367 |
except ResourceNotFoundError as exc: |
| 368 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 369 |
|
| 370 |
results = [] |
| 371 |
for item in response: |
| 372 |
if self.has_tags(item.tags, self.tags): |
| 373 |
results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES)) |
| 374 |
|
| 375 |
return results |
| 376 |
|
| 377 |
|
| 378 |
def main(): |
| 379 |
"""Main module execution code path""" |
| 380 |
|
| 381 |
AzureRMVirtualMachineScaleSetInfo() |
| 382 |
|
| 383 |
|
| 384 |
if __name__ == '__main__': |
| 385 |
main() |