| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# (c) 2016 James Turner <[email protected]> |
| 4 |
# (c) 2017 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 |
name: aws_service_ip_ranges |
| 9 |
author: |
| 10 |
- James Turner (!UNKNOWN) <[email protected]> |
| 11 |
requirements: |
| 12 |
- must have public internet connectivity |
| 13 |
short_description: Look up the IP ranges for services provided in AWS such as EC2 and S3 |
| 14 |
description: |
| 15 |
- AWS publishes IP ranges used on the public internet by EC2, S3, CloudFront, CodeBuild, Route53, and Route53 Health Checking. |
| 16 |
- This module produces a list of all the ranges (by default) or can narrow down the list to the specified region or service. |
| 17 |
options: |
| 18 |
service: |
| 19 |
description: |
| 20 |
- The service to filter ranges by. |
| 21 |
- Options include V(EC2), V(S3), V(CLOUDFRONT), V(CODEBUILD), V(ROUTE53), V(ROUTE53_HEALTHCHECKS). |
| 22 |
region: |
| 23 |
description: |
| 24 |
- The AWS region to narrow the ranges to. Examples include V(us-east-1), V(eu-west-2), V(ap-southeast-1). |
| 25 |
ipv6_prefixes: |
| 26 |
description: |
| 27 |
- When O(ipv6_prefixes=true) the lookup will return ipv6 addresses instead of ipv4 addresses. |
| 28 |
version_added: 2.1.0 |
| 29 |
""" |
| 30 |
|
| 31 |
EXAMPLES = r""" |
| 32 |
vars: |
| 33 |
ec2_ranges: "{{ lookup('aws_service_ip_ranges', region='ap-southeast-2', service='EC2', wantlist=True) }}" |
| 34 |
tasks: |
| 35 |
- name: "use list return option and iterate as a loop" |
| 36 |
ansible.builtin.debug: msg="{% for cidr in ec2_ranges %}{{ cidr }} {% endfor %}" |
| 37 |
# "52.62.0.0/15 52.64.0.0/17 52.64.128.0/17 52.65.0.0/16 52.95.241.0/24 52.95.255.16/28 54.66.0.0/16 " |
| 38 |
|
| 39 |
- name: "Pull S3 IP ranges, and print the default return style" |
| 40 |
ansible.builtin.debug: msg="{{ lookup('aws_service_ip_ranges', region='us-east-1', service='S3') }}" |
| 41 |
# "52.92.16.0/20,52.216.0.0/15,54.231.0.0/17" |
| 42 |
""" |
| 43 |
|
| 44 |
RETURN = r""" |
| 45 |
_raw: |
| 46 |
description: Comma-separated list of CIDR ranges. |
| 47 |
""" |
| 48 |
|
| 49 |
import json |
| 50 |
|
| 51 |
import ansible.module_utils.six.moves.urllib.error |
| 52 |
import ansible.module_utils.urls |
| 53 |
from ansible.errors import AnsibleLookupError |
| 54 |
from ansible.module_utils.common.text.converters import to_native |
| 55 |
from ansible.plugins.lookup import LookupBase |
| 56 |
|
| 57 |
|
| 58 |
class LookupModule(LookupBase): |
| 59 |
def run(self, terms, variables, **kwargs): |
| 60 |
if "ipv6_prefixes" in kwargs and kwargs["ipv6_prefixes"]: |
| 61 |
prefixes_label = "ipv6_prefixes" |
| 62 |
ip_prefix_label = "ipv6_prefix" |
| 63 |
else: |
| 64 |
prefixes_label = "prefixes" |
| 65 |
ip_prefix_label = "ip_prefix" |
| 66 |
|
| 67 |
try: |
| 68 |
resp = ansible.module_utils.urls.open_url("https://ip-ranges.amazonaws.com/ip-ranges.json") |
| 69 |
amazon_response = json.load(resp)[prefixes_label] |
| 70 |
except getattr(json.decoder, "JSONDecodeError", ValueError) as e: |
| 71 |
# on Python 3+, json.decoder.JSONDecodeError is raised for bad |
| 72 |
# JSON. On 2.x it's a ValueError |
| 73 |
raise AnsibleLookupError(f"Could not decode AWS IP ranges: {to_native(e)}") |
| 74 |
except ansible.module_utils.six.moves.urllib.error.HTTPError as e: |
| 75 |
raise AnsibleLookupError(f"Received HTTP error while pulling IP ranges: {to_native(e)}") |
| 76 |
except ansible.module_utils.urls.SSLValidationError as e: |
| 77 |
raise AnsibleLookupError(f"Error validating the server's certificate for: {to_native(e)}") |
| 78 |
except ansible.module_utils.six.moves.urllib.error.URLError as e: |
| 79 |
raise AnsibleLookupError(f"Failed look up IP range service: {to_native(e)}") |
| 80 |
except ansible.module_utils.urls.ConnectionError as e: |
| 81 |
raise AnsibleLookupError(f"Error connecting to IP range service: {to_native(e)}") |
| 82 |
|
| 83 |
if "region" in kwargs: |
| 84 |
region = kwargs["region"] |
| 85 |
amazon_response = (item for item in amazon_response if item["region"] == region) |
| 86 |
if "service" in kwargs: |
| 87 |
service = str.upper(kwargs["service"]) |
| 88 |
amazon_response = (item for item in amazon_response if item["service"] == service) |
| 89 |
iprange = [item[ip_prefix_label] for item in amazon_response] |
| 90 |
return iprange |