| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, division, print_function |
| 5 |
__metaclass__ = type |
| 6 |
|
| 7 |
|
| 8 |
DOCUMENTATION = ''' |
| 9 |
--- |
| 10 |
module: azure_rm_virtualnetworkgateway |
| 11 |
|
| 12 |
version_added: "0.0.1" |
| 13 |
|
| 14 |
short_description: Manage Azure virtual network gateways |
| 15 |
|
| 16 |
description: |
| 17 |
- Create, update or delete a virtual network gateway(VPN Gateway). |
| 18 |
- When creating a VPN Gateway you must provide the name of an existing virtual network. |
| 19 |
|
| 20 |
options: |
| 21 |
resource_group: |
| 22 |
description: |
| 23 |
- Name of a resource group where VPN Gateway exists or will be created. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
name: |
| 27 |
description: |
| 28 |
- Name of VPN Gateway. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
state: |
| 32 |
description: |
| 33 |
- State of the VPN Gateway. Use C(present) to create or update VPN gateway and C(absent) to delete VPN gateway. |
| 34 |
default: present |
| 35 |
type: str |
| 36 |
choices: |
| 37 |
- absent |
| 38 |
- present |
| 39 |
required: false |
| 40 |
location: |
| 41 |
description: |
| 42 |
- Valid Azure location. Defaults to location of the resource group. |
| 43 |
type: str |
| 44 |
virtual_network: |
| 45 |
description: |
| 46 |
- An existing virtual network with which the VPN Gateway will be associated. |
| 47 |
- Required when creating a VPN Gateway. |
| 48 |
- Can be the name of the virtual network. |
| 49 |
- Must be in the same resource group as VPN gateway when specified by name. |
| 50 |
- Can be the resource ID of the virtual network. |
| 51 |
- Can be a dict which contains I(name) and I(resource_group) of the virtual network. |
| 52 |
- Required when creating. |
| 53 |
type: raw |
| 54 |
aliases: |
| 55 |
- virtual_network_name |
| 56 |
ip_configurations: |
| 57 |
description: |
| 58 |
- List of IP configurations. |
| 59 |
type: list |
| 60 |
elements: dict |
| 61 |
suboptions: |
| 62 |
name: |
| 63 |
description: |
| 64 |
- Name of the IP configuration. |
| 65 |
type: str |
| 66 |
required: true |
| 67 |
private_ip_allocation_method: |
| 68 |
description: |
| 69 |
- Private IP allocation method. |
| 70 |
type: str |
| 71 |
choices: |
| 72 |
- Dynamic |
| 73 |
- Static |
| 74 |
default: Dynamic |
| 75 |
public_ip_address_name: |
| 76 |
description: |
| 77 |
- Name of the public IP address. Use 'None' to disable the public IP address. |
| 78 |
type: str |
| 79 |
subnet: |
| 80 |
description: |
| 81 |
- ID of the gateway subnet for VPN. |
| 82 |
type: str |
| 83 |
gateway_type: |
| 84 |
description: |
| 85 |
- The type of this virtual network gateway. |
| 86 |
default: vpn |
| 87 |
type: str |
| 88 |
choices: |
| 89 |
- vpn |
| 90 |
- express_route |
| 91 |
vpn_type: |
| 92 |
description: |
| 93 |
- The type of this virtual private network. |
| 94 |
default: route_based |
| 95 |
type: str |
| 96 |
choices: |
| 97 |
- route_based |
| 98 |
- policy_based |
| 99 |
enable_bgp: |
| 100 |
description: |
| 101 |
- Whether BGP is enabled for this virtual network gateway or not. |
| 102 |
default: false |
| 103 |
type: bool |
| 104 |
sku: |
| 105 |
description: |
| 106 |
- The reference of the VirtualNetworkGatewaySku resource which represents the SKU selected for Virtual network gateway. |
| 107 |
default: VpnGw1AZ |
| 108 |
type: str |
| 109 |
choices: |
| 110 |
- VpnGw1 |
| 111 |
- VpnGw2 |
| 112 |
- VpnGw3 |
| 113 |
- Standard |
| 114 |
- Basic |
| 115 |
- HighPerformance |
| 116 |
- VpnGw1AZ |
| 117 |
- VpnGw2AZ |
| 118 |
- VpnGw3AZ |
| 119 |
- VpnGw4AZ |
| 120 |
- VpnGw5AZ |
| 121 |
- ErGw1AZ |
| 122 |
- ErGw2AZ |
| 123 |
- ErGw3AZ |
| 124 |
- ErGwScale |
| 125 |
vpn_gateway_generation: |
| 126 |
description: |
| 127 |
- The generation for this VirtualNetworkGateway. Must be C(None) if C(gateway_type) is not VPN. |
| 128 |
default: Generation1 |
| 129 |
type: str |
| 130 |
choices: |
| 131 |
- None |
| 132 |
- Generation1 |
| 133 |
- Generation2 |
| 134 |
bgp_settings: |
| 135 |
description: |
| 136 |
- Virtual network gateway's BGP speaker settings. |
| 137 |
type: dict |
| 138 |
suboptions: |
| 139 |
asn: |
| 140 |
description: |
| 141 |
- The BGP speaker's ASN. |
| 142 |
type: int |
| 143 |
required: True |
| 144 |
|
| 145 |
extends_documentation_fragment: |
| 146 |
- azure.azcollection.azure |
| 147 |
- azure.azcollection.azure_tags |
| 148 |
|
| 149 |
author: |
| 150 |
- Madhura Naniwadekar (@Madhura-CSI) |
| 151 |
''' |
| 152 |
|
| 153 |
EXAMPLES = ''' |
| 154 |
- name: Create virtual network gateway without bgp settings |
| 155 |
azure_rm_virtualnetworkgateway: |
| 156 |
resource_group: myResourceGroup |
| 157 |
name: myVirtualNetworkGateway |
| 158 |
ip_configurations: |
| 159 |
- name: testipconfig |
| 160 |
private_ip_allocation_method: Dynamic |
| 161 |
public_ip_address_name: testipaddr |
| 162 |
virtual_network: myVirtualNetwork |
| 163 |
tags: |
| 164 |
common: "xyz" |
| 165 |
|
| 166 |
- name: Create virtual network gateway Generation2 |
| 167 |
azure_rm_virtualnetworkgateway: |
| 168 |
resource_group: myResourceGroup |
| 169 |
name: myVirtualNetworkGateway |
| 170 |
sku: vpn_gw2 |
| 171 |
vpn_gateway_generation: Generation2 |
| 172 |
ip_configurations: |
| 173 |
- name: testipconfig |
| 174 |
private_ip_allocation_method: Dynamic |
| 175 |
public_ip_address_name: testipaddr |
| 176 |
virtual_network: myVirtualNetwork |
| 177 |
tags: |
| 178 |
common: "xyz" |
| 179 |
|
| 180 |
- name: Create virtual network gateway with bgp |
| 181 |
azure_rm_virtualnetworkgateway: |
| 182 |
resource_group: myResourceGroup |
| 183 |
name: myVirtualNetworkGateway |
| 184 |
sku: vpn_gw1 |
| 185 |
ip_configurations: |
| 186 |
- name: testipconfig |
| 187 |
private_ip_allocation_method: Dynamic |
| 188 |
public_ip_address_name: testipaddr |
| 189 |
enable_bgp: true |
| 190 |
virtual_network: myVirtualNetwork |
| 191 |
bgp_settings: |
| 192 |
asn: 65515 |
| 193 |
bgp_peering_address: "169.254.54.209" |
| 194 |
tags: |
| 195 |
common: "xyz" |
| 196 |
|
| 197 |
- name: Delete instance of virtual network gateway |
| 198 |
azure_rm_virtualnetworkgateway: |
| 199 |
resource_group: myResourceGroup |
| 200 |
name: myVirtualNetworkGateway |
| 201 |
state: absent |
| 202 |
''' |
| 203 |
|
| 204 |
RETURN = ''' |
| 205 |
id: |
| 206 |
description: |
| 207 |
- Virtual Network Gateway resource ID. |
| 208 |
returned: always |
| 209 |
type: str |
| 210 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworkGateways/myV |
| 211 |
irtualNetworkGateway" |
| 212 |
''' |
| 213 |
|
| 214 |
try: |
| 215 |
from azure.core.exceptions import ResourceNotFoundError |
| 216 |
except ImportError: |
| 217 |
|
| 218 |
pass |
| 219 |
|
| 220 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 221 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel |
| 222 |
|
| 223 |
|
| 224 |
AZURE_VPN_GATEWAY_OBJECT_CLASS = 'VirtualNetworkGateway' |
| 225 |
|
| 226 |
|
| 227 |
ip_configuration_spec = dict( |
| 228 |
name=dict(type='str', required=True), |
| 229 |
private_ip_allocation_method=dict(type='str', choices=['Dynamic', 'Static'], default='Dynamic'), |
| 230 |
subnet=dict(type='str'), |
| 231 |
public_ip_address_name=dict(type='str'), |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
sku_spec = dict( |
| 236 |
name=dict(type='str', default='VpnGw1AZ'), |
| 237 |
tier=dict(type='str', default='VpnGw1AZ') |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
bgp_spec = dict( |
| 242 |
asn=dict(type='int', required=True), |
| 243 |
) |
| 244 |
|
| 245 |
|
| 246 |
def vgw_to_dict(vgw): |
| 247 |
results = dict( |
| 248 |
id=vgw.id, |
| 249 |
name=vgw.name, |
| 250 |
location=vgw.location, |
| 251 |
gateway_type=vgw.gateway_type, |
| 252 |
vpn_type=vgw.vpn_type, |
| 253 |
vpn_gateway_generation=vgw.vpn_gateway_generation, |
| 254 |
enable_bgp=vgw.enable_bgp, |
| 255 |
tags=vgw.tags, |
| 256 |
provisioning_state=vgw.provisioning_state, |
| 257 |
sku=dict( |
| 258 |
name=vgw.sku.name, |
| 259 |
tier=vgw.sku.tier |
| 260 |
), |
| 261 |
bgp_settings=dict( |
| 262 |
asn=vgw.bgp_settings.asn, |
| 263 |
bgp_peering_address=vgw.bgp_settings.bgp_peering_address, |
| 264 |
peer_weight=vgw.bgp_settings.peer_weight |
| 265 |
) if vgw.bgp_settings else None, |
| 266 |
etag=vgw.etag |
| 267 |
) |
| 268 |
return results |
| 269 |
|
| 270 |
|
| 271 |
class AzureRMVirtualNetworkGateway(AzureRMModuleBase): |
| 272 |
|
| 273 |
def __init__(self): |
| 274 |
|
| 275 |
self.module_arg_spec = dict( |
| 276 |
resource_group=dict(type='str', required=True), |
| 277 |
name=dict(type='str', required=True), |
| 278 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 279 |
location=dict(type='str'), |
| 280 |
ip_configurations=dict(type='list', default=None, elements='dict', options=ip_configuration_spec), |
| 281 |
gateway_type=dict(type='str', default='vpn', choices=['vpn', 'express_route']), |
| 282 |
vpn_type=dict(type='str', default='route_based', choices=['route_based', 'policy_based']), |
| 283 |
vpn_gateway_generation=dict(type='str', default='Generation1', choices=['None', 'Generation1', 'Generation2']), |
| 284 |
enable_bgp=dict(type='bool', default=False), |
| 285 |
sku=dict(type='str', default='VpnGw1AZ', choices=[ |
| 286 |
'VpnGw1', 'VpnGw2', 'VpnGw3', 'Standard', 'Basic', 'HighPerformance', |
| 287 |
'VpnGw1AZ', 'VpnGw2AZ', 'VpnGw3AZ', 'VpnGw4AZ', 'VpnGw5AZ', |
| 288 |
'ErGw1AZ', 'ErGw2AZ', 'ErGw3AZ', 'ErGwScale' |
| 289 |
]), |
| 290 |
bgp_settings=dict(type='dict', options=bgp_spec), |
| 291 |
virtual_network=dict(type='raw', aliases=['virtual_network_name']) |
| 292 |
) |
| 293 |
|
| 294 |
self.resource_group = None |
| 295 |
self.name = None |
| 296 |
self.state = None |
| 297 |
self.location = None |
| 298 |
self.ip_configurations = None |
| 299 |
self.gateway_type = None |
| 300 |
self.vpn_type = None |
| 301 |
self.enable_bgp = None |
| 302 |
self.sku = None |
| 303 |
self.vpn_gateway_generation = None |
| 304 |
self.bgp_settings = None |
| 305 |
|
| 306 |
required_if = [('state', 'present', ['virtual_network'])] |
| 307 |
self.results = dict( |
| 308 |
changed=False, |
| 309 |
state=dict() |
| 310 |
) |
| 311 |
|
| 312 |
super(AzureRMVirtualNetworkGateway, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 313 |
required_if=required_if, |
| 314 |
supports_check_mode=True) |
| 315 |
|
| 316 |
def exec_module(self, **kwargs): |
| 317 |
|
| 318 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 319 |
setattr(self, key, kwargs[key]) |
| 320 |
|
| 321 |
changed = False |
| 322 |
results = dict() |
| 323 |
vgw = None |
| 324 |
|
| 325 |
resource_group = self.get_resource_group(self.resource_group) |
| 326 |
if not self.location: |
| 327 |
self.location = resource_group.location |
| 328 |
|
| 329 |
self.virtual_network = self.parse_resource_to_dict(self.virtual_network) |
| 330 |
resource_group = self.get_resource_group(self.resource_group) |
| 331 |
|
| 332 |
try: |
| 333 |
vgw = self.network_client.virtual_network_gateways.get(self.resource_group, self.name) |
| 334 |
if self.state == 'absent': |
| 335 |
self.log("CHANGED: vnet exists but requested state is 'absent'") |
| 336 |
changed = True |
| 337 |
except ResourceNotFoundError: |
| 338 |
if self.state == 'present': |
| 339 |
self.log("CHANGED: VPN Gateway {0} does not exist but requested state is 'present'".format(self.name)) |
| 340 |
changed = True |
| 341 |
|
| 342 |
if vgw: |
| 343 |
results = vgw_to_dict(vgw) |
| 344 |
if self.state == 'present': |
| 345 |
update_tags, results['tags'] = self.update_tags(results['tags']) |
| 346 |
if update_tags: |
| 347 |
changed = True |
| 348 |
sku = dict(name=self.sku, tier=self.sku) |
| 349 |
if sku != results['sku']: |
| 350 |
changed = True |
| 351 |
if self.enable_bgp != results['enable_bgp']: |
| 352 |
changed = True |
| 353 |
if self.bgp_settings and self.bgp_settings['asn'] != results['bgp_settings']['asn']: |
| 354 |
changed = True |
| 355 |
|
| 356 |
self.results['changed'] = changed |
| 357 |
self.results['id'] = results.get('id') |
| 358 |
|
| 359 |
if self.check_mode: |
| 360 |
return self.results |
| 361 |
if changed: |
| 362 |
if self.state == 'present': |
| 363 |
if not self.sku: |
| 364 |
self.fail('Parameter error: sku is required when creating a vpn gateway') |
| 365 |
if not self.ip_configurations: |
| 366 |
self.fail('Parameter error: ip_configurations required when creating a vpn gateway') |
| 367 |
subnet = self.network_models.SubResource( |
| 368 |
id='/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/virtualNetworks/{2}/subnets/GatewaySubnet'.format( |
| 369 |
self.virtual_network['subscription_id'], |
| 370 |
self.virtual_network['resource_group'], |
| 371 |
self.virtual_network['name'])) |
| 372 |
|
| 373 |
public_ip_address = self.network_models.SubResource( |
| 374 |
id='/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/publicIPAddresses/{2}'.format( |
| 375 |
self.virtual_network['subscription_id'], |
| 376 |
self.virtual_network['resource_group'], |
| 377 |
self.ip_configurations[0]['public_ip_address_name'])) |
| 378 |
|
| 379 |
vgw_ip_configurations = [ |
| 380 |
self.network_models.VirtualNetworkGatewayIPConfiguration( |
| 381 |
private_ip_allocation_method=ip_config.get('private_ip_allocation_method'), |
| 382 |
subnet=subnet, |
| 383 |
public_ip_address=public_ip_address, |
| 384 |
name='default' |
| 385 |
) for ip_config in self.ip_configurations |
| 386 |
] |
| 387 |
|
| 388 |
vgw_sku = self.network_models.VirtualNetworkGatewaySku( |
| 389 |
name=self.sku, |
| 390 |
tier=self.sku |
| 391 |
) |
| 392 |
|
| 393 |
vgw_bgp_settings = self.network_models.BgpSettings( |
| 394 |
asn=self.bgp_settings.get('asn'), |
| 395 |
) if self.bgp_settings else None |
| 396 |
vgw = self.network_models.VirtualNetworkGateway( |
| 397 |
location=self.location, |
| 398 |
ip_configurations=vgw_ip_configurations, |
| 399 |
gateway_type=_snake_to_camel(self.gateway_type, True), |
| 400 |
vpn_type=_snake_to_camel(self.vpn_type, True), |
| 401 |
vpn_gateway_generation=_snake_to_camel(self.vpn_gateway_generation, True), |
| 402 |
enable_bgp=self.enable_bgp, |
| 403 |
sku=vgw_sku, |
| 404 |
bgp_settings=vgw_bgp_settings |
| 405 |
) |
| 406 |
if self.tags: |
| 407 |
vgw.tags = self.tags |
| 408 |
results = self.create_or_update_vgw(vgw) |
| 409 |
|
| 410 |
else: |
| 411 |
results = self.delete_vgw() |
| 412 |
|
| 413 |
if self.state == 'present': |
| 414 |
self.results['id'] = results.get('id') |
| 415 |
return self.results |
| 416 |
|
| 417 |
def create_or_update_vgw(self, vgw): |
| 418 |
try: |
| 419 |
poller = self.network_client.virtual_network_gateways.begin_create_or_update(self.resource_group, self.name, vgw) |
| 420 |
new_vgw = self.get_poller_result(poller) |
| 421 |
return vgw_to_dict(new_vgw) |
| 422 |
except Exception as exc: |
| 423 |
self.fail("Error creating or updating virtual network gateway {0} - {1}".format(self.name, str(exc))) |
| 424 |
|
| 425 |
def delete_vgw(self): |
| 426 |
try: |
| 427 |
poller = self.network_client.virtual_network_gateways.begin_delete(self.resource_group, self.name) |
| 428 |
self.get_poller_result(poller) |
| 429 |
except Exception as exc: |
| 430 |
self.fail("Error deleting virtual network gateway {0} - {1}".format(self.name, str(exc))) |
| 431 |
return True |
| 432 |
|
| 433 |
|
| 434 |
def main(): |
| 435 |
AzureRMVirtualNetworkGateway() |
| 436 |
|
| 437 |
|
| 438 |
if __name__ == '__main__': |
| 439 |
main() |