diff --git a/plugins/lookup/aws_service_ip_ranges.py b/plugins/lookup/aws_service_ip_ranges.py index c390fe390b6..a96a6dc0012 100644 --- a/plugins/lookup/aws_service_ip_ranges.py +++ b/plugins/lookup/aws_service_ip_ranges.py @@ -1,11 +1,10 @@ # (c) 2016 James Turner # (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import absolute_import, division, print_function - +from __future__ import (absolute_import, division, print_function) __metaclass__ = type -DOCUMENTATION = """ +DOCUMENTATION = ''' lookup: aws_service_ip_ranges author: - James Turner @@ -22,7 +21,7 @@ description: 'The AWS region to narrow the ranges to. Examples: us-east-1, eu-west-2, ap-southeast-1' ipv6_prefix: description: 'Return only ipv6 addresses. Option: ipv6_prefix=True' -""" +''' EXAMPLES = """ vars: @@ -47,9 +46,12 @@ import json from ansible.errors import AnsibleError +from ansible.module_utils.six.moves.urllib.error import HTTPError +from ansible.module_utils.six.moves.urllib.error import URLError from ansible.module_utils._text import to_native -from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError -from ansible.module_utils.urls import ConnectionError, SSLValidationError, open_url +from ansible.module_utils.urls import ConnectionError +from ansible.module_utils.urls import open_url +from ansible.module_utils.urls import SSLValidationError from ansible.plugins.lookup import LookupBase @@ -61,36 +63,28 @@ def run(self, terms, variables, **kwargs): else: prefixes_label = "ipv6_prefixes" ip_prefix_label = "ipv6_prefix" + try: - resp = open_url("https://ip-ranges.amazonaws.com/ip-ranges.json") + resp = open_url('https://ip-ranges.amazonaws.com/ip-ranges.json') amazon_response = json.load(resp)[prefixes_label] - except getattr(json.decoder, "JSONDecodeError", ValueError) as e: + except getattr(json.decoder, 'JSONDecodeError', ValueError) as e: # on Python 3+, json.decoder.JSONDecodeError is raised for bad # JSON. On 2.x it's a ValueError raise AnsibleError("Could not decode AWS IP ranges: %s" % to_native(e)) except HTTPError as e: - raise AnsibleError( - "Received HTTP error while pulling IP ranges: %s" % to_native(e) - ) + raise AnsibleError("Received HTTP error while pulling IP ranges: %s" % to_native(e)) except SSLValidationError as e: - raise AnsibleError( - "Error validating the server's certificate for: %s" % to_native(e) - ) + raise AnsibleError("Error validating the server's certificate for: %s" % to_native(e)) except URLError as e: raise AnsibleError("Failed look up IP range service: %s" % to_native(e)) except ConnectionError as e: - raise AnsibleError( - "Error connecting to IP range service: %s" % to_native(e) - ) + raise AnsibleError("Error connecting to IP range service: %s" % to_native(e)) + + if 'region' in kwargs: + region = kwargs['region'] + amazon_response = (item for item in amazon_response if item['region'] == region) + if 'service' in kwargs: + service = str.upper(kwargs['service']) + amazon_response = (item for item in amazon_response if item['service'] == service) - if "region" in kwargs: - region = kwargs["region"] - amazon_response = ( - item for item in amazon_response if item["region"] == region - ) - if "service" in kwargs: - service = str.upper(kwargs["service"]) - amazon_response = ( - item for item in amazon_response if item["service"] == service - ) return [item[ip_prefix_label] for item in amazon_response]