| 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_webapp_info |
| 14 |
|
| 15 |
version_added: "0.1.2" |
| 16 |
|
| 17 |
short_description: Get Azure web app facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for a specific web app or all web app in a resource group, or all web app in current subscription. |
| 21 |
|
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Only show results for a specific web app. |
| 26 |
type: str |
| 27 |
resource_group: |
| 28 |
description: |
| 29 |
- Limit results by resource group. |
| 30 |
type: str |
| 31 |
return_publish_profile: |
| 32 |
description: |
| 33 |
- Indicate whether to return publishing profile of the web app. |
| 34 |
default: False |
| 35 |
type: bool |
| 36 |
tags: |
| 37 |
description: |
| 38 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
|
| 42 |
extends_documentation_fragment: |
| 43 |
- azure.azcollection.azure |
| 44 |
|
| 45 |
author: |
| 46 |
- Yunge Zhu (@yungezz) |
| 47 |
''' |
| 48 |
|
| 49 |
EXAMPLES = ''' |
| 50 |
- name: Get facts for web app by name |
| 51 |
azure_rm_webapp_info: |
| 52 |
resource_group: myResourceGroup |
| 53 |
name: winwebapp1 |
| 54 |
|
| 55 |
- name: Get facts for web apps in resource group |
| 56 |
azure_rm_webapp_info: |
| 57 |
resource_group: myResourceGroup |
| 58 |
|
| 59 |
- name: Get facts for web apps with tags |
| 60 |
azure_rm_webapp_info: |
| 61 |
tags: |
| 62 |
- testtag |
| 63 |
- foo:bar |
| 64 |
''' |
| 65 |
|
| 66 |
RETURN = ''' |
| 67 |
webapps: |
| 68 |
description: |
| 69 |
- List of web apps. |
| 70 |
returned: always |
| 71 |
type: complex |
| 72 |
contains: |
| 73 |
id: |
| 74 |
description: |
| 75 |
- ID of the web app. |
| 76 |
returned: always |
| 77 |
type: str |
| 78 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Web/sites/myWebApp |
| 79 |
name: |
| 80 |
description: |
| 81 |
- Name of the web app. |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: winwebapp1 |
| 85 |
resource_group: |
| 86 |
description: |
| 87 |
- Resource group of the web app. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: myResourceGroup |
| 91 |
location: |
| 92 |
description: |
| 93 |
- Location of the web app. |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: eastus |
| 97 |
plan: |
| 98 |
description: |
| 99 |
- ID of app service plan used by the web app. |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan |
| 103 |
app_settings: |
| 104 |
description: |
| 105 |
- App settings of the application. Only returned when web app has app settings. |
| 106 |
returned: always |
| 107 |
type: dict |
| 108 |
sample: { |
| 109 |
"testkey": "testvalue", |
| 110 |
"testkey2": "testvalue2" |
| 111 |
} |
| 112 |
frameworks: |
| 113 |
description: |
| 114 |
- Frameworks of the application. Only returned when web app has frameworks. |
| 115 |
returned: always |
| 116 |
type: list |
| 117 |
sample: [ |
| 118 |
{ |
| 119 |
"name": "net_framework", |
| 120 |
"version": "v4.0" |
| 121 |
}, |
| 122 |
{ |
| 123 |
"name": "java", |
| 124 |
"settings": { |
| 125 |
"java_container": "tomcat", |
| 126 |
"java_container_version": "8.5" |
| 127 |
}, |
| 128 |
"version": "1.7" |
| 129 |
}, |
| 130 |
{ |
| 131 |
"name": "php", |
| 132 |
"version": "5.6" |
| 133 |
} |
| 134 |
] |
| 135 |
always_on: |
| 136 |
description: |
| 137 |
- If the app is kept loaded even when there's no traffic. |
| 138 |
returned: always |
| 139 |
type: bool |
| 140 |
sample: true |
| 141 |
http20_enabled: |
| 142 |
description: |
| 143 |
- Configures a web site to allow clients to connect over HTTP 2.0. |
| 144 |
returned: always |
| 145 |
type: bool |
| 146 |
sample: true |
| 147 |
min_tls_version: |
| 148 |
description: |
| 149 |
- The minimum TLS encryption version required for the app. |
| 150 |
returned: always |
| 151 |
type: str |
| 152 |
sample: 1.2 |
| 153 |
ftps_state: |
| 154 |
description: |
| 155 |
- The state of the FTP/FTPS service. |
| 156 |
returned: always |
| 157 |
type: str |
| 158 |
sample: FtpsOnly |
| 159 |
availability_state: |
| 160 |
description: |
| 161 |
- Availability of this web app. |
| 162 |
returned: always |
| 163 |
type: str |
| 164 |
sample: Normal |
| 165 |
default_host_name: |
| 166 |
description: |
| 167 |
- Host name of the web app. |
| 168 |
returned: always |
| 169 |
type: str |
| 170 |
sample: vxxisurg397winapp4.azurewebsites.net |
| 171 |
enabled: |
| 172 |
description: |
| 173 |
- Indicates the web app enabled or not. |
| 174 |
returned: always |
| 175 |
type: bool |
| 176 |
sample: true |
| 177 |
enabled_host_names: |
| 178 |
description: |
| 179 |
- Enabled host names of the web app. |
| 180 |
returned: always |
| 181 |
type: list |
| 182 |
sample: [ |
| 183 |
"vxxisurg397winapp4.azurewebsites.net", |
| 184 |
"vxxisurg397winapp4.scm.azurewebsites.net" |
| 185 |
] |
| 186 |
host_name_ssl_states: |
| 187 |
description: |
| 188 |
- SSL state per host names of the web app. |
| 189 |
returned: always |
| 190 |
type: list |
| 191 |
sample: [ |
| 192 |
{ |
| 193 |
"hostType": "Standard", |
| 194 |
"name": "vxxisurg397winapp4.azurewebsites.net", |
| 195 |
"sslState": "Disabled" |
| 196 |
}, |
| 197 |
{ |
| 198 |
"hostType": "Repository", |
| 199 |
"name": "vxxisurg397winapp4.scm.azurewebsites.net", |
| 200 |
"sslState": "Disabled" |
| 201 |
} |
| 202 |
] |
| 203 |
host_names: |
| 204 |
description: |
| 205 |
- Host names of the web app. |
| 206 |
returned: always |
| 207 |
type: list |
| 208 |
sample: [ |
| 209 |
"vxxisurg397winapp4.azurewebsites.net" |
| 210 |
] |
| 211 |
outbound_ip_addresses: |
| 212 |
description: |
| 213 |
- Outbound IP address of the web app. |
| 214 |
returned: always |
| 215 |
type: str |
| 216 |
sample: "40.71.11.131,40.85.166.200,168.62.166.67,137.135.126.248,137.135.121.45" |
| 217 |
ftp_publish_url: |
| 218 |
description: |
| 219 |
- Publishing URL of the web app when deployment type is FTP. |
| 220 |
returned: always |
| 221 |
type: str |
| 222 |
sample: ftp://xxxx.ftp.azurewebsites.windows.net |
| 223 |
state: |
| 224 |
description: |
| 225 |
- State of the web app. |
| 226 |
returned: always |
| 227 |
type: str |
| 228 |
sample: running |
| 229 |
publishing_username: |
| 230 |
description: |
| 231 |
- Publishing profile user name. |
| 232 |
returned: only when I(return_publish_profile=True). |
| 233 |
type: str |
| 234 |
sample: "$vxxisuRG397winapp4" |
| 235 |
publishing_password: |
| 236 |
description: |
| 237 |
- Publishing profile password. |
| 238 |
returned: only when I(return_publish_profile=True). |
| 239 |
type: str |
| 240 |
sample: "uvANsPQpGjWJmrFfm4Ssd5rpBSqGhjMk11pMSgW2vCsQtNx9tcgZ0xN26s9A" |
| 241 |
tags: |
| 242 |
description: |
| 243 |
- Tags assigned to the resource. Dictionary of string:string pairs. |
| 244 |
returned: always |
| 245 |
type: dict |
| 246 |
sample: { tag1: abc } |
| 247 |
site_auth_settings: |
| 248 |
description: |
| 249 |
- The Authentication / Authorization settings associated with web app. |
| 250 |
type: dict |
| 251 |
returned: always |
| 252 |
sample: {} |
| 253 |
''' |
| 254 |
try: |
| 255 |
from azure.core.exceptions import ResourceNotFoundError |
| 256 |
from azure.core.polling import LROPoller |
| 257 |
from azure.mgmt.web.models import CsmPublishingProfileOptions |
| 258 |
from azure.core.exceptions import HttpResponseError |
| 259 |
except Exception: |
| 260 |
|
| 261 |
pass |
| 262 |
|
| 263 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 264 |
try: |
| 265 |
import xmltodict |
| 266 |
except Exception: |
| 267 |
pass |
| 268 |
|
| 269 |
AZURE_OBJECT_CLASS = 'WebApp' |
| 270 |
|
| 271 |
|
| 272 |
class AzureRMWebAppInfo(AzureRMModuleBase): |
| 273 |
|
| 274 |
def __init__(self): |
| 275 |
|
| 276 |
self.module_arg_spec = dict( |
| 277 |
name=dict(type='str'), |
| 278 |
resource_group=dict(type='str'), |
| 279 |
tags=dict(type='list', elements='str'), |
| 280 |
return_publish_profile=dict(type='bool', default=False), |
| 281 |
) |
| 282 |
|
| 283 |
self.results = dict( |
| 284 |
changed=False, |
| 285 |
webapps=[], |
| 286 |
) |
| 287 |
|
| 288 |
self.name = None |
| 289 |
self.resource_group = None |
| 290 |
self.tags = None |
| 291 |
self.return_publish_profile = False |
| 292 |
|
| 293 |
self.framework_names = ['net_framework', 'java', 'php', 'node', 'python', 'dotnetcore', 'ruby'] |
| 294 |
|
| 295 |
super(AzureRMWebAppInfo, self).__init__(self.module_arg_spec, |
| 296 |
supports_check_mode=True, |
| 297 |
supports_tags=False, |
| 298 |
facts_module=True) |
| 299 |
|
| 300 |
def exec_module(self, **kwargs): |
| 301 |
|
| 302 |
for key in self.module_arg_spec: |
| 303 |
setattr(self, key, kwargs[key]) |
| 304 |
|
| 305 |
if self.name: |
| 306 |
self.results['webapps'] = self.list_by_name() |
| 307 |
elif self.resource_group: |
| 308 |
self.results['webapps'] = self.list_by_resource_group() |
| 309 |
else: |
| 310 |
self.results['webapps'] = self.list_all() |
| 311 |
|
| 312 |
return self.results |
| 313 |
|
| 314 |
def list_by_name(self): |
| 315 |
self.log('Get web app {0}'.format(self.name)) |
| 316 |
item = None |
| 317 |
result = [] |
| 318 |
|
| 319 |
try: |
| 320 |
item = self.web_client.web_apps.get(resource_group_name=self.resource_group, name=self.name) |
| 321 |
except ResourceNotFoundError: |
| 322 |
pass |
| 323 |
|
| 324 |
if item and self.has_tags(item.tags, self.tags): |
| 325 |
curated_result = self.get_curated_webapp(self.resource_group, self.name, item) |
| 326 |
result = [curated_result] |
| 327 |
|
| 328 |
return result |
| 329 |
|
| 330 |
def list_by_resource_group(self): |
| 331 |
self.log('List web apps in resource groups {0}'.format(self.resource_group)) |
| 332 |
try: |
| 333 |
response = list(self.web_client.web_apps.list_by_resource_group(resource_group_name=self.resource_group)) |
| 334 |
except Exception as exc: |
| 335 |
request_id = exc.request_id if exc.request_id else '' |
| 336 |
self.fail("Error listing web apps in resource groups {0}, request id: {1} - {2}".format(self.resource_group, request_id, str(exc))) |
| 337 |
|
| 338 |
results = [] |
| 339 |
for item in response: |
| 340 |
if self.has_tags(item.tags, self.tags): |
| 341 |
curated_output = self.get_curated_webapp(self.resource_group, item.name, item) |
| 342 |
results.append(curated_output) |
| 343 |
return results |
| 344 |
|
| 345 |
def list_all(self): |
| 346 |
self.log('List web apps in current subscription') |
| 347 |
try: |
| 348 |
response = list(self.web_client.web_apps.list()) |
| 349 |
except Exception as exc: |
| 350 |
request_id = exc.request_id if exc.request_id else '' |
| 351 |
self.fail("Error listing web apps, request id {0} - {1}".format(request_id, str(exc))) |
| 352 |
|
| 353 |
results = [] |
| 354 |
for item in response: |
| 355 |
if self.has_tags(item.tags, self.tags): |
| 356 |
curated_output = self.get_curated_webapp(item.resource_group, item.name, item) |
| 357 |
results.append(curated_output) |
| 358 |
return results |
| 359 |
|
| 360 |
def list_webapp_configuration(self, resource_group, name): |
| 361 |
self.log('Get web app {0} configuration'.format(name)) |
| 362 |
|
| 363 |
response = [] |
| 364 |
|
| 365 |
try: |
| 366 |
response = self.web_client.web_apps.get_configuration(resource_group_name=resource_group, name=name) |
| 367 |
except Exception as ex: |
| 368 |
request_id = ex.request_id if ex.request_id else '' |
| 369 |
self.fail('Error getting web app {0} configuration, request id {1} - {2}'.format(name, request_id, str(ex))) |
| 370 |
|
| 371 |
return response.as_dict() |
| 372 |
|
| 373 |
def list_webapp_appsettings(self, resource_group, name): |
| 374 |
self.log('Get web app {0} app settings'.format(name)) |
| 375 |
|
| 376 |
response = [] |
| 377 |
|
| 378 |
try: |
| 379 |
response = self.web_client.web_apps.list_application_settings(resource_group_name=resource_group, name=name) |
| 380 |
except Exception as ex: |
| 381 |
request_id = ex.request_id if ex.request_id else '' |
| 382 |
self.fail('Error getting web app {0} app settings, request id {1} - {2}'.format(name, request_id, str(ex))) |
| 383 |
|
| 384 |
return response.as_dict() |
| 385 |
|
| 386 |
def get_publish_credentials(self, resource_group, name): |
| 387 |
self.log('Get web app {0} publish credentials'.format(name)) |
| 388 |
try: |
| 389 |
poller = self.web_client.web_apps.begin_list_publishing_credentials(resource_group_name=resource_group, name=name) |
| 390 |
if isinstance(poller, LROPoller): |
| 391 |
response = self.get_poller_result(poller) |
| 392 |
except Exception as ex: |
| 393 |
request_id = ex.request_id if ex.request_id else '' |
| 394 |
self.fail('Error getting web app {0} publishing credentials - {1}'.format(request_id, str(ex))) |
| 395 |
return response |
| 396 |
|
| 397 |
def get_auth_settings(self, resource_group, name): |
| 398 |
self.log('Get web app {0} auth settings'.format(name)) |
| 399 |
try: |
| 400 |
response = self.web_client.web_apps.get_auth_settings(resource_group_name=resource_group, name=name) |
| 401 |
return response.as_dict() |
| 402 |
except HttpResponseError as ex: |
| 403 |
self.log('Error getting web app {0} auth setting, exception as {1}'.format(name, str(ex))) |
| 404 |
|
| 405 |
def get_webapp_ftp_publish_url(self, resource_group, name): |
| 406 |
|
| 407 |
self.log('Get web app {0} app publish profile'.format(name)) |
| 408 |
|
| 409 |
url = None |
| 410 |
try: |
| 411 |
publishing_profile_options = CsmPublishingProfileOptions( |
| 412 |
format="Ftp" |
| 413 |
) |
| 414 |
content = self.web_client.web_apps.list_publishing_profile_xml_with_secrets(resource_group_name=resource_group, |
| 415 |
name=name, |
| 416 |
publishing_profile_options=publishing_profile_options) |
| 417 |
if not content: |
| 418 |
return url |
| 419 |
|
| 420 |
full_xml = '' |
| 421 |
for f in content: |
| 422 |
full_xml += f.decode() |
| 423 |
profiles = xmltodict.parse(full_xml, xml_attribs=True)['publishData']['publishProfile'] |
| 424 |
|
| 425 |
if not profiles: |
| 426 |
return url |
| 427 |
|
| 428 |
for profile in profiles: |
| 429 |
if profile['@publishMethod'] == 'FTP': |
| 430 |
url = profile['@publishUrl'] |
| 431 |
|
| 432 |
except Exception as ex: |
| 433 |
self.fail('Error getting web app {0} app settings - {1}'.format(name, str(ex))) |
| 434 |
|
| 435 |
return url |
| 436 |
|
| 437 |
def get_curated_webapp(self, resource_group, name, webapp): |
| 438 |
pip = self.serialize_obj(webapp, AZURE_OBJECT_CLASS) |
| 439 |
|
| 440 |
try: |
| 441 |
site_config = self.list_webapp_configuration(resource_group, name) |
| 442 |
app_settings = self.list_webapp_appsettings(resource_group, name) |
| 443 |
publish_cred = self.get_publish_credentials(resource_group, name) |
| 444 |
ftp_publish_url = self.get_webapp_ftp_publish_url(resource_group, name) |
| 445 |
site_auth_settings = self.get_auth_settings(resource_group, name) |
| 446 |
except Exception: |
| 447 |
pass |
| 448 |
return self.construct_curated_webapp(webapp=pip, |
| 449 |
configuration=site_config, |
| 450 |
app_settings=app_settings, |
| 451 |
deployment_slot=None, |
| 452 |
ftp_publish_url=ftp_publish_url, |
| 453 |
publish_credentials=publish_cred, |
| 454 |
site_auth_settings=site_auth_settings) |
| 455 |
|
| 456 |
def construct_curated_webapp(self, |
| 457 |
webapp, |
| 458 |
configuration=None, |
| 459 |
app_settings=None, |
| 460 |
deployment_slot=None, |
| 461 |
ftp_publish_url=None, |
| 462 |
publish_credentials=None, |
| 463 |
site_auth_settings=None): |
| 464 |
curated_output = dict() |
| 465 |
curated_output['id'] = webapp['id'] |
| 466 |
curated_output['name'] = webapp['name'] |
| 467 |
curated_output['resource_group'] = webapp['resource_group'] |
| 468 |
curated_output['location'] = webapp['location'] |
| 469 |
curated_output['plan'] = webapp['server_farm_id'] |
| 470 |
curated_output['tags'] = webapp.get('tags', None) |
| 471 |
|
| 472 |
|
| 473 |
curated_output['app_state'] = webapp['state'] |
| 474 |
curated_output['availability_state'] = webapp['availability_state'] |
| 475 |
curated_output['default_host_name'] = webapp['default_host_name'] |
| 476 |
curated_output['host_names'] = webapp['host_names'] |
| 477 |
curated_output['enabled'] = webapp['enabled'] |
| 478 |
curated_output['enabled_host_names'] = webapp['enabled_host_names'] |
| 479 |
curated_output['host_name_ssl_states'] = webapp['host_name_ssl_states'] |
| 480 |
curated_output['outbound_ip_addresses'] = webapp['outbound_ip_addresses'] |
| 481 |
curated_output['identity'] = webapp.get('identity', None) |
| 482 |
|
| 483 |
|
| 484 |
if configuration: |
| 485 |
curated_output['frameworks'] = [] |
| 486 |
for fx_name in self.framework_names: |
| 487 |
fx_version = configuration.get(fx_name + '_version', None) |
| 488 |
if fx_version: |
| 489 |
fx = { |
| 490 |
'name': fx_name, |
| 491 |
'version': fx_version |
| 492 |
} |
| 493 |
|
| 494 |
if fx_name == 'java': |
| 495 |
if configuration['java_container'] and configuration['java_container_version']: |
| 496 |
settings = { |
| 497 |
'java_container': configuration['java_container'].lower(), |
| 498 |
'java_container_version': configuration['java_container_version'] |
| 499 |
} |
| 500 |
fx['settings'] = settings |
| 501 |
|
| 502 |
curated_output['frameworks'].append(fx) |
| 503 |
|
| 504 |
|
| 505 |
if configuration.get('linux_fx_version', None): |
| 506 |
tmp = configuration.get('linux_fx_version').split("|") |
| 507 |
if len(tmp) == 2: |
| 508 |
curated_output['frameworks'].append({'name': tmp[0].lower(), 'version': tmp[1]}) |
| 509 |
|
| 510 |
curated_output['always_on'] = configuration.get('always_on') |
| 511 |
curated_output['http20_enabled'] = configuration.get('http20_enabled') |
| 512 |
curated_output['ftps_state'] = configuration.get('ftps_state') |
| 513 |
curated_output['min_tls_version'] = configuration.get('min_tls_version') |
| 514 |
|
| 515 |
|
| 516 |
if app_settings and app_settings.get('properties', None): |
| 517 |
curated_output['app_settings'] = dict() |
| 518 |
for item in app_settings['properties']: |
| 519 |
curated_output['app_settings'][item] = app_settings['properties'][item] |
| 520 |
|
| 521 |
|
| 522 |
if deployment_slot: |
| 523 |
curated_output['deployment_slot'] = deployment_slot |
| 524 |
|
| 525 |
|
| 526 |
if ftp_publish_url: |
| 527 |
curated_output['ftp_publish_url'] = ftp_publish_url |
| 528 |
|
| 529 |
|
| 530 |
if publish_credentials and self.return_publish_profile: |
| 531 |
curated_output['publishing_username'] = publish_credentials.publishing_user_name |
| 532 |
curated_output['publishing_password'] = publish_credentials.publishing_password |
| 533 |
|
| 534 |
|
| 535 |
curated_output['site_auth_settings'] = site_auth_settings if site_auth_settings is not None else {} |
| 536 |
return curated_output |
| 537 |
|
| 538 |
|
| 539 |
def main(): |
| 540 |
AzureRMWebAppInfo() |
| 541 |
|
| 542 |
|
| 543 |
if __name__ == '__main__': |
| 544 |
main() |