2015-01-27 23:29:16 +00:00
|
|
|
# (c) 2015, Brian Coca <bcoca@ansible.com>
|
2017-09-19 14:49:07 +00:00
|
|
|
# (c) 2012-17 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2015-04-13 20:28:01 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2015-01-27 23:29:16 +00:00
|
|
|
|
2017-09-19 14:49:07 +00:00
|
|
|
DOCUMENTATION = """
|
2017-11-13 18:43:19 +00:00
|
|
|
lookup: url
|
|
|
|
author: Brian Coca (@bcoca)
|
|
|
|
version_added: "1.9"
|
|
|
|
short_description: return contents from URL
|
|
|
|
description:
|
|
|
|
- Returns the content of the URL requested to be used as data in play.
|
|
|
|
options:
|
|
|
|
_terms:
|
|
|
|
description: urls to query
|
|
|
|
validate_certs:
|
|
|
|
description: Flag to control SSL certificate validation
|
|
|
|
type: boolean
|
|
|
|
default: True
|
|
|
|
split_lines:
|
|
|
|
description: Flag to control if content is returned as a list of lines or as a single text blob
|
|
|
|
type: boolean
|
|
|
|
default: True
|
|
|
|
use_proxy:
|
|
|
|
description: Flag to control if the lookup will observe HTTP proxy environment variables when present.
|
|
|
|
type: boolean
|
|
|
|
default: True
|
2017-09-19 14:49:07 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: url lookup splits lines by default
|
|
|
|
debug: msg="{{item}}"
|
2017-11-13 18:43:19 +00:00
|
|
|
loop: "{{ lookup('url', 'https://github.com/gremlin.keys', wantlist=True) }}"
|
|
|
|
|
|
|
|
- name: display ip ranges
|
2017-11-22 14:00:17 +00:00
|
|
|
debug: msg="{{ lookup('url', 'https://ip-ranges.amazonaws.com/ip-ranges.json', split_lines=False) }}"
|
2017-09-19 14:49:07 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
_list:
|
|
|
|
description: list of list of lines or content of url(s)
|
|
|
|
"""
|
2015-01-27 23:29:16 +00:00
|
|
|
|
2015-06-12 19:32:02 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2017-03-23 20:35:05 +00:00
|
|
|
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError
|
2016-09-07 05:54:17 +00:00
|
|
|
from ansible.module_utils._text import to_text
|
2015-06-12 19:32:02 +00:00
|
|
|
from ansible.module_utils.urls import open_url, ConnectionError, SSLValidationError
|
2016-09-07 05:54:17 +00:00
|
|
|
from ansible.plugins.lookup import LookupBase
|
2015-06-12 19:32:02 +00:00
|
|
|
|
2015-11-11 16:33:44 +00:00
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
2015-01-27 23:29:16 +00:00
|
|
|
|
2015-11-11 16:33:44 +00:00
|
|
|
class LookupModule(LookupBase):
|
2015-01-27 23:29:16 +00:00
|
|
|
|
2015-06-12 19:32:02 +00:00
|
|
|
def run(self, terms, variables=None, **kwargs):
|
2015-01-27 23:29:16 +00:00
|
|
|
|
2015-06-12 19:32:02 +00:00
|
|
|
validate_certs = kwargs.get('validate_certs', True)
|
2017-05-11 10:26:32 +00:00
|
|
|
split_lines = kwargs.get('split_lines', True)
|
2017-04-20 14:43:25 +00:00
|
|
|
use_proxy = kwargs.get('use_proxy', True)
|
2015-06-12 19:32:02 +00:00
|
|
|
|
2015-01-27 23:29:16 +00:00
|
|
|
ret = []
|
|
|
|
for term in terms:
|
2015-11-11 16:33:44 +00:00
|
|
|
display.vvvv("url lookup connecting to %s" % term)
|
2015-01-27 23:29:16 +00:00
|
|
|
try:
|
2017-04-20 14:43:25 +00:00
|
|
|
response = open_url(term, validate_certs=validate_certs, use_proxy=use_proxy)
|
2016-08-31 14:03:20 +00:00
|
|
|
except HTTPError as e:
|
2015-06-12 19:32:02 +00:00
|
|
|
raise AnsibleError("Received HTTP error for %s : %s" % (term, str(e)))
|
2016-08-31 14:03:20 +00:00
|
|
|
except URLError as e:
|
2015-09-27 18:01:19 +00:00
|
|
|
raise AnsibleError("Failed lookup url for %s : %s" % (term, str(e)))
|
2015-06-12 19:32:02 +00:00
|
|
|
except SSLValidationError as e:
|
|
|
|
raise AnsibleError("Error validating the server's certificate for %s: %s" % (term, str(e)))
|
|
|
|
except ConnectionError as e:
|
|
|
|
raise AnsibleError("Error connecting to %s: %s" % (term, str(e)))
|
2015-01-27 23:29:16 +00:00
|
|
|
|
2017-05-11 10:26:32 +00:00
|
|
|
if split_lines:
|
|
|
|
for line in response.read().splitlines():
|
|
|
|
ret.append(to_text(line))
|
|
|
|
else:
|
|
|
|
ret.append(to_text(response.read()))
|
2015-01-27 23:29:16 +00:00
|
|
|
return ret
|