| 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_networkflowlog |
| 14 |
version_added: "2.5.0" |
| 15 |
short_description: Manage the network flow logs |
| 16 |
description: |
| 17 |
- Create, update or delete the network flow logs. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
location: |
| 25 |
description: |
| 26 |
- Valid Azure location. Defaults to location of the resource group. |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the network flow logs. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
network_watcher_name: |
| 34 |
description: |
| 35 |
- The name of the network watcher. |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
target_resource_id: |
| 39 |
description: |
| 40 |
- ID of network security group to which flow log will be applied. |
| 41 |
type: str |
| 42 |
storage_id: |
| 43 |
description: |
| 44 |
- ID of the storage account which is used to store the flow log. |
| 45 |
type: str |
| 46 |
enabled: |
| 47 |
description: |
| 48 |
- Flag to enable/disable flow logging. |
| 49 |
type: bool |
| 50 |
retention_policy: |
| 51 |
description: |
| 52 |
- Parameters that define the retention policy for flow log. |
| 53 |
type: dict |
| 54 |
suboptions: |
| 55 |
days: |
| 56 |
description: |
| 57 |
- Number of days to retain flow log records. |
| 58 |
type: int |
| 59 |
enabled: |
| 60 |
description: |
| 61 |
- Flag to enable/disable retention. |
| 62 |
type: bool |
| 63 |
flow_analytics_configuration: |
| 64 |
description: |
| 65 |
- Parameters that define the configuration of traffic analytics. |
| 66 |
type: dict |
| 67 |
suboptions: |
| 68 |
network_watcher_flow_analytics_configuration: |
| 69 |
description: |
| 70 |
- Parameters that define the configuration of traffic analytics. |
| 71 |
type: dict |
| 72 |
suboptions: |
| 73 |
enabled: |
| 74 |
description: |
| 75 |
- Flag to enable/disable traffic analytics. |
| 76 |
type: bool |
| 77 |
workspace_id: |
| 78 |
description: |
| 79 |
- The resource guid of the attached workspace. |
| 80 |
type: str |
| 81 |
workspace_region: |
| 82 |
description: |
| 83 |
- The location of the attached workspace. |
| 84 |
type: str |
| 85 |
workspace_resource_id: |
| 86 |
description: |
| 87 |
- Resource Id of the attached workspace. |
| 88 |
type: str |
| 89 |
traffic_analytics_interval: |
| 90 |
description: |
| 91 |
- The interval in minutes which would decide how frequently TA service should do flow analytics. |
| 92 |
type: int |
| 93 |
choices: |
| 94 |
- 10 |
| 95 |
- 60 |
| 96 |
state: |
| 97 |
description: |
| 98 |
- State of the Flow Logs. Use C(present) to create or update and C(absent) to delete. |
| 99 |
default: present |
| 100 |
type: str |
| 101 |
choices: |
| 102 |
- absent |
| 103 |
- present |
| 104 |
|
| 105 |
extends_documentation_fragment: |
| 106 |
- azure.azcollection.azure |
| 107 |
- azure.azcollection.azure_tags |
| 108 |
|
| 109 |
author: |
| 110 |
- xuzhang3 (@xuzhang3) |
| 111 |
- Fred-sun (@Fred-sun) |
| 112 |
|
| 113 |
''' |
| 114 |
|
| 115 |
EXAMPLES = ''' |
| 116 |
- name: Create network flow log |
| 117 |
azure_rm_networkflowlog: |
| 118 |
resource_group: NetworkWatcherRG |
| 119 |
network_watcher_name: NetworkWatcher_eastus |
| 120 |
name: xz3mlwvnet-xz3mlwaiserv-flowlog02 |
| 121 |
enabled: false |
| 122 |
flow_analytics_configuration: |
| 123 |
network_watcher_flow_analytics_configuration: |
| 124 |
enabled: false |
| 125 |
traffic_analytics_interval: 60 |
| 126 |
workspace_id: 7c16a8dd-b983-4f75-b78b-a804c169306c |
| 127 |
workspace_region: eastus |
| 128 |
workspace_resource_id: "/subscriptions/xxx-xxx/resourceGroups/DefaultRG-EUS/providers/Microsoft.OperationalInsights/workspaces/DeWorkspace-0-EUS" |
| 129 |
retention_policy: |
| 130 |
days: 2 |
| 131 |
enabled: true |
| 132 |
storage_id: "/subscriptions/xxx-xxx/resourceGroups/AutoTagFunctionAppRG/providers/Microsoft.Storage/storageAccounts/autotagfunctionappr9a08" |
| 133 |
target_resource_id: "/subscriptions/xxx-xxx/resourceGroups/xz3mlwaiserv/providers/Microsoft.Network/virtualNetworks/xz3mlwvnet" |
| 134 |
tags: |
| 135 |
key2: value2 |
| 136 |
key5: value5 |
| 137 |
|
| 138 |
- name: Delete a Flow Logs |
| 139 |
azure_rm_networkflowlog: |
| 140 |
resource_group: myResourceGroup |
| 141 |
network_watcher_name: testwatcher |
| 142 |
name: myNetflowlog |
| 143 |
state: absent |
| 144 |
''' |
| 145 |
RETURN = ''' |
| 146 |
state: |
| 147 |
description: |
| 148 |
- The facts of the network flow logs. |
| 149 |
returned: always |
| 150 |
type: complex |
| 151 |
contains: |
| 152 |
resource_group: |
| 153 |
description: |
| 154 |
- The resource group. |
| 155 |
type: str |
| 156 |
returned: always |
| 157 |
sample: NetworkWatcherRG |
| 158 |
id: |
| 159 |
description: |
| 160 |
- Resource ID. |
| 161 |
returned: always |
| 162 |
type: str |
| 163 |
sample: /subscriptions/xxx-xxx/resourceGroups/NetWatcherRG/providers/Microsoft.Network/networkWatchers/NetWatcher_eastus/flowLogs/xz-flowlog" |
| 164 |
location: |
| 165 |
description: |
| 166 |
- Resource location. |
| 167 |
returned: always |
| 168 |
type: str |
| 169 |
sample: eastus |
| 170 |
name: |
| 171 |
description: |
| 172 |
- Resource name. |
| 173 |
returned: always |
| 174 |
type: str |
| 175 |
sample: xz-flowlog |
| 176 |
network_watcher_name: |
| 177 |
description: |
| 178 |
- The name of the network watcher. |
| 179 |
type: str |
| 180 |
returned: always |
| 181 |
sample: NetWatcher_eastus |
| 182 |
target_resource_id: |
| 183 |
description: |
| 184 |
- ID of network security group to which flow log will be applied. |
| 185 |
type: str |
| 186 |
returned: always |
| 187 |
sample: /subscriptions/xxx-xxx/resourceGroups/xz3mlwaiserv/providers/Microsoft.Network/virtualNetworks/xz3mlwvnet" |
| 188 |
storage_id: |
| 189 |
description: |
| 190 |
- ID of the storage account which is used to store the flow log. |
| 191 |
type: str |
| 192 |
returned: always |
| 193 |
sample: "/subscriptions/xxx-xxx/resourceGroups/AutoTagFunctionAppRG/providers/Microsoft.Storage/storageAccounts/autotagfunc01" |
| 194 |
enanbled: |
| 195 |
description: |
| 196 |
- Flag to enable/disable flow logging. |
| 197 |
type: str |
| 198 |
returned: always |
| 199 |
sample: true |
| 200 |
retention_policy: |
| 201 |
description: |
| 202 |
- Parameters that define the retention policy for flow log. |
| 203 |
type: complex |
| 204 |
returned: always |
| 205 |
contains: |
| 206 |
day: |
| 207 |
description: |
| 208 |
- Number of days to retain flow log records. |
| 209 |
type: int |
| 210 |
returned: always |
| 211 |
sample: 0 |
| 212 |
enabled: |
| 213 |
description: |
| 214 |
- Flag to enable/disable retention. |
| 215 |
type: bool |
| 216 |
returned: always |
| 217 |
sample: false |
| 218 |
flow_analytics_configuration: |
| 219 |
description: |
| 220 |
- Parameters that define the configuration of traffic analytics. |
| 221 |
type: complex |
| 222 |
returned: always |
| 223 |
contains: |
| 224 |
network_watcher_flow_analytics_configuration: |
| 225 |
description: |
| 226 |
- Parameters that define the configuration of traffic analytics. |
| 227 |
type: complex |
| 228 |
returned: always |
| 229 |
contains: |
| 230 |
enabled: |
| 231 |
description: |
| 232 |
- Flag to enable/disable traffic analytics. |
| 233 |
type: bool |
| 234 |
returned: always |
| 235 |
sample: true |
| 236 |
workspace_id: |
| 237 |
description: |
| 238 |
- The resource guid of the attached workspace. |
| 239 |
type: str |
| 240 |
returned: always |
| 241 |
sample: 7c16a8dd-b983-4f75-b78b-a804c169306c |
| 242 |
workspace_region: |
| 243 |
description: |
| 244 |
- The location of the attached workspace. |
| 245 |
type: str |
| 246 |
returned: always |
| 247 |
sample: eastus |
| 248 |
workspace_resource_id: |
| 249 |
description: |
| 250 |
- Resource Id of the attached workspace. |
| 251 |
type: str |
| 252 |
returned: always |
| 253 |
sample: /subscriptions/xxx-xxx/resourceGroups/DefaulUS/providers/Microsoft.OperationalInsights/workspaces/DefaultWorkspace-0-EUS" |
| 254 |
traffic_analytics_interval: |
| 255 |
description: |
| 256 |
- The interval in minutes which would decide how frequently TA service should do flow analytics. |
| 257 |
type: str |
| 258 |
returned: always |
| 259 |
sample: 60 |
| 260 |
tags: |
| 261 |
description: |
| 262 |
- Resource tags. |
| 263 |
returned: always |
| 264 |
type: dict |
| 265 |
sample: { 'key1':'value1' } |
| 266 |
type: |
| 267 |
description: |
| 268 |
- Resource type. |
| 269 |
returned: always |
| 270 |
type: str |
| 271 |
sample: "Microsoft.Network/networkWatchers/flowLogs" |
| 272 |
provisioning_state: |
| 273 |
description: |
| 274 |
- The provisioning state of the network flow logs resource. |
| 275 |
type: str |
| 276 |
returned: always |
| 277 |
sample: Succeeded |
| 278 |
''' |
| 279 |
|
| 280 |
try: |
| 281 |
from azure.core.exceptions import ResourceNotFoundError |
| 282 |
except ImportError: |
| 283 |
|
| 284 |
pass |
| 285 |
|
| 286 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 287 |
|
| 288 |
|
| 289 |
class AzureRMNetworkFlowLog(AzureRMModuleBaseExt): |
| 290 |
|
| 291 |
def __init__(self): |
| 292 |
|
| 293 |
self.module_arg_spec = dict( |
| 294 |
resource_group=dict(type='str', required=True), |
| 295 |
network_watcher_name=dict(type='str', required=True), |
| 296 |
name=dict(type='str', required=True), |
| 297 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 298 |
location=dict(type='str'), |
| 299 |
target_resource_id=dict(type='str'), |
| 300 |
storage_id=dict(type='str'), |
| 301 |
enabled=dict(type='bool'), |
| 302 |
retention_policy=dict( |
| 303 |
type='dict', |
| 304 |
options=dict( |
| 305 |
days=dict(type='int'), |
| 306 |
enabled=dict(type='bool'), |
| 307 |
), |
| 308 |
), |
| 309 |
flow_analytics_configuration=dict( |
| 310 |
type='dict', |
| 311 |
options=dict( |
| 312 |
network_watcher_flow_analytics_configuration=dict( |
| 313 |
type='dict', |
| 314 |
options=dict( |
| 315 |
enabled=dict(type='bool'), |
| 316 |
workspace_id=dict(type='str'), |
| 317 |
workspace_region=dict(type='str'), |
| 318 |
workspace_resource_id=dict(type='str'), |
| 319 |
traffic_analytics_interval=dict(type='int', choices=[10, 60]) |
| 320 |
) |
| 321 |
) |
| 322 |
) |
| 323 |
), |
| 324 |
) |
| 325 |
|
| 326 |
self.resource_group = None |
| 327 |
self.network_watcher_name = None |
| 328 |
self.name = None |
| 329 |
self.state = None |
| 330 |
self.location = None |
| 331 |
self.tags = None |
| 332 |
self.body = dict() |
| 333 |
|
| 334 |
self.results = dict( |
| 335 |
changed=False, |
| 336 |
state=dict() |
| 337 |
) |
| 338 |
|
| 339 |
super(AzureRMNetworkFlowLog, self).__init__(self.module_arg_spec, |
| 340 |
supports_tags=True, |
| 341 |
supports_check_mode=True) |
| 342 |
|
| 343 |
def exec_module(self, **kwargs): |
| 344 |
|
| 345 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 346 |
if hasattr(self, key): |
| 347 |
setattr(self, key, kwargs[key]) |
| 348 |
elif kwargs[key] is not None: |
| 349 |
self.body[key] = kwargs[key] |
| 350 |
|
| 351 |
resource_group = self.get_resource_group(self.resource_group) |
| 352 |
if self.location is not None: |
| 353 |
self.body['location'] = self.location |
| 354 |
else: |
| 355 |
|
| 356 |
self.body['location'] = resource_group.location |
| 357 |
self.body['tags'] = self.tags |
| 358 |
|
| 359 |
changed = False |
| 360 |
results = dict() |
| 361 |
|
| 362 |
old_response = self.get_by_name() |
| 363 |
|
| 364 |
if old_response is not None: |
| 365 |
if self.state == 'present': |
| 366 |
if self.body.get('retention_policy') is not None and\ |
| 367 |
not self.default_compare({}, self.body.get('retention_policy'), old_response.get('retention_policy'), '', dict(compare=[])): |
| 368 |
changed = True |
| 369 |
elif (self.body.get('flow_analytics_configuration') is not None and not self.default_compare( |
| 370 |
{}, self.body['flow_analytics_configuration'], old_response['flow_analytics_configuration'], '', dict(compare=[]))): |
| 371 |
changed = True |
| 372 |
|
| 373 |
elif self.body.get('enabled') is not None and bool(self.body['enabled']) != bool(old_response.get('enabled')): |
| 374 |
changed = True |
| 375 |
if changed: |
| 376 |
results = self.create_or_update(self.body) |
| 377 |
else: |
| 378 |
results = old_response |
| 379 |
|
| 380 |
update_tags, new_tags = self.update_tags(old_response['tags']) |
| 381 |
if update_tags: |
| 382 |
changed = True |
| 383 |
if not self.check_mode: |
| 384 |
results = self.update_flowlog_tags(new_tags) |
| 385 |
else: |
| 386 |
changed = True |
| 387 |
if not self.check_mode: |
| 388 |
results = self.delete_flowlog() |
| 389 |
else: |
| 390 |
if self.state == 'present': |
| 391 |
changed = True |
| 392 |
if not self.check_mode: |
| 393 |
results = self.create_or_update(self.body) |
| 394 |
else: |
| 395 |
changed = False |
| 396 |
self.log("The Flow Log is not exists") |
| 397 |
|
| 398 |
self.results['changed'] = changed |
| 399 |
self.results['state'] = results |
| 400 |
|
| 401 |
return self.results |
| 402 |
|
| 403 |
def get_by_name(self): |
| 404 |
response = None |
| 405 |
try: |
| 406 |
response = self.network_client.flow_logs.get(self.resource_group, self.network_watcher_name, self.name) |
| 407 |
|
| 408 |
except ResourceNotFoundError as exec: |
| 409 |
self.log("Failed to get network flow log, Exception as {0}".format(exec)) |
| 410 |
|
| 411 |
return self.to_dict(response) |
| 412 |
|
| 413 |
def create_or_update(self, body): |
| 414 |
response = None |
| 415 |
try: |
| 416 |
response = self.network_client.flow_logs.begin_create_or_update(self.resource_group, self.network_watcher_name, self.name, body) |
| 417 |
response = self.get_poller_result(response) |
| 418 |
except Exception as exc: |
| 419 |
self.fail("Error creating Flow Log {0} - {1}".format(self.name, str(exc))) |
| 420 |
|
| 421 |
return self.to_dict(response) |
| 422 |
|
| 423 |
def update_flowlog_tags(self, tags): |
| 424 |
response = None |
| 425 |
try: |
| 426 |
response = self.network_client.flow_logs.update_tags(self.resource_group, self.network_watcher_name, self.name, dict(tags=tags)) |
| 427 |
except Exception as exc: |
| 428 |
self.fail("Error updating Flow Logs {0} - {1}".format(self.name, str(exc))) |
| 429 |
return self.to_dict(response) |
| 430 |
|
| 431 |
def delete_flowlog(self): |
| 432 |
try: |
| 433 |
self.network_client.flow_logs.begin_delete(self.resource_group, self.network_watcher_name, self.name) |
| 434 |
except Exception as exc: |
| 435 |
self.fail("Error deleting Flow Logs {0} - {1}".format(self.name, str(exc))) |
| 436 |
|
| 437 |
def to_dict(self, body): |
| 438 |
results = dict() |
| 439 |
if body is not None: |
| 440 |
results = dict( |
| 441 |
resource_group=self.resource_group, |
| 442 |
network_watcher_name=self.network_watcher_name, |
| 443 |
id=body.id, |
| 444 |
name=body.name, |
| 445 |
location=body.location, |
| 446 |
tags=body.tags, |
| 447 |
type=body.type, |
| 448 |
provisioning_state=body.provisioning_state, |
| 449 |
target_resource_id=body.target_resource_id, |
| 450 |
storage_id=body.storage_id, |
| 451 |
enabled=body.enabled, |
| 452 |
retention_policy=dict(), |
| 453 |
flow_analytics_configuration=dict() |
| 454 |
) |
| 455 |
if body.retention_policy is not None: |
| 456 |
results['retention_policy']['days'] = body.retention_policy.days |
| 457 |
results['retention_policy']['enabled'] = body.retention_policy.enabled |
| 458 |
if body.flow_analytics_configuration is not None: |
| 459 |
results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration'] = dict() |
| 460 |
if body.flow_analytics_configuration.network_watcher_flow_analytics_configuration is not None: |
| 461 |
new_config = body.flow_analytics_configuration.network_watcher_flow_analytics_configuration |
| 462 |
results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['enabled'] = new_config.enabled |
| 463 |
results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['workspace_id'] = new_config.workspace_id |
| 464 |
results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['workspace_region'] = new_config.workspace_region |
| 465 |
results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['workspace_resource_id'] = \ |
| 466 |
new_config.workspace_resource_id |
| 467 |
results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['traffic_analytics_interval'] = \ |
| 468 |
new_config.traffic_analytics_interval |
| 469 |
|
| 470 |
return results |
| 471 |
return None |
| 472 |
|
| 473 |
|
| 474 |
def main(): |
| 475 |
AzureRMNetworkFlowLog() |
| 476 |
|
| 477 |
|
| 478 |
if __name__ == '__main__': |
| 479 |
main() |