| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the Ansible project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: ec2_customer_gateway |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage an AWS customer gateway |
| 12 |
description: |
| 13 |
- Manage an AWS customer gateway. |
| 14 |
author: |
| 15 |
- Michael Baydoun (@MichaelBaydoun) |
| 16 |
notes: |
| 17 |
- You cannot create more than one customer gateway with the same IP address. If you run an identical request more than one time, the |
| 18 |
first request creates the customer gateway, and subsequent requests return information about the existing customer gateway. The subsequent |
| 19 |
requests do not create new customer gateway resources. |
| 20 |
- Return values contain customer_gateway and customer_gateways keys which are identical dicts. You should use |
| 21 |
customer_gateway. See U(https://github.com/ansible/ansible-modules-extras/issues/2773) for details. |
| 22 |
options: |
| 23 |
bgp_asn: |
| 24 |
description: |
| 25 |
- Border Gateway Protocol (BGP) Autonomous System Number (ASN). |
| 26 |
- Defaults to C(65000) if not specified when I(state=present). |
| 27 |
type: int |
| 28 |
ip_address: |
| 29 |
description: |
| 30 |
- Internet-routable IP address for customers gateway, must be a static address. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
name: |
| 34 |
description: |
| 35 |
- Name of the customer gateway. |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
routing: |
| 39 |
description: |
| 40 |
- The type of routing. |
| 41 |
choices: ['static', 'dynamic'] |
| 42 |
default: dynamic |
| 43 |
type: str |
| 44 |
state: |
| 45 |
description: |
| 46 |
- Create or terminate the Customer Gateway. |
| 47 |
default: present |
| 48 |
choices: [ 'present', 'absent' ] |
| 49 |
type: str |
| 50 |
extends_documentation_fragment: |
| 51 |
- amazon.aws.common.modules |
| 52 |
- amazon.aws.region.modules |
| 53 |
- amazon.aws.boto3 |
| 54 |
""" |
| 55 |
|
| 56 |
EXAMPLES = r""" |
| 57 |
- name: Create Customer Gateway |
| 58 |
community.aws.ec2_customer_gateway: |
| 59 |
bgp_asn: 12345 |
| 60 |
ip_address: 1.2.3.4 |
| 61 |
name: IndianapolisOffice |
| 62 |
region: us-east-1 |
| 63 |
register: cgw |
| 64 |
|
| 65 |
- name: Delete Customer Gateway |
| 66 |
community.aws.ec2_customer_gateway: |
| 67 |
ip_address: 1.2.3.4 |
| 68 |
name: IndianapolisOffice |
| 69 |
state: absent |
| 70 |
region: us-east-1 |
| 71 |
register: cgw |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
gateway: |
| 76 |
description: details about the customer gateway that was created. |
| 77 |
returned: success |
| 78 |
type: complex |
| 79 |
contains: |
| 80 |
customer_gateways: |
| 81 |
description: details about the gateway that was created. |
| 82 |
returned: success |
| 83 |
type: complex |
| 84 |
contains: |
| 85 |
bgp_asn: |
| 86 |
description: The Border Gateway Autonomous System Number. |
| 87 |
returned: when exists and gateway is available. |
| 88 |
sample: 65123 |
| 89 |
type: str |
| 90 |
customer_gateway_id: |
| 91 |
description: gateway id assigned by amazon. |
| 92 |
returned: when exists and gateway is available. |
| 93 |
sample: cgw-cb6386a2 |
| 94 |
type: str |
| 95 |
ip_address: |
| 96 |
description: ip address of your gateway device. |
| 97 |
returned: when exists and gateway is available. |
| 98 |
sample: 1.2.3.4 |
| 99 |
type: str |
| 100 |
state: |
| 101 |
description: state of gateway. |
| 102 |
returned: when gateway exists and is available. |
| 103 |
sample: available |
| 104 |
type: str |
| 105 |
tags: |
| 106 |
description: Any tags on the gateway. |
| 107 |
returned: when gateway exists and is available, and when tags exist. |
| 108 |
type: list |
| 109 |
type: |
| 110 |
description: encryption type. |
| 111 |
returned: when gateway exists and is available. |
| 112 |
sample: ipsec.1 |
| 113 |
type: str |
| 114 |
""" |
| 115 |
|
| 116 |
try: |
| 117 |
import botocore |
| 118 |
except ImportError: |
| 119 |
pass # Handled by AnsibleAWSModule |
| 120 |
|
| 121 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 122 |
|
| 123 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 124 |
|
| 125 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 126 |
|
| 127 |
|
| 128 |
class Ec2CustomerGatewayManager: |
| 129 |
def __init__(self, module): |
| 130 |
self.module = module |
| 131 |
|
| 132 |
try: |
| 133 |
self.ec2 = module.client("ec2") |
| 134 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 135 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 136 |
|
| 137 |
@AWSRetry.jittered_backoff(delay=2, max_delay=30, retries=6, catch_extra_error_codes=["IncorrectState"]) |
| 138 |
def ensure_cgw_absent(self, gw_id): |
| 139 |
response = self.ec2.delete_customer_gateway(DryRun=False, CustomerGatewayId=gw_id) |
| 140 |
return response |
| 141 |
|
| 142 |
def ensure_cgw_present(self, bgp_asn, ip_address): |
| 143 |
if not bgp_asn: |
| 144 |
bgp_asn = 65000 |
| 145 |
response = self.ec2.create_customer_gateway( |
| 146 |
DryRun=False, |
| 147 |
Type="ipsec.1", |
| 148 |
PublicIp=ip_address, |
| 149 |
BgpAsn=bgp_asn, |
| 150 |
) |
| 151 |
return response |
| 152 |
|
| 153 |
def tag_cgw_name(self, gw_id, name): |
| 154 |
response = self.ec2.create_tags( |
| 155 |
DryRun=False, |
| 156 |
Resources=[ |
| 157 |
gw_id, |
| 158 |
], |
| 159 |
Tags=[ |
| 160 |
{"Key": "Name", "Value": name}, |
| 161 |
], |
| 162 |
) |
| 163 |
return response |
| 164 |
|
| 165 |
def describe_gateways(self, ip_address): |
| 166 |
response = self.ec2.describe_customer_gateways( |
| 167 |
DryRun=False, |
| 168 |
Filters=[ |
| 169 |
{ |
| 170 |
"Name": "state", |
| 171 |
"Values": [ |
| 172 |
"available", |
| 173 |
], |
| 174 |
}, |
| 175 |
{ |
| 176 |
"Name": "ip-address", |
| 177 |
"Values": [ |
| 178 |
ip_address, |
| 179 |
], |
| 180 |
}, |
| 181 |
], |
| 182 |
) |
| 183 |
return response |
| 184 |
|
| 185 |
|
| 186 |
def main(): |
| 187 |
argument_spec = dict( |
| 188 |
bgp_asn=dict(required=False, type="int"), |
| 189 |
ip_address=dict(required=True), |
| 190 |
name=dict(required=True), |
| 191 |
routing=dict(default="dynamic", choices=["dynamic", "static"]), |
| 192 |
state=dict(default="present", choices=["present", "absent"]), |
| 193 |
) |
| 194 |
|
| 195 |
module = AnsibleAWSModule( |
| 196 |
argument_spec=argument_spec, |
| 197 |
supports_check_mode=True, |
| 198 |
required_if=[ |
| 199 |
("routing", "dynamic", ["bgp_asn"]), |
| 200 |
], |
| 201 |
) |
| 202 |
|
| 203 |
gw_mgr = Ec2CustomerGatewayManager(module) |
| 204 |
|
| 205 |
name = module.params.get("name") |
| 206 |
|
| 207 |
existing = gw_mgr.describe_gateways(module.params["ip_address"]) |
| 208 |
|
| 209 |
results = dict(changed=False) |
| 210 |
if module.params["state"] == "present": |
| 211 |
if existing["CustomerGateways"]: |
| 212 |
existing["CustomerGateway"] = existing["CustomerGateways"][0] |
| 213 |
results["gateway"] = existing |
| 214 |
if existing["CustomerGateway"]["Tags"]: |
| 215 |
tag_array = existing["CustomerGateway"]["Tags"] |
| 216 |
for key, value in enumerate(tag_array): |
| 217 |
if value["Key"] == "Name": |
| 218 |
current_name = value["Value"] |
| 219 |
if current_name != name: |
| 220 |
results["name"] = gw_mgr.tag_cgw_name( |
| 221 |
results["gateway"]["CustomerGateway"]["CustomerGatewayId"], |
| 222 |
module.params["name"], |
| 223 |
) |
| 224 |
results["changed"] = True |
| 225 |
else: |
| 226 |
if not module.check_mode: |
| 227 |
results["gateway"] = gw_mgr.ensure_cgw_present( |
| 228 |
module.params["bgp_asn"], |
| 229 |
module.params["ip_address"], |
| 230 |
) |
| 231 |
results["name"] = gw_mgr.tag_cgw_name( |
| 232 |
results["gateway"]["CustomerGateway"]["CustomerGatewayId"], |
| 233 |
module.params["name"], |
| 234 |
) |
| 235 |
results["changed"] = True |
| 236 |
|
| 237 |
elif module.params["state"] == "absent": |
| 238 |
if existing["CustomerGateways"]: |
| 239 |
existing["CustomerGateway"] = existing["CustomerGateways"][0] |
| 240 |
results["gateway"] = existing |
| 241 |
if not module.check_mode: |
| 242 |
results["gateway"] = gw_mgr.ensure_cgw_absent(existing["CustomerGateway"]["CustomerGatewayId"]) |
| 243 |
results["changed"] = True |
| 244 |
|
| 245 |
pretty_results = camel_dict_to_snake_dict(results) |
| 246 |
module.exit_json(**pretty_results) |
| 247 |
|
| 248 |
|
| 249 |
if __name__ == "__main__": |
| 250 |
main() |