| 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_functionapp |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Function Apps |
| 16 |
description: |
| 17 |
- Create, update or delete an Azure Function App. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of resource group. |
| 22 |
required: true |
| 23 |
aliases: |
| 24 |
- resource_group_name |
| 25 |
type: str |
| 26 |
name: |
| 27 |
description: |
| 28 |
- Name of the Azure Function App. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
location: |
| 32 |
description: |
| 33 |
- Valid Azure location. Defaults to location of the resource group. |
| 34 |
type: str |
| 35 |
plan: |
| 36 |
description: |
| 37 |
- App service plan. |
| 38 |
- It can be name of existing app service plan in same resource group as function app. |
| 39 |
- It can be resource id of existing app service plan. |
| 40 |
- Resource id. For example /subscriptions/<subs_id>/resourceGroups/<resource_group>/providers/Microsoft.Web/serverFarms/<plan_name>. |
| 41 |
- It can be a dict which contains C(name), C(resource_group). |
| 42 |
- C(name). Name of app service plan. |
| 43 |
- C(resource_group). Resource group name of app service plan. |
| 44 |
type: raw |
| 45 |
container_settings: |
| 46 |
description: |
| 47 |
- Web app container settings. |
| 48 |
type: dict |
| 49 |
suboptions: |
| 50 |
name: |
| 51 |
description: |
| 52 |
- Name of container. For example "imagename:tag". |
| 53 |
required: True |
| 54 |
type: str |
| 55 |
registry_server_url: |
| 56 |
description: |
| 57 |
- Container registry server url. For example C(mydockerregistry.io). |
| 58 |
type: str |
| 59 |
registry_server_user: |
| 60 |
description: |
| 61 |
- The container registry server user name. |
| 62 |
type: str |
| 63 |
registry_server_password: |
| 64 |
description: |
| 65 |
- The container registry server password. |
| 66 |
type: str |
| 67 |
storage_account: |
| 68 |
description: |
| 69 |
- Name of the storage account to use. |
| 70 |
- Required when creating. |
| 71 |
type: str |
| 72 |
aliases: |
| 73 |
- storage |
| 74 |
- storage_account_name |
| 75 |
app_settings: |
| 76 |
description: |
| 77 |
- Dictionary containing application settings. |
| 78 |
type: dict |
| 79 |
state: |
| 80 |
description: |
| 81 |
- Assert the state of the Function App. Use C(present) to create or update a Function App and C(absent) to delete. |
| 82 |
default: present |
| 83 |
type: str |
| 84 |
choices: |
| 85 |
- absent |
| 86 |
- present |
| 87 |
|
| 88 |
extends_documentation_fragment: |
| 89 |
- azure.azcollection.azure |
| 90 |
- azure.azcollection.azure_tags |
| 91 |
- azure.azcollection.azure_identity_multiple |
| 92 |
|
| 93 |
author: |
| 94 |
- Thomas Stringer (@trstringer) |
| 95 |
''' |
| 96 |
|
| 97 |
EXAMPLES = ''' |
| 98 |
- name: Create a function app |
| 99 |
azure_rm_functionapp: |
| 100 |
resource_group: myResourceGroup |
| 101 |
name: myFunctionApp |
| 102 |
storage_account: myStorageAccount |
| 103 |
|
| 104 |
- name: Create a function app with app settings |
| 105 |
azure_rm_functionapp: |
| 106 |
resource_group: myResourceGroup |
| 107 |
name: myFunctionApp |
| 108 |
storage_account: myStorageAccount |
| 109 |
app_settings: |
| 110 |
setting1: value1 |
| 111 |
setting2: value2 |
| 112 |
|
| 113 |
- name: Create container based function app |
| 114 |
azure_rm_functionapp: |
| 115 |
resource_group: myResourceGroup |
| 116 |
name: myFunctionApp |
| 117 |
storage_account: myStorageAccount |
| 118 |
plan: |
| 119 |
resource_group: myResourceGroup |
| 120 |
name: myAppPlan |
| 121 |
container_settings: |
| 122 |
name: httpd |
| 123 |
registry_server_url: index.docker.io |
| 124 |
|
| 125 |
- name: Delete a function app |
| 126 |
azure_rm_functionapp: |
| 127 |
resource_group: myResourceGroup |
| 128 |
name: myFunctionApp |
| 129 |
state: absent |
| 130 |
''' |
| 131 |
|
| 132 |
RETURN = ''' |
| 133 |
state: |
| 134 |
description: |
| 135 |
- Current state of the Azure Function App. |
| 136 |
returned: success |
| 137 |
type: dict |
| 138 |
example: |
| 139 |
id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Web/sites/myFunctionApp |
| 140 |
name: myfunctionapp |
| 141 |
kind: functionapp |
| 142 |
location: East US |
| 143 |
type: Microsoft.Web/sites |
| 144 |
state: Running |
| 145 |
host_names: |
| 146 |
- myfunctionapp.azurewebsites.net |
| 147 |
repository_site_name: myfunctionapp |
| 148 |
usage_state: Normal |
| 149 |
enabled: true |
| 150 |
enabled_host_names: |
| 151 |
- myfunctionapp.azurewebsites.net |
| 152 |
- myfunctionapp.scm.azurewebsites.net |
| 153 |
availability_state: Normal |
| 154 |
host_name_ssl_states: |
| 155 |
- name: myfunctionapp.azurewebsites.net |
| 156 |
ssl_state: Disabled |
| 157 |
host_type: Standard |
| 158 |
- name: myfunctionapp.scm.azurewebsites.net |
| 159 |
ssl_state: Disabled |
| 160 |
host_type: Repository |
| 161 |
server_farm_id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/EastUSPlan |
| 162 |
reserved: false |
| 163 |
last_modified_time_utc: '2017-08-22T18:54:01.190Z' |
| 164 |
scm_site_also_stopped: false |
| 165 |
client_affinity_enabled: true |
| 166 |
client_cert_enabled: false |
| 167 |
host_names_disabled: false |
| 168 |
outbound_ip_addresses: ............ |
| 169 |
container_size: 1536 |
| 170 |
daily_memory_time_quota: 0 |
| 171 |
resource_group: myResourceGroup |
| 172 |
default_host_name: myfunctionapp.azurewebsites.net |
| 173 |
''' |
| 174 |
|
| 175 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 176 |
|
| 177 |
try: |
| 178 |
from azure.core.exceptions import ResourceNotFoundError |
| 179 |
from azure.mgmt.web.models import Site, SiteConfig, NameValuePair, ManagedServiceIdentity, UserAssignedIdentity |
| 180 |
|
| 181 |
except ImportError: |
| 182 |
|
| 183 |
pass |
| 184 |
|
| 185 |
container_settings_spec = dict( |
| 186 |
name=dict(type='str', required=True), |
| 187 |
registry_server_url=dict(type='str'), |
| 188 |
registry_server_user=dict(type='str'), |
| 189 |
registry_server_password=dict(type='str', no_log=True) |
| 190 |
) |
| 191 |
|
| 192 |
|
| 193 |
class AzureRMFunctionApp(AzureRMModuleBaseExt): |
| 194 |
|
| 195 |
def __init__(self): |
| 196 |
|
| 197 |
self.module_arg_spec = dict( |
| 198 |
resource_group=dict(type='str', required=True, aliases=['resource_group_name']), |
| 199 |
name=dict(type='str', required=True), |
| 200 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 201 |
location=dict(type='str'), |
| 202 |
storage_account=dict( |
| 203 |
type='str', |
| 204 |
aliases=['storage', 'storage_account_name'] |
| 205 |
), |
| 206 |
app_settings=dict(type='dict'), |
| 207 |
plan=dict( |
| 208 |
type='raw' |
| 209 |
), |
| 210 |
identity=dict( |
| 211 |
type='dict', |
| 212 |
options=self.managed_identity_multiple_spec |
| 213 |
), |
| 214 |
container_settings=dict( |
| 215 |
type='dict', |
| 216 |
options=container_settings_spec |
| 217 |
) |
| 218 |
) |
| 219 |
|
| 220 |
self.results = dict( |
| 221 |
changed=False, |
| 222 |
state=dict() |
| 223 |
) |
| 224 |
|
| 225 |
self.resource_group = None |
| 226 |
self.name = None |
| 227 |
self.state = None |
| 228 |
self.location = None |
| 229 |
self.storage_account = None |
| 230 |
self.app_settings = None |
| 231 |
self.plan = None |
| 232 |
self.container_settings = None |
| 233 |
self._managed_identity = None |
| 234 |
|
| 235 |
|
| 236 |
self.identity = None |
| 237 |
|
| 238 |
required_if = [('state', 'present', ['storage_account'])] |
| 239 |
|
| 240 |
super(AzureRMFunctionApp, self).__init__( |
| 241 |
self.module_arg_spec, |
| 242 |
supports_check_mode=True, |
| 243 |
required_if=required_if |
| 244 |
) |
| 245 |
|
| 246 |
@property |
| 247 |
def managed_identity(self): |
| 248 |
if not self._managed_identity: |
| 249 |
self._managed_identity = {"identity": ManagedServiceIdentity, |
| 250 |
"user_assigned": UserAssignedIdentity |
| 251 |
} |
| 252 |
return self._managed_identity |
| 253 |
|
| 254 |
def exec_module(self, **kwargs): |
| 255 |
|
| 256 |
for key in self.module_arg_spec: |
| 257 |
setattr(self, key, kwargs[key]) |
| 258 |
if self.app_settings is None: |
| 259 |
self.app_settings = dict() |
| 260 |
|
| 261 |
try: |
| 262 |
resource_group = self.rm_client.resource_groups.get(self.resource_group) |
| 263 |
except Exception: |
| 264 |
self.fail('Unable to retrieve resource group') |
| 265 |
|
| 266 |
self.location = self.location or resource_group.location |
| 267 |
|
| 268 |
try: |
| 269 |
function_app = self.web_client.web_apps.get(resource_group_name=self.resource_group, name=self.name) |
| 270 |
|
| 271 |
exists = function_app is not None |
| 272 |
except ResourceNotFoundError as exc: |
| 273 |
exists = False |
| 274 |
|
| 275 |
if self.state == 'absent': |
| 276 |
if exists: |
| 277 |
if self.check_mode: |
| 278 |
self.results['changed'] = True |
| 279 |
return self.results |
| 280 |
try: |
| 281 |
self.web_client.web_apps.delete(resource_group_name=self.resource_group, name=self.name) |
| 282 |
self.results['changed'] = True |
| 283 |
except Exception as exc: |
| 284 |
self.fail('Failure while deleting web app: {0}'.format(exc)) |
| 285 |
else: |
| 286 |
self.results['changed'] = False |
| 287 |
else: |
| 288 |
kind = 'functionapp' |
| 289 |
linux_fx_version = None |
| 290 |
if self.container_settings and self.container_settings.get('name'): |
| 291 |
kind = 'functionapp,linux,container' |
| 292 |
linux_fx_version = 'DOCKER|' |
| 293 |
if self.container_settings.get('registry_server_url'): |
| 294 |
self.app_settings['DOCKER_REGISTRY_SERVER_URL'] = 'https://' + self.container_settings['registry_server_url'] |
| 295 |
linux_fx_version += self.container_settings['registry_server_url'] + '/' |
| 296 |
linux_fx_version += self.container_settings['name'] |
| 297 |
if self.container_settings.get('registry_server_user'): |
| 298 |
self.app_settings['DOCKER_REGISTRY_SERVER_USERNAME'] = self.container_settings.get('registry_server_user') |
| 299 |
|
| 300 |
if self.container_settings.get('registry_server_password'): |
| 301 |
self.app_settings['DOCKER_REGISTRY_SERVER_PASSWORD'] = self.container_settings.get('registry_server_password') |
| 302 |
|
| 303 |
if not self.plan and exists: |
| 304 |
self.plan = function_app.server_farm_id |
| 305 |
|
| 306 |
curr_identity = function_app.identity.as_dict() if exists and function_app.identity is not None else None |
| 307 |
update_identity = False |
| 308 |
|
| 309 |
if self.identity: |
| 310 |
update_identity, self.identity = self.update_managed_identity(new_identity=self.identity, curr_identity=curr_identity) |
| 311 |
|
| 312 |
if not exists: |
| 313 |
function_app = Site( |
| 314 |
location=self.location, |
| 315 |
kind=kind, |
| 316 |
site_config=SiteConfig( |
| 317 |
app_settings=self.aggregated_app_settings(), |
| 318 |
scm_type='LocalGit' |
| 319 |
), |
| 320 |
identity=self.identity |
| 321 |
) |
| 322 |
self.results['changed'] = True |
| 323 |
else: |
| 324 |
new_identity = None |
| 325 |
if update_identity: |
| 326 |
new_identity = self.identity |
| 327 |
self.results['changed'], function_app = self.update(function_app, new_identity) |
| 328 |
|
| 329 |
|
| 330 |
if self.plan: |
| 331 |
if isinstance(self.plan, dict): |
| 332 |
self.plan = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/serverfarms/{2}".format( |
| 333 |
self.subscription_id, |
| 334 |
self.plan.get('resource_group', self.resource_group), |
| 335 |
self.plan.get('name') |
| 336 |
) |
| 337 |
function_app.server_farm_id = self.plan |
| 338 |
|
| 339 |
|
| 340 |
if linux_fx_version: |
| 341 |
function_app.site_config.linux_fx_version = linux_fx_version |
| 342 |
|
| 343 |
if self.check_mode: |
| 344 |
self.results['state'] = function_app.as_dict() |
| 345 |
elif self.results['changed']: |
| 346 |
try: |
| 347 |
response = self.web_client.web_apps.begin_create_or_update(resource_group_name=self.resource_group, |
| 348 |
name=self.name, |
| 349 |
site_envelope=function_app) |
| 350 |
new_function_app = self.get_poller_result(response) |
| 351 |
self.results['state'] = new_function_app.as_dict() |
| 352 |
except Exception as exc: |
| 353 |
self.fail('Error creating or updating web app: {0}'.format(exc)) |
| 354 |
|
| 355 |
return self.results |
| 356 |
|
| 357 |
def update(self, source_function_app, identity=None): |
| 358 |
"""Update the Site object if there are any changes""" |
| 359 |
|
| 360 |
source_app_settings = self.web_client.web_apps.list_application_settings( |
| 361 |
resource_group_name=self.resource_group, |
| 362 |
name=self.name |
| 363 |
) |
| 364 |
|
| 365 |
changed, target_app_settings = self.update_app_settings(source_app_settings.properties) |
| 366 |
|
| 367 |
source_function_app.site_config = SiteConfig( |
| 368 |
app_settings=target_app_settings, |
| 369 |
scm_type='LocalGit' |
| 370 |
) |
| 371 |
|
| 372 |
if identity is not None: |
| 373 |
source_function_app.identity = identity |
| 374 |
changed = True |
| 375 |
|
| 376 |
return changed, source_function_app |
| 377 |
|
| 378 |
def update_app_settings(self, source_app_settings): |
| 379 |
"""Update app settings""" |
| 380 |
|
| 381 |
target_app_settings = self.aggregated_app_settings() |
| 382 |
target_app_settings_dict = dict([(i.name, i.value) for i in target_app_settings]) |
| 383 |
return target_app_settings_dict != source_app_settings, target_app_settings |
| 384 |
|
| 385 |
def necessary_functionapp_settings(self): |
| 386 |
"""Construct the necessary app settings required for an Azure Function App""" |
| 387 |
|
| 388 |
function_app_settings = [] |
| 389 |
|
| 390 |
if self.container_settings is None: |
| 391 |
for key in ['AzureWebJobsStorage', 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING', 'AzureWebJobsDashboard']: |
| 392 |
function_app_settings.append(NameValuePair(name=key, value=self.storage_connection_string)) |
| 393 |
function_app_settings.append(NameValuePair(name='FUNCTIONS_EXTENSION_VERSION', value='~1')) |
| 394 |
function_app_settings.append(NameValuePair(name='WEBSITE_NODE_DEFAULT_VERSION', value='6.5.0')) |
| 395 |
function_app_settings.append(NameValuePair(name='WEBSITE_CONTENTSHARE', value=self.name)) |
| 396 |
else: |
| 397 |
function_app_settings.append(NameValuePair(name='FUNCTIONS_EXTENSION_VERSION', value='~2')) |
| 398 |
function_app_settings.append(NameValuePair(name='WEBSITES_ENABLE_APP_SERVICE_STORAGE', value=False)) |
| 399 |
function_app_settings.append(NameValuePair(name='AzureWebJobsStorage', value=self.storage_connection_string)) |
| 400 |
|
| 401 |
return function_app_settings |
| 402 |
|
| 403 |
def aggregated_app_settings(self): |
| 404 |
"""Combine both system and user app settings""" |
| 405 |
|
| 406 |
function_app_settings = self.necessary_functionapp_settings() |
| 407 |
for app_setting_key in self.app_settings: |
| 408 |
found_setting = None |
| 409 |
for s in function_app_settings: |
| 410 |
if s.name == app_setting_key: |
| 411 |
found_setting = s |
| 412 |
break |
| 413 |
if found_setting: |
| 414 |
found_setting.value = self.app_settings[app_setting_key] |
| 415 |
else: |
| 416 |
function_app_settings.append(NameValuePair( |
| 417 |
name=app_setting_key, |
| 418 |
value=self.app_settings[app_setting_key] |
| 419 |
)) |
| 420 |
return function_app_settings |
| 421 |
|
| 422 |
@property |
| 423 |
def storage_connection_string(self): |
| 424 |
"""Construct the storage account connection string""" |
| 425 |
|
| 426 |
return 'DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}'.format( |
| 427 |
self.storage_account, |
| 428 |
self.storage_key |
| 429 |
) |
| 430 |
|
| 431 |
@property |
| 432 |
def storage_key(self): |
| 433 |
"""Retrieve the storage account key""" |
| 434 |
|
| 435 |
return self.storage_client.storage_accounts.list_keys( |
| 436 |
resource_group_name=self.resource_group, |
| 437 |
account_name=self.storage_account |
| 438 |
).keys[0].value |
| 439 |
|
| 440 |
|
| 441 |
def main(): |
| 442 |
"""Main function execution""" |
| 443 |
|
| 444 |
AzureRMFunctionApp() |
| 445 |
|
| 446 |
|
| 447 |
if __name__ == '__main__': |
| 448 |
main() |