| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_securitygroup_info |
| 15 |
|
| 16 |
version_added: "0.1.2" |
| 17 |
|
| 18 |
short_description: Get security group facts |
| 19 |
|
| 20 |
description: |
| 21 |
- Get facts for a specific security group or all security groups within a resource group. |
| 22 |
|
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Only show results for a specific security group. |
| 27 |
type: str |
| 28 |
resource_group: |
| 29 |
description: |
| 30 |
- Name of the resource group to use. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
tags: |
| 34 |
description: |
| 35 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 36 |
type: list |
| 37 |
elements: str |
| 38 |
|
| 39 |
extends_documentation_fragment: |
| 40 |
- azure.azcollection.azure |
| 41 |
|
| 42 |
author: |
| 43 |
- Chris Houseknecht (@chouseknecht) |
| 44 |
- Matt Davis (@nitzmahone) |
| 45 |
|
| 46 |
''' |
| 47 |
|
| 48 |
EXAMPLES = ''' |
| 49 |
- name: Get facts for one security group |
| 50 |
azure_rm_securitygroup_info: |
| 51 |
resource_group: myResourceGroup |
| 52 |
name: secgroup001 |
| 53 |
|
| 54 |
- name: Get facts for all security groups |
| 55 |
azure_rm_securitygroup_info: |
| 56 |
resource_group: myResourceGroup |
| 57 |
''' |
| 58 |
|
| 59 |
RETURN = ''' |
| 60 |
securitygroups: |
| 61 |
description: |
| 62 |
- List containing security group dicts. |
| 63 |
returned: always |
| 64 |
type: complex |
| 65 |
contains: |
| 66 |
etag: |
| 67 |
description: |
| 68 |
- A unique read-only string that changes whenever the resource is updated. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: 'W/"d036f4d7-d977-429a-a8c6-879bc2523399"' |
| 72 |
id: |
| 73 |
description: |
| 74 |
- Resource ID. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/secgroup001" |
| 78 |
location: |
| 79 |
description: |
| 80 |
- Resource location. |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
sample: "eastus2" |
| 84 |
name: |
| 85 |
description: |
| 86 |
- Resource name. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: "secgroup001" |
| 90 |
default_rules: |
| 91 |
description: |
| 92 |
- The default security rules of network security group. |
| 93 |
returned: always |
| 94 |
type: list |
| 95 |
sample: [ |
| 96 |
{ |
| 97 |
"access": "Allow", |
| 98 |
"description": "Allow inbound traffic from all VMs in VNET", |
| 99 |
"destination_address_prefix": "VirtualNetwork", |
| 100 |
"destination_port_range": "*", |
| 101 |
"direction": "Inbound", |
| 102 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 103 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowVnetInBound", |
| 104 |
"name": "AllowVnetInBound", |
| 105 |
"priority": 65000, |
| 106 |
"protocol": "*", |
| 107 |
"provisioning_state": "Succeeded", |
| 108 |
"source_address_prefix": "VirtualNetwork", |
| 109 |
"source_port_range": "*" |
| 110 |
}, |
| 111 |
{ |
| 112 |
"access": "Allow", |
| 113 |
"description": "Allow inbound traffic from azure load balancer", |
| 114 |
"destination_address_prefix": "*", |
| 115 |
"destination_port_range": "*", |
| 116 |
"direction": "Inbound", |
| 117 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 118 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowAzureLoadBalancerInBound", |
| 119 |
"name": "AllowAzureLoadBalancerInBound", |
| 120 |
"priority": 65001, |
| 121 |
"protocol": "*", |
| 122 |
"provisioning_state": "Succeeded", |
| 123 |
"source_address_prefix": "AzureLoadBalancer", |
| 124 |
"source_port_range": "*" |
| 125 |
}, |
| 126 |
{ |
| 127 |
"access": "Deny", |
| 128 |
"description": "Deny all inbound traffic", |
| 129 |
"destination_address_prefix": "*", |
| 130 |
"destination_port_range": "*", |
| 131 |
"direction": "Inbound", |
| 132 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 133 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/DenyAllInBound", |
| 134 |
"name": "DenyAllInBound", |
| 135 |
"priority": 65500, |
| 136 |
"protocol": "*", |
| 137 |
"provisioning_state": "Succeeded", |
| 138 |
"source_address_prefix": "*", |
| 139 |
"source_port_range": "*" |
| 140 |
}, |
| 141 |
{ |
| 142 |
"access": "Allow", |
| 143 |
"description": "Allow outbound traffic from all VMs to all VMs in VNET", |
| 144 |
"destination_address_prefix": "VirtualNetwork", |
| 145 |
"destination_port_range": "*", |
| 146 |
"direction": "Outbound", |
| 147 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 148 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowVnetOutBound", |
| 149 |
"name": "AllowVnetOutBound", |
| 150 |
"priority": 65000, |
| 151 |
"protocol": "*", |
| 152 |
"provisioning_state": "Succeeded", |
| 153 |
"source_address_prefix": "VirtualNetwork", |
| 154 |
"source_port_range": "*" |
| 155 |
}, |
| 156 |
{ |
| 157 |
"access": "Allow", |
| 158 |
"description": "Allow outbound traffic from all VMs to Internet", |
| 159 |
"destination_address_prefix": "Internet", |
| 160 |
"destination_port_range": "*", |
| 161 |
"direction": "Outbound", |
| 162 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 163 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowInternetOutBound", |
| 164 |
"name": "AllowInternetOutBound", |
| 165 |
"priority": 65001, |
| 166 |
"protocol": "*", |
| 167 |
"provisioning_state": "Succeeded", |
| 168 |
"source_address_prefix": "*", |
| 169 |
"source_port_range": "*" |
| 170 |
}, |
| 171 |
{ |
| 172 |
"access": "Deny", |
| 173 |
"description": "Deny all outbound traffic", |
| 174 |
"destination_address_prefix": "*", |
| 175 |
"destination_port_range": "*", |
| 176 |
"direction": "Outbound", |
| 177 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 178 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/DenyAllOutBound", |
| 179 |
"name": "DenyAllOutBound", |
| 180 |
"priority": 65500, |
| 181 |
"protocol": "*", |
| 182 |
"provisioning_state": "Succeeded", |
| 183 |
"source_address_prefix": "*", |
| 184 |
"source_port_range": "*" |
| 185 |
} |
| 186 |
] |
| 187 |
network_interfaces: |
| 188 |
description: |
| 189 |
- A collection of references to network interfaces. |
| 190 |
returned: always |
| 191 |
type: list |
| 192 |
sample: [] |
| 193 |
rules: |
| 194 |
description: |
| 195 |
- A collection of security rules of the network security group. |
| 196 |
returned: always |
| 197 |
type: list |
| 198 |
sample: [ |
| 199 |
{ |
| 200 |
"access": "Deny", |
| 201 |
"description": null, |
| 202 |
"destination_address_prefix": "*", |
| 203 |
"destination_port_range": "22", |
| 204 |
"direction": "Inbound", |
| 205 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 206 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/securityRules/DenySSH", |
| 207 |
"name": "DenySSH", |
| 208 |
"priority": 100, |
| 209 |
"protocol": "Tcp", |
| 210 |
"provisioning_state": "Succeeded", |
| 211 |
"source_address_prefix": "*", |
| 212 |
"source_port_range": "*" |
| 213 |
}, |
| 214 |
{ |
| 215 |
"access": "Allow", |
| 216 |
"description": null, |
| 217 |
"destination_address_prefix": "*", |
| 218 |
"destination_port_range": "22", |
| 219 |
"direction": "Inbound", |
| 220 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 221 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/securityRules/AllowSSH", |
| 222 |
"name": "AllowSSH", |
| 223 |
"priority": 101, |
| 224 |
"protocol": "Tcp", |
| 225 |
"provisioning_state": "Succeeded", |
| 226 |
"source_address_prefix": "174.109.158.0/24", |
| 227 |
"source_port_range": "*" |
| 228 |
} |
| 229 |
] |
| 230 |
subnets: |
| 231 |
description: |
| 232 |
- A collection of references to subnets. |
| 233 |
returned: always |
| 234 |
type: list |
| 235 |
sample: [] |
| 236 |
tags: |
| 237 |
description: |
| 238 |
- Tags to assign to the security group. |
| 239 |
returned: always |
| 240 |
type: dict |
| 241 |
sample: { 'tag': 'value' } |
| 242 |
type: |
| 243 |
description: |
| 244 |
- Type of the resource. |
| 245 |
returned: always |
| 246 |
type: str |
| 247 |
sample: "Microsoft.Network/networkSecurityGroups" |
| 248 |
|
| 249 |
''' |
| 250 |
|
| 251 |
try: |
| 252 |
from azure.core.exceptions import ResourceNotFoundError |
| 253 |
except Exception: |
| 254 |
|
| 255 |
pass |
| 256 |
|
| 257 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 258 |
|
| 259 |
|
| 260 |
def create_rule_dict_from_obj(rule): |
| 261 |
return dict( |
| 262 |
id=rule.id, |
| 263 |
name=rule.name, |
| 264 |
description=rule.description, |
| 265 |
protocol=rule.protocol, |
| 266 |
source_port_range=rule.source_port_range, |
| 267 |
destination_port_range=rule.destination_port_range, |
| 268 |
source_address_prefix=rule.source_address_prefix, |
| 269 |
destination_address_prefix=rule.destination_address_prefix, |
| 270 |
source_port_ranges=rule.source_port_ranges, |
| 271 |
destination_port_ranges=rule.destination_port_ranges, |
| 272 |
source_address_prefixes=rule.source_address_prefixes, |
| 273 |
destination_address_prefixes=rule.destination_address_prefixes, |
| 274 |
source_application_security_groups=[p.id for p in rule.source_application_security_groups] if rule.source_application_security_groups else None, |
| 275 |
destination_application_security_groups=[ |
| 276 |
p.id for p in rule.destination_application_security_groups] if rule.destination_application_security_groups else None, |
| 277 |
access=rule.access, |
| 278 |
priority=rule.priority, |
| 279 |
direction=rule.direction, |
| 280 |
provisioning_state=rule.provisioning_state, |
| 281 |
etag=rule.etag |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
def create_network_security_group_dict(nsg): |
| 286 |
result = dict( |
| 287 |
etag=nsg.etag, |
| 288 |
id=nsg.id, |
| 289 |
location=nsg.location, |
| 290 |
name=nsg.name, |
| 291 |
tags=nsg.tags, |
| 292 |
type=nsg.type, |
| 293 |
) |
| 294 |
result['rules'] = [] |
| 295 |
if nsg.security_rules: |
| 296 |
for rule in nsg.security_rules: |
| 297 |
result['rules'].append(create_rule_dict_from_obj(rule)) |
| 298 |
|
| 299 |
result['default_rules'] = [] |
| 300 |
if nsg.default_security_rules: |
| 301 |
for rule in nsg.default_security_rules: |
| 302 |
result['default_rules'].append(create_rule_dict_from_obj(rule)) |
| 303 |
|
| 304 |
result['network_interfaces'] = [] |
| 305 |
if nsg.network_interfaces: |
| 306 |
for interface in nsg.network_interfaces: |
| 307 |
result['network_interfaces'].append(interface.id) |
| 308 |
|
| 309 |
result['subnets'] = [] |
| 310 |
if nsg.subnets: |
| 311 |
for subnet in nsg.subnets: |
| 312 |
result['subnets'].append(subnet.id) |
| 313 |
|
| 314 |
return result |
| 315 |
|
| 316 |
|
| 317 |
class AzureRMSecurityGroupInfo(AzureRMModuleBase): |
| 318 |
|
| 319 |
def __init__(self): |
| 320 |
|
| 321 |
self.module_arg_spec = dict( |
| 322 |
name=dict(type='str'), |
| 323 |
resource_group=dict(required=True, type='str'), |
| 324 |
tags=dict(type='list', elements='str'), |
| 325 |
) |
| 326 |
|
| 327 |
self.results = dict( |
| 328 |
changed=False, |
| 329 |
) |
| 330 |
|
| 331 |
self.name = None |
| 332 |
self.resource_group = None |
| 333 |
self.tags = None |
| 334 |
|
| 335 |
super(AzureRMSecurityGroupInfo, self).__init__(self.module_arg_spec, |
| 336 |
supports_check_mode=True, |
| 337 |
supports_tags=False, |
| 338 |
facts_module=True) |
| 339 |
|
| 340 |
def exec_module(self, **kwargs): |
| 341 |
|
| 342 |
for key in self.module_arg_spec: |
| 343 |
setattr(self, key, kwargs[key]) |
| 344 |
|
| 345 |
if self.name is not None: |
| 346 |
info = self.get_item() |
| 347 |
else: |
| 348 |
info = self.list_items() |
| 349 |
|
| 350 |
self.results['securitygroups'] = info |
| 351 |
|
| 352 |
return self.results |
| 353 |
|
| 354 |
def get_item(self): |
| 355 |
self.log('Get properties for {0}'.format(self.name)) |
| 356 |
item = None |
| 357 |
result = [] |
| 358 |
|
| 359 |
try: |
| 360 |
item = self.network_client.network_security_groups.get(self.resource_group, self.name) |
| 361 |
except ResourceNotFoundError: |
| 362 |
pass |
| 363 |
|
| 364 |
if item and self.has_tags(item.tags, self.tags): |
| 365 |
result = [create_network_security_group_dict(item)] |
| 366 |
|
| 367 |
return result |
| 368 |
|
| 369 |
def list_items(self): |
| 370 |
self.log('List all items') |
| 371 |
try: |
| 372 |
response = self.network_client.network_security_groups.list(self.resource_group) |
| 373 |
except Exception as exc: |
| 374 |
self.fail("Error listing all items - {0}".format(str(exc))) |
| 375 |
|
| 376 |
results = [] |
| 377 |
for item in response: |
| 378 |
if self.has_tags(item.tags, self.tags): |
| 379 |
results.append(create_network_security_group_dict(item)) |
| 380 |
return results |
| 381 |
|
| 382 |
|
| 383 |
def main(): |
| 384 |
AzureRMSecurityGroupInfo() |
| 385 |
|
| 386 |
|
| 387 |
if __name__ == '__main__': |
| 388 |
main() |