| 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_automationaccount_info |
| 14 |
version_added: '0.1.2' |
| 15 |
short_description: Get Azure automation account facts |
| 16 |
description: |
| 17 |
- Get facts of automation account. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group. |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the automation account. |
| 27 |
type: str |
| 28 |
tags: |
| 29 |
description: |
| 30 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 31 |
type: list |
| 32 |
elements: str |
| 33 |
list_statistics: |
| 34 |
description: |
| 35 |
- List statistics details for a automation account. |
| 36 |
- Note this will cost network overhead, suggest only used when I(name) set. |
| 37 |
type: bool |
| 38 |
list_usages: |
| 39 |
description: |
| 40 |
- List usage details for a automation account. |
| 41 |
- Note this will cost network overhead, suggest only used when I(name) set. |
| 42 |
type: bool |
| 43 |
list_keys: |
| 44 |
description: |
| 45 |
- List keys for a automation account. |
| 46 |
- Note this will cost network overhead, suggest only used when I(name) set. |
| 47 |
type: bool |
| 48 |
|
| 49 |
extends_documentation_fragment: |
| 50 |
- azure.azcollection.azure |
| 51 |
|
| 52 |
author: |
| 53 |
- Yuwei Zhou (@yuwzho) |
| 54 |
|
| 55 |
''' |
| 56 |
|
| 57 |
EXAMPLES = ''' |
| 58 |
- name: Get details of an automation account |
| 59 |
azure_rm_automationaccount_info: |
| 60 |
name: Testing |
| 61 |
resource_group: myResourceGroup |
| 62 |
list_statistics: true |
| 63 |
list_usages: true |
| 64 |
list_keys: true |
| 65 |
tags: |
| 66 |
- key |
| 67 |
- key:value |
| 68 |
|
| 69 |
- name: List automation account in a resource group |
| 70 |
azure_rm_automationaccount_info: |
| 71 |
resource_group: myResourceGroup |
| 72 |
|
| 73 |
- name: List automation account in a resource group |
| 74 |
azure_rm_automationaccount_info: |
| 75 |
''' |
| 76 |
|
| 77 |
RETURN = ''' |
| 78 |
automation_accounts: |
| 79 |
description: |
| 80 |
- List of automation account dicts. |
| 81 |
returned: always |
| 82 |
type: complex |
| 83 |
contains: |
| 84 |
id: |
| 85 |
description: |
| 86 |
- Resource ID. |
| 87 |
type: str |
| 88 |
returned: always |
| 89 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups |
| 90 |
/myResourceGroup/providers/Microsoft.Automation/automationAccounts/Testing" |
| 91 |
resource_group: |
| 92 |
description: |
| 93 |
- Resource group name. |
| 94 |
type: str |
| 95 |
returned: always |
| 96 |
sample: myResourceGroup |
| 97 |
name: |
| 98 |
description: |
| 99 |
- Resource name. |
| 100 |
type: str |
| 101 |
returned: always |
| 102 |
sample: Testing |
| 103 |
location: |
| 104 |
description: |
| 105 |
- Resource location. |
| 106 |
type: str |
| 107 |
returned: always |
| 108 |
sample: eastus |
| 109 |
creation_time: |
| 110 |
description: |
| 111 |
- Resource creation date time. |
| 112 |
type: str |
| 113 |
returned: always |
| 114 |
sample: "2019-04-26T02:55:16.500Z" |
| 115 |
last_modified_time: |
| 116 |
description: |
| 117 |
- Resource last modified date time. |
| 118 |
type: str |
| 119 |
returned: always |
| 120 |
sample: "2019-04-26T02:55:16.500Z" |
| 121 |
state: |
| 122 |
description: |
| 123 |
- Resource state. |
| 124 |
type: str |
| 125 |
returned: always |
| 126 |
sample: ok |
| 127 |
account_keys: |
| 128 |
description: |
| 129 |
- Resource keys. |
| 130 |
type: complex |
| 131 |
returned: when I(list_keys=True) |
| 132 |
contains: |
| 133 |
key_name: |
| 134 |
description: |
| 135 |
- Name of the key. |
| 136 |
type: str |
| 137 |
returned: when I(list_keys=True) |
| 138 |
sample: Primary |
| 139 |
permissions: |
| 140 |
description: |
| 141 |
- Permission of the key. |
| 142 |
type: str |
| 143 |
returned: when I(list_keys=True) |
| 144 |
sample: Full |
| 145 |
value: |
| 146 |
description: |
| 147 |
- Value of the key. |
| 148 |
type: str |
| 149 |
returned: when I(list_keys=True) |
| 150 |
sample: "MbepKTO6IyGwml0GaKBkKN" |
| 151 |
keys: |
| 152 |
description: |
| 153 |
- Resource keys. |
| 154 |
- This return value has been deprecated and will be removed in a release after |
| 155 |
2027-05-01. Use C(account_keys) instead. |
| 156 |
type: complex |
| 157 |
returned: when I(list_keys=True) |
| 158 |
contains: |
| 159 |
key_name: |
| 160 |
description: |
| 161 |
- Name of the key. |
| 162 |
type: str |
| 163 |
returned: when I(list_keys=True) |
| 164 |
sample: Primary |
| 165 |
permissions: |
| 166 |
description: |
| 167 |
- Permission of the key. |
| 168 |
type: str |
| 169 |
returned: when I(list_keys=True) |
| 170 |
sample: Full |
| 171 |
value: |
| 172 |
description: |
| 173 |
- Value of the key. |
| 174 |
type: str |
| 175 |
returned: when I(list_keys=True) |
| 176 |
sample: "MbepKTO6IyGwml0GaKBkKN" |
| 177 |
statistics: |
| 178 |
description: |
| 179 |
- Resource statistics. |
| 180 |
type: complex |
| 181 |
returned: always |
| 182 |
contains: |
| 183 |
counter_property: |
| 184 |
description: |
| 185 |
- Property value of the statistic. |
| 186 |
type: str |
| 187 |
returned: always |
| 188 |
sample: New |
| 189 |
counter_value: |
| 190 |
description: |
| 191 |
- Value of the statistic. |
| 192 |
type: int |
| 193 |
returned: always |
| 194 |
sample: 0 |
| 195 |
end_time: |
| 196 |
description: |
| 197 |
- EndTime of the statistic. |
| 198 |
type: str |
| 199 |
returned: always |
| 200 |
sample: "2019-04-26T06:29:43.587518Z" |
| 201 |
id: |
| 202 |
description: |
| 203 |
- ID of the statistic. |
| 204 |
type: str |
| 205 |
returned: always |
| 206 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups |
| 207 |
/myResourceGroup/providers/Microsoft.Automation/automationAccounts/Testing/statistics/New" |
| 208 |
start_time: |
| 209 |
description: |
| 210 |
- StartTime of the statistic. |
| 211 |
type: str |
| 212 |
returned: always |
| 213 |
sample: "2019-04-26T06:29:43.587518Z" |
| 214 |
usages: |
| 215 |
description: |
| 216 |
- Resource usages. |
| 217 |
type: complex |
| 218 |
returned: always |
| 219 |
contains: |
| 220 |
current_value: |
| 221 |
description: |
| 222 |
- Current usage. |
| 223 |
type: float |
| 224 |
returned: always |
| 225 |
sample: 0.0 |
| 226 |
limit: |
| 227 |
description: |
| 228 |
- Max limit, C(-1) for unlimited. |
| 229 |
type: int |
| 230 |
returned: always |
| 231 |
sample: -1 |
| 232 |
name: |
| 233 |
description: |
| 234 |
- Usage counter name. |
| 235 |
type: complex |
| 236 |
returned: always |
| 237 |
contains: |
| 238 |
localized_value: |
| 239 |
description: |
| 240 |
- Localized name. |
| 241 |
type: str |
| 242 |
returned: always |
| 243 |
sample: "SubscriptionUsage" |
| 244 |
value: |
| 245 |
description: |
| 246 |
- Name value. |
| 247 |
type: str |
| 248 |
returned: always |
| 249 |
sample: "SubscriptionUsage" |
| 250 |
unit: |
| 251 |
description: |
| 252 |
- Usage unit name. |
| 253 |
type: str |
| 254 |
returned: always |
| 255 |
sample: "Minute" |
| 256 |
throttle_status: |
| 257 |
description: |
| 258 |
- Usage throttle status. |
| 259 |
type: str |
| 260 |
returned: always |
| 261 |
sample: "NotThrottled" |
| 262 |
|
| 263 |
''' |
| 264 |
|
| 265 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 266 |
|
| 267 |
try: |
| 268 |
from azure.mgmt.core.tools import parse_resource_id |
| 269 |
from azure.core.exceptions import ResourceNotFoundError |
| 270 |
except ImportError: |
| 271 |
pass |
| 272 |
|
| 273 |
|
| 274 |
class AzureRMAutomationAccountInfo(AzureRMModuleBase): |
| 275 |
def __init__(self): |
| 276 |
|
| 277 |
self.module_arg_spec = dict( |
| 278 |
resource_group=dict( |
| 279 |
type='str' |
| 280 |
), |
| 281 |
name=dict( |
| 282 |
type='str' |
| 283 |
), |
| 284 |
tags=dict( |
| 285 |
type='list', |
| 286 |
elements='str' |
| 287 |
), |
| 288 |
list_statistics=dict( |
| 289 |
type='bool' |
| 290 |
), |
| 291 |
list_usages=dict( |
| 292 |
type='bool' |
| 293 |
), |
| 294 |
list_keys=dict( |
| 295 |
type='bool' |
| 296 |
) |
| 297 |
) |
| 298 |
|
| 299 |
self.results = dict() |
| 300 |
self.resource_group = None |
| 301 |
self.name = None |
| 302 |
self.tags = None |
| 303 |
self.list_statistics = None |
| 304 |
self.list_usages = None |
| 305 |
self.list_keys = None |
| 306 |
|
| 307 |
super(AzureRMAutomationAccountInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True) |
| 308 |
|
| 309 |
def exec_module(self, **kwargs): |
| 310 |
|
| 311 |
for key in list(self.module_arg_spec): |
| 312 |
setattr(self, key, kwargs[key]) |
| 313 |
|
| 314 |
if self.list_keys: |
| 315 |
self.module.deprecate( |
| 316 |
"The 'keys' return key is deprecated. Use 'account_keys' instead.", |
| 317 |
date="2027-05-01", |
| 318 |
collection_name="azure.azcollection", |
| 319 |
) |
| 320 |
|
| 321 |
if self.resource_group and self.name: |
| 322 |
accounts = [self.get()] |
| 323 |
elif self.resource_group: |
| 324 |
accounts = self.list_by_resource_group() |
| 325 |
else: |
| 326 |
accounts = self.list_all() |
| 327 |
self.results['automation_accounts'] = [self.to_dict(x) for x in accounts if self.has_tags(x.tags, self.tags)] |
| 328 |
return self.results |
| 329 |
|
| 330 |
def to_dict(self, account): |
| 331 |
if not account: |
| 332 |
return None |
| 333 |
id_dict = parse_resource_id(account.id) |
| 334 |
result = account.as_dict() |
| 335 |
result['resource_group'] = id_dict['resource_group'] |
| 336 |
if self.list_statistics: |
| 337 |
result['statistics'] = self.get_statics(id_dict['resource_group'], account.name) |
| 338 |
if self.list_usages: |
| 339 |
result['usages'] = self.get_usages(id_dict['resource_group'], account.name) |
| 340 |
if self.list_keys: |
| 341 |
result['account_keys'] = self.list_account_keys(id_dict['resource_group'], account.name) |
| 342 |
result['keys'] = result['account_keys'] |
| 343 |
return result |
| 344 |
|
| 345 |
def get(self): |
| 346 |
try: |
| 347 |
return self.automation_client.automation_account.get(self.resource_group, self.name) |
| 348 |
except ResourceNotFoundError as exc: |
| 349 |
self.fail('Error when getting automation account {0}: {1}'.format(self.name, exc.message)) |
| 350 |
|
| 351 |
def list_by_resource_group(self): |
| 352 |
result = [] |
| 353 |
try: |
| 354 |
resp = self.automation_client.automation_account.list_by_resource_group(self.resource_group) |
| 355 |
while True: |
| 356 |
result.append(resp.next()) |
| 357 |
except StopIteration: |
| 358 |
pass |
| 359 |
except Exception as exc: |
| 360 |
self.fail('Error when listing automation account in resource group {0}: {1}'.format(self.resource_group, exc.message)) |
| 361 |
return result |
| 362 |
|
| 363 |
def list_all(self): |
| 364 |
result = [] |
| 365 |
try: |
| 366 |
resp = self.automation_client.automation_account.list() |
| 367 |
while True: |
| 368 |
result.append(resp.next()) |
| 369 |
except StopIteration: |
| 370 |
pass |
| 371 |
except Exception as exc: |
| 372 |
self.fail('Error when listing automation account: {0}'.format(exc.message)) |
| 373 |
return result |
| 374 |
|
| 375 |
def get_statics(self, resource_group, name): |
| 376 |
result = [] |
| 377 |
try: |
| 378 |
resp = self.automation_client.statistics.list_by_automation_account(resource_group, name) |
| 379 |
while True: |
| 380 |
result.append(resp.next().as_dict()) |
| 381 |
except StopIteration: |
| 382 |
pass |
| 383 |
except Exception as exc: |
| 384 |
self.fail('Error when getting statics for automation account {0}/{1}: {2}'.format(resource_group, name, exc.message)) |
| 385 |
return result |
| 386 |
|
| 387 |
def get_usages(self, resource_group, name): |
| 388 |
result = [] |
| 389 |
try: |
| 390 |
resp = self.automation_client.usages.list_by_automation_account(resource_group, name) |
| 391 |
while True: |
| 392 |
result.append(resp.next().as_dict()) |
| 393 |
except StopIteration: |
| 394 |
pass |
| 395 |
except Exception as exc: |
| 396 |
self.fail('Error when getting usage for automation account {0}/{1}: {2}'.format(resource_group, name, exc.message)) |
| 397 |
return result |
| 398 |
|
| 399 |
def list_account_keys(self, resource_group, name): |
| 400 |
try: |
| 401 |
resp = self.automation_client.keys.list_by_automation_account(resource_group, name) |
| 402 |
return [x.as_dict() for x in resp.keys] |
| 403 |
except Exception as exc: |
| 404 |
self.fail('Error when listing keys for automation account {0}/{1}: {2}'.format(resource_group, name, exc.message)) |
| 405 |
|
| 406 |
|
| 407 |
def main(): |
| 408 |
AzureRMAutomationAccountInfo() |
| 409 |
|
| 410 |
|
| 411 |
if __name__ == '__main__': |
| 412 |
main() |