| 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_resource_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Generic facts of Azure resources |
| 16 |
description: |
| 17 |
- Obtain facts of any resource using Azure REST API. |
| 18 |
- This module gives access to resources that are not supported via Ansible modules. |
| 19 |
- Refer to U(https://docs.microsoft.com/en-us/rest/api/) regarding details related to specific resource REST API. |
| 20 |
|
| 21 |
options: |
| 22 |
url: |
| 23 |
description: |
| 24 |
- Azure RM Resource URL. |
| 25 |
type: str |
| 26 |
api_version: |
| 27 |
description: |
| 28 |
- Specific API version to be used. |
| 29 |
type: str |
| 30 |
provider: |
| 31 |
description: |
| 32 |
- Provider type, should be specified in no URL is given. |
| 33 |
type: str |
| 34 |
resource_group: |
| 35 |
description: |
| 36 |
- Resource group to be used. |
| 37 |
- Required if URL is not specified. |
| 38 |
type: str |
| 39 |
resource_type: |
| 40 |
description: |
| 41 |
- Resource type. |
| 42 |
type: str |
| 43 |
resource_name: |
| 44 |
description: |
| 45 |
- Resource name. |
| 46 |
type: str |
| 47 |
method: |
| 48 |
description: |
| 49 |
- The HTTP method of the request or response. It must be uppercase. |
| 50 |
type: str |
| 51 |
choices: |
| 52 |
- GET |
| 53 |
- PUT |
| 54 |
- POST |
| 55 |
- HEAD |
| 56 |
- PATCH |
| 57 |
- DELETE |
| 58 |
- MERGE |
| 59 |
default: "GET" |
| 60 |
subresource: |
| 61 |
description: |
| 62 |
- List of subresources. |
| 63 |
type: list |
| 64 |
elements: dict |
| 65 |
default: [] |
| 66 |
suboptions: |
| 67 |
namespace: |
| 68 |
description: |
| 69 |
- Subresource namespace. |
| 70 |
type: str |
| 71 |
type: |
| 72 |
description: |
| 73 |
- Subresource type. |
| 74 |
type: str |
| 75 |
name: |
| 76 |
description: |
| 77 |
- Subresource name. |
| 78 |
type: str |
| 79 |
tags: |
| 80 |
description: |
| 81 |
- A dictionary of tags to filter on. |
| 82 |
- Each key-value pair in the dictionary specifies a tag name and its value to filter on differente resources. |
| 83 |
type: dict |
| 84 |
required: false |
| 85 |
default: {} |
| 86 |
|
| 87 |
extends_documentation_fragment: |
| 88 |
- azure.azcollection.azure |
| 89 |
|
| 90 |
author: |
| 91 |
- Zim Kalinowski (@zikalino) |
| 92 |
|
| 93 |
''' |
| 94 |
|
| 95 |
EXAMPLES = ''' |
| 96 |
- name: Get scaleset info |
| 97 |
azure_rm_resource_info: |
| 98 |
resource_group: myResourceGroup |
| 99 |
provider: compute |
| 100 |
resource_type: virtualmachinescalesets |
| 101 |
resource_name: myVmss |
| 102 |
api_version: "2017-12-01" |
| 103 |
|
| 104 |
- name: Query all the resources in the resource group |
| 105 |
azure_rm_resource_info: |
| 106 |
resource_group: "{{ resource_group }}" |
| 107 |
resource_type: resources |
| 108 |
|
| 109 |
- name: Get all snapshots of all resource groups of a subscription but filtering with two tags. |
| 110 |
azure_rm_resource_info: |
| 111 |
provider: compute |
| 112 |
resource_type: snapshots |
| 113 |
tags: |
| 114 |
enviroment: dev |
| 115 |
department: hr |
| 116 |
''' |
| 117 |
|
| 118 |
RETURN = ''' |
| 119 |
response: |
| 120 |
description: |
| 121 |
- Response specific to resource type. |
| 122 |
returned: always |
| 123 |
type: complex |
| 124 |
contains: |
| 125 |
id: |
| 126 |
description: |
| 127 |
- Id of the Azure resource. |
| 128 |
type: str |
| 129 |
returned: always |
| 130 |
sample: "/subscriptions/xxxx...xxxx/resourceGroups/v-xisuRG/providers/Microsoft.Compute/virtualMachines/myVM" |
| 131 |
location: |
| 132 |
description: |
| 133 |
- Resource location. |
| 134 |
type: str |
| 135 |
returned: always |
| 136 |
sample: eastus |
| 137 |
name: |
| 138 |
description: |
| 139 |
- Resource name. |
| 140 |
type: str |
| 141 |
returned: always |
| 142 |
sample: myVM |
| 143 |
properties: |
| 144 |
description: |
| 145 |
- Specifies the virtual machine's property. |
| 146 |
type: complex |
| 147 |
returned: always |
| 148 |
contains: |
| 149 |
diagnosticsProfile: |
| 150 |
description: |
| 151 |
- Specifies the boot diagnostic settings state. |
| 152 |
type: complex |
| 153 |
returned: always |
| 154 |
contains: |
| 155 |
bootDiagnostics: |
| 156 |
description: |
| 157 |
- A debugging feature, which to view Console Output and Screenshot to diagnose VM status. |
| 158 |
type: dict |
| 159 |
returned: always |
| 160 |
sample: { |
| 161 |
"enabled": true, |
| 162 |
"storageUri": "https://vxisurgdiag.blob.core.windows.net/" |
| 163 |
} |
| 164 |
hardwareProfile: |
| 165 |
description: |
| 166 |
- Specifies the hardware settings for the virtual machine. |
| 167 |
type: dict |
| 168 |
returned: always |
| 169 |
sample: { |
| 170 |
"vmSize": "Standard_D2s_v3" |
| 171 |
} |
| 172 |
networkProfile: |
| 173 |
description: |
| 174 |
- Specifies the network interfaces of the virtual machine. |
| 175 |
type: complex |
| 176 |
returned: always |
| 177 |
contains: |
| 178 |
networkInterfaces: |
| 179 |
description: |
| 180 |
- Describes a network interface reference. |
| 181 |
type: list |
| 182 |
returned: always |
| 183 |
sample: |
| 184 |
- { |
| 185 |
"id": "/subscriptions/xxxx...xxxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/networkInterfaces/myvm441" |
| 186 |
} |
| 187 |
osProfile: |
| 188 |
description: |
| 189 |
- Specifies the operating system settings for the virtual machine. |
| 190 |
type: complex |
| 191 |
returned: always |
| 192 |
contains: |
| 193 |
adminUsername: |
| 194 |
description: |
| 195 |
- Specifies the name of the administrator account. |
| 196 |
type: str |
| 197 |
returned: always |
| 198 |
sample: azureuser |
| 199 |
allowExtensionOperations: |
| 200 |
description: |
| 201 |
- Specifies whether extension operations should be allowed on the virtual machine. |
| 202 |
- This may only be set to False when no extensions are present on the virtual machine. |
| 203 |
type: bool |
| 204 |
returned: always |
| 205 |
sample: true |
| 206 |
computerName: |
| 207 |
description: |
| 208 |
- Specifies the host OS name of the virtual machine. |
| 209 |
type: str |
| 210 |
returned: always |
| 211 |
sample: myVM |
| 212 |
requireGuestProvisionSignale: |
| 213 |
description: |
| 214 |
- Specifies the host require guest provision signal or not. |
| 215 |
type: bool |
| 216 |
returned: always |
| 217 |
sample: true |
| 218 |
secrets: |
| 219 |
description: |
| 220 |
- Specifies set of certificates that should be installed onto the virtual machine. |
| 221 |
type: list |
| 222 |
returned: always |
| 223 |
sample: [] |
| 224 |
linuxConfiguration: |
| 225 |
description: |
| 226 |
- Specifies the Linux operating system settings on the virtual machine. |
| 227 |
type: dict |
| 228 |
returned: when OS type is Linux |
| 229 |
sample: { |
| 230 |
"disablePasswordAuthentication": false, |
| 231 |
"provisionVMAgent": true |
| 232 |
} |
| 233 |
provisioningState: |
| 234 |
description: |
| 235 |
- The provisioning state. |
| 236 |
type: str |
| 237 |
returned: always |
| 238 |
sample: Succeeded |
| 239 |
vmID: |
| 240 |
description: |
| 241 |
- Specifies the VM unique ID which is a 128-bits identifier that is encoded and stored in all Azure laaS VMs SMBIOS. |
| 242 |
- It can be read using platform BIOS commands. |
| 243 |
type: str |
| 244 |
returned: always |
| 245 |
sample: "eb86d9bb-6725-4787-a487-2e497d5b340c" |
| 246 |
storageProfile: |
| 247 |
description: |
| 248 |
- Specifies the storage account type for the managed disk. |
| 249 |
type: complex |
| 250 |
returned: always |
| 251 |
contains: |
| 252 |
dataDisks: |
| 253 |
description: |
| 254 |
- Specifies the parameters that are used to add a data disk to virtual machine. |
| 255 |
type: list |
| 256 |
returned: always |
| 257 |
sample: |
| 258 |
- { |
| 259 |
"caching": "None", |
| 260 |
"createOption": "Attach", |
| 261 |
"diskSizeGB": 1023, |
| 262 |
"lun": 2, |
| 263 |
"managedDisk": { |
| 264 |
"id": "/subscriptions/xxxx....xxxx/resourceGroups/V-XISURG/providers/Microsoft.Compute/disks/testdisk2", |
| 265 |
"storageAccountType": "StandardSSD_LRS" |
| 266 |
}, |
| 267 |
"name": "testdisk2" |
| 268 |
} |
| 269 |
- { |
| 270 |
"caching": "None", |
| 271 |
"createOption": "Attach", |
| 272 |
"diskSizeGB": 1023, |
| 273 |
"lun": 1, |
| 274 |
"managedDisk": { |
| 275 |
"id": "/subscriptions/xxxx...xxxx/resourceGroups/V-XISURG/providers/Microsoft.Compute/disks/testdisk3", |
| 276 |
"storageAccountType": "StandardSSD_LRS" |
| 277 |
}, |
| 278 |
"name": "testdisk3" |
| 279 |
} |
| 280 |
|
| 281 |
imageReference: |
| 282 |
description: |
| 283 |
- Specifies information about the image to use. |
| 284 |
type: dict |
| 285 |
returned: always |
| 286 |
sample: { |
| 287 |
"offer": "UbuntuServer", |
| 288 |
"publisher": "Canonical", |
| 289 |
"sku": "20_04-lts", |
| 290 |
"version": "latest" |
| 291 |
} |
| 292 |
osDisk: |
| 293 |
description: |
| 294 |
- Specifies information about the operating system disk used by the virtual machine. |
| 295 |
type: dict |
| 296 |
returned: always |
| 297 |
sample: { |
| 298 |
"caching": "ReadWrite", |
| 299 |
"createOption": "FromImage", |
| 300 |
"diskSizeGB": 30, |
| 301 |
"managedDisk": { |
| 302 |
"id": "/subscriptions/xxx...xxxx/resourceGroups/v-xisuRG/providers/Microsoft.Compute/disks/myVM_disk1_xxx", |
| 303 |
"storageAccountType": "Premium_LRS" |
| 304 |
}, |
| 305 |
"name": "myVM_disk1_xxx", |
| 306 |
"osType": "Linux" |
| 307 |
} |
| 308 |
type: |
| 309 |
description: |
| 310 |
- The type of identity used for the virtual machine. |
| 311 |
type: str |
| 312 |
returned: always |
| 313 |
sample: "Microsoft.Compute/virtualMachines" |
| 314 |
''' |
| 315 |
|
| 316 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 317 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 318 |
|
| 319 |
try: |
| 320 |
from azure.mgmt.core.tools import resource_id |
| 321 |
import json |
| 322 |
|
| 323 |
except ImportError: |
| 324 |
|
| 325 |
pass |
| 326 |
|
| 327 |
|
| 328 |
class AzureRMResourceInfo(AzureRMModuleBase): |
| 329 |
def __init__(self): |
| 330 |
|
| 331 |
self.module_arg_spec = dict( |
| 332 |
url=dict( |
| 333 |
type='str' |
| 334 |
), |
| 335 |
provider=dict( |
| 336 |
type='str' |
| 337 |
), |
| 338 |
resource_group=dict( |
| 339 |
type='str' |
| 340 |
), |
| 341 |
resource_type=dict( |
| 342 |
type='str' |
| 343 |
), |
| 344 |
resource_name=dict( |
| 345 |
type='str' |
| 346 |
), |
| 347 |
subresource=dict( |
| 348 |
type='list', |
| 349 |
elements='dict', |
| 350 |
default=[], |
| 351 |
options=dict( |
| 352 |
namespace=dict(type='str'), |
| 353 |
type=dict(type='str'), |
| 354 |
name=dict(type='str') |
| 355 |
) |
| 356 |
), |
| 357 |
method=dict( |
| 358 |
type='str', |
| 359 |
default='GET', |
| 360 |
choices=["GET", "PUT", "POST", "HEAD", "PATCH", "DELETE", "MERGE"] |
| 361 |
), |
| 362 |
api_version=dict( |
| 363 |
type='str' |
| 364 |
), |
| 365 |
tags=dict(type='dict', default={}) |
| 366 |
) |
| 367 |
|
| 368 |
self.results = dict( |
| 369 |
response=[] |
| 370 |
) |
| 371 |
self.mgmt_client = None |
| 372 |
self.url = None |
| 373 |
self.api_version = None |
| 374 |
self.provider = None |
| 375 |
self.resource_group = None |
| 376 |
self.resource_type = None |
| 377 |
self.resource_name = None |
| 378 |
self.subresource = [] |
| 379 |
super(AzureRMResourceInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) |
| 380 |
|
| 381 |
def exec_module(self, **kwargs): |
| 382 |
|
| 383 |
for key in self.module_arg_spec: |
| 384 |
setattr(self, key, kwargs[key]) |
| 385 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 386 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 387 |
|
| 388 |
if self.url is None: |
| 389 |
orphan = None |
| 390 |
rargs = dict() |
| 391 |
rargs['subscription'] = self.subscription_id |
| 392 |
rargs['resource_group'] = self.resource_group |
| 393 |
if not (self.provider is None or self.provider.lower().startswith('.microsoft')): |
| 394 |
rargs['namespace'] = "Microsoft." + self.provider |
| 395 |
else: |
| 396 |
rargs['namespace'] = self.provider |
| 397 |
|
| 398 |
if self.resource_type is not None and self.resource_name is not None: |
| 399 |
rargs['type'] = self.resource_type |
| 400 |
rargs['name'] = self.resource_name |
| 401 |
for i in range(len(self.subresource)): |
| 402 |
resource_ns = self.subresource[i].get('namespace', None) |
| 403 |
resource_type = self.subresource[i].get('type', None) |
| 404 |
resource_name = self.subresource[i].get('name', None) |
| 405 |
if resource_type is not None and resource_name is not None: |
| 406 |
rargs['child_namespace_' + str(i + 1)] = resource_ns |
| 407 |
rargs['child_type_' + str(i + 1)] = resource_type |
| 408 |
rargs['child_name_' + str(i + 1)] = resource_name |
| 409 |
else: |
| 410 |
orphan = resource_type |
| 411 |
else: |
| 412 |
orphan = self.resource_type |
| 413 |
|
| 414 |
self.url = resource_id(**rargs) |
| 415 |
|
| 416 |
if orphan is not None: |
| 417 |
self.url += '/' + orphan |
| 418 |
|
| 419 |
|
| 420 |
if not self.api_version: |
| 421 |
try: |
| 422 |
|
| 423 |
if "/providers/" in self.url: |
| 424 |
provider = self.url.split("/providers/")[1].split("/")[0] |
| 425 |
resourceType = self.url.split(provider + "/")[1].split("/")[0] |
| 426 |
url = "/subscriptions/" + self.subscription_id + "/providers/" + provider |
| 427 |
api_versions = json.loads(self.mgmt_client.query(url, self.method, {'api-version': '2015-01-01'}, None, None, [200], 0, 0).body()) |
| 428 |
for rt in api_versions['resourceTypes']: |
| 429 |
if rt['resourceType'].lower() == resourceType.lower(): |
| 430 |
self.api_version = rt['apiVersions'][0] |
| 431 |
break |
| 432 |
else: |
| 433 |
|
| 434 |
self.api_version = '2018-05-01' |
| 435 |
if not self.api_version: |
| 436 |
self.fail("Couldn't find api version for {0}/{1}".format(provider, resourceType)) |
| 437 |
except Exception as exc: |
| 438 |
self.fail("Failed to obtain API version: {0}".format(str(exc))) |
| 439 |
|
| 440 |
self.results['url'] = self.url |
| 441 |
|
| 442 |
query_parameters = {} |
| 443 |
query_parameters['api-version'] = self.api_version |
| 444 |
|
| 445 |
header_parameters = {} |
| 446 |
header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 447 |
skiptoken = None |
| 448 |
|
| 449 |
while True: |
| 450 |
if skiptoken: |
| 451 |
query_parameters['skiptoken'] = skiptoken |
| 452 |
response = self.mgmt_client.query(self.url, self.method, query_parameters, header_parameters, None, [200, 404], 0, 0) |
| 453 |
try: |
| 454 |
if isinstance(response.body(), bytes) and bool(response.body().decode()) is False: |
| 455 |
self.results['status_code'] = response.status_code |
| 456 |
else: |
| 457 |
self.results['status_code'] = response.status_code |
| 458 |
response = json.loads(response.body()) |
| 459 |
if isinstance(response, dict): |
| 460 |
if response.get('value'): |
| 461 |
self.results['response'] = self.results['response'] + response['value'] |
| 462 |
skiptoken = response.get('nextLink') |
| 463 |
else: |
| 464 |
self.results['response'] = self.results['response'] + [response] |
| 465 |
except Exception as e: |
| 466 |
self.fail('Failed to parse response: ' + str(e)) |
| 467 |
if not skiptoken: |
| 468 |
break |
| 469 |
if kwargs['tags']: |
| 470 |
filtered_response = [] |
| 471 |
for resource in self.results['response']: |
| 472 |
if all(resource.get('tags', {}).get(tag_key) == tag_value for tag_key, tag_value in kwargs['tags'].items()): |
| 473 |
filtered_response.append(resource) |
| 474 |
self.results['response'] = filtered_response |
| 475 |
return self.results |
| 476 |
|
| 477 |
|
| 478 |
def main(): |
| 479 |
AzureRMResourceInfo() |
| 480 |
|
| 481 |
|
| 482 |
if __name__ == '__main__': |
| 483 |
main() |