| 1 |
# (c) 2015, Brian Coca <[email protected]> |
| 2 |
# (c) 2012-17 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
DOCUMENTATION = """ |
| 7 |
name: url |
| 8 |
author: Brian Coca (@bcoca) |
| 9 |
version_added: "1.9" |
| 10 |
short_description: return contents from URL |
| 11 |
description: |
| 12 |
- Returns the content of the URL requested to be used as data in play. |
| 13 |
positional: _terms |
| 14 |
options: |
| 15 |
_terms: |
| 16 |
description: urls to query |
| 17 |
validate_certs: |
| 18 |
description: Flag to control SSL certificate validation |
| 19 |
type: boolean |
| 20 |
default: True |
| 21 |
split_lines: |
| 22 |
description: Flag to control if content is returned as a list of lines or as a single text blob |
| 23 |
type: boolean |
| 24 |
default: True |
| 25 |
use_proxy: |
| 26 |
description: Flag to control if the lookup will observe HTTP proxy environment variables when present. |
| 27 |
type: boolean |
| 28 |
default: True |
| 29 |
username: |
| 30 |
description: Username to use for HTTP authentication. |
| 31 |
type: string |
| 32 |
version_added: "2.8" |
| 33 |
password: |
| 34 |
description: Password to use for HTTP authentication. |
| 35 |
type: string |
| 36 |
secret: true |
| 37 |
version_added: "2.8" |
| 38 |
headers: |
| 39 |
description: HTTP request headers |
| 40 |
type: dictionary |
| 41 |
default: {} |
| 42 |
version_added: "2.9" |
| 43 |
force: |
| 44 |
description: Whether or not to set "cache-control" header with value "no-cache" |
| 45 |
type: boolean |
| 46 |
version_added: "2.10" |
| 47 |
default: False |
| 48 |
vars: |
| 49 |
- name: ansible_lookup_url_force |
| 50 |
env: |
| 51 |
- name: ANSIBLE_LOOKUP_URL_FORCE |
| 52 |
ini: |
| 53 |
- section: url_lookup |
| 54 |
key: force |
| 55 |
timeout: |
| 56 |
description: How long to wait for the server to send data before giving up |
| 57 |
type: float |
| 58 |
version_added: "2.10" |
| 59 |
default: 10 |
| 60 |
vars: |
| 61 |
- name: ansible_lookup_url_timeout |
| 62 |
env: |
| 63 |
- name: ANSIBLE_LOOKUP_URL_TIMEOUT |
| 64 |
ini: |
| 65 |
- section: url_lookup |
| 66 |
key: timeout |
| 67 |
http_agent: |
| 68 |
description: User-Agent to use in the request. The default was changed in 2.11 to V(ansible-httpget). |
| 69 |
type: string |
| 70 |
version_added: "2.10" |
| 71 |
default: ansible-httpget |
| 72 |
vars: |
| 73 |
- name: ansible_lookup_url_agent |
| 74 |
env: |
| 75 |
- name: ANSIBLE_LOOKUP_URL_AGENT |
| 76 |
ini: |
| 77 |
- section: url_lookup |
| 78 |
key: agent |
| 79 |
force_basic_auth: |
| 80 |
description: Force basic authentication |
| 81 |
type: boolean |
| 82 |
version_added: "2.10" |
| 83 |
default: False |
| 84 |
vars: |
| 85 |
- name: ansible_lookup_url_force_basic_auth |
| 86 |
env: |
| 87 |
- name: ANSIBLE_LOOKUP_URL_FORCE_BASIC_AUTH |
| 88 |
ini: |
| 89 |
- section: url_lookup |
| 90 |
key: force_basic_auth |
| 91 |
follow_redirects: |
| 92 |
type: string |
| 93 |
version_added: "2.10" |
| 94 |
default: 'urllib2' |
| 95 |
vars: |
| 96 |
- name: ansible_lookup_url_follow_redirects |
| 97 |
env: |
| 98 |
- name: ANSIBLE_LOOKUP_URL_FOLLOW_REDIRECTS |
| 99 |
ini: |
| 100 |
- section: url_lookup |
| 101 |
key: follow_redirects |
| 102 |
use_gssapi: |
| 103 |
description: |
| 104 |
- Use GSSAPI handler of requests |
| 105 |
- As of Ansible 2.11, GSSAPI credentials can be specified with O(username) and O(password). |
| 106 |
type: boolean |
| 107 |
version_added: "2.10" |
| 108 |
default: False |
| 109 |
vars: |
| 110 |
- name: ansible_lookup_url_use_gssapi |
| 111 |
env: |
| 112 |
- name: ANSIBLE_LOOKUP_URL_USE_GSSAPI |
| 113 |
ini: |
| 114 |
- section: url_lookup |
| 115 |
key: use_gssapi |
| 116 |
use_netrc: |
| 117 |
description: |
| 118 |
- Determining whether to use credentials from ``~/.netrc`` file |
| 119 |
- By default .netrc is used with Basic authentication headers |
| 120 |
- When set to False, .netrc credentials are ignored |
| 121 |
type: boolean |
| 122 |
version_added: "2.14" |
| 123 |
default: True |
| 124 |
vars: |
| 125 |
- name: ansible_lookup_url_use_netrc |
| 126 |
env: |
| 127 |
- name: ANSIBLE_LOOKUP_URL_USE_NETRC |
| 128 |
ini: |
| 129 |
- section: url_lookup |
| 130 |
key: use_netrc |
| 131 |
unix_socket: |
| 132 |
description: String of file system path to unix socket file to use when establishing connection to the provided url |
| 133 |
type: string |
| 134 |
version_added: "2.10" |
| 135 |
vars: |
| 136 |
- name: ansible_lookup_url_unix_socket |
| 137 |
env: |
| 138 |
- name: ANSIBLE_LOOKUP_URL_UNIX_SOCKET |
| 139 |
ini: |
| 140 |
- section: url_lookup |
| 141 |
key: unix_socket |
| 142 |
ca_path: |
| 143 |
description: String of file system path to CA cert bundle to use |
| 144 |
type: string |
| 145 |
version_added: "2.10" |
| 146 |
vars: |
| 147 |
- name: ansible_lookup_url_ca_path |
| 148 |
env: |
| 149 |
- name: ANSIBLE_LOOKUP_URL_CA_PATH |
| 150 |
ini: |
| 151 |
- section: url_lookup |
| 152 |
key: ca_path |
| 153 |
unredirected_headers: |
| 154 |
description: A list of headers to not attach on a redirected request |
| 155 |
type: list |
| 156 |
elements: string |
| 157 |
version_added: "2.10" |
| 158 |
vars: |
| 159 |
- name: ansible_lookup_url_unredir_headers |
| 160 |
env: |
| 161 |
- name: ANSIBLE_LOOKUP_URL_UNREDIR_HEADERS |
| 162 |
ini: |
| 163 |
- section: url_lookup |
| 164 |
key: unredirected_headers |
| 165 |
ciphers: |
| 166 |
description: |
| 167 |
- SSL/TLS Ciphers to use for the request |
| 168 |
- 'When a list is provided, all ciphers are joined in order with C(:)' |
| 169 |
- See the L(OpenSSL Cipher List Format,https://docs.openssl.org/master/man1/openssl-ciphers/#cipher-list-format) |
| 170 |
for more details. |
| 171 |
- The available ciphers is dependent on the Python and OpenSSL/LibreSSL versions |
| 172 |
type: list |
| 173 |
elements: string |
| 174 |
version_added: '2.14' |
| 175 |
vars: |
| 176 |
- name: ansible_lookup_url_ciphers |
| 177 |
env: |
| 178 |
- name: ANSIBLE_LOOKUP_URL_CIPHERS |
| 179 |
ini: |
| 180 |
- section: url_lookup |
| 181 |
key: ciphers |
| 182 |
extends_documentation_fragment: |
| 183 |
- url.url_redirect |
| 184 |
""" |
| 185 |
|
| 186 |
EXAMPLES = """ |
| 187 |
- name: url lookup splits lines by default |
| 188 |
ansible.builtin.debug: msg="{{item}}" |
| 189 |
loop: "{{ lookup('ansible.builtin.url', 'https://github.com/gremlin.keys', wantlist=True) }}" |
| 190 |
|
| 191 |
- name: display ip ranges |
| 192 |
ansible.builtin.debug: msg="{{ lookup('ansible.builtin.url', 'https://ip-ranges.amazonaws.com/ip-ranges.json', split_lines=False) }}" |
| 193 |
|
| 194 |
- name: url lookup using authentication |
| 195 |
ansible.builtin.debug: msg="{{ lookup('ansible.builtin.url', 'https://some.private.site.com/file.txt', username='bob', password='hunter2') }}" |
| 196 |
|
| 197 |
- name: url lookup using basic authentication |
| 198 |
ansible.builtin.debug: |
| 199 |
msg: "{{ lookup('ansible.builtin.url', 'https://some.private.site.com/file.txt', username='bob', password='hunter2', force_basic_auth='True') }}" |
| 200 |
|
| 201 |
- name: url lookup using headers |
| 202 |
ansible.builtin.debug: |
| 203 |
msg: "{{ lookup('ansible.builtin.url', 'https://some.private.site.com/api/service', headers={'header1':'value1', 'header2':'value2'} ) }}" |
| 204 |
""" |
| 205 |
|
| 206 |
RETURN = """ |
| 207 |
_list: |
| 208 |
description: list of list of lines or content of url(s) |
| 209 |
type: list |
| 210 |
elements: str |
| 211 |
""" |
| 212 |
|
| 213 |
from urllib.error import HTTPError, URLError |
| 214 |
|
| 215 |
from ansible.errors import AnsibleError |
| 216 |
from ansible.module_utils.common.text.converters import to_text |
| 217 |
from ansible.module_utils.urls import open_url, mask_url, ConnectionError, SSLValidationError |
| 218 |
from ansible.plugins.lookup import LookupBase |
| 219 |
from ansible.utils.display import Display |
| 220 |
|
| 221 |
display = Display() |
| 222 |
|
| 223 |
|
| 224 |
class LookupModule(LookupBase): |
| 225 |
|
| 226 |
def run(self, terms, variables=None, **kwargs): |
| 227 |
|
| 228 |
self.set_options(var_options=variables, direct=kwargs) |
| 229 |
|
| 230 |
ret = [] |
| 231 |
for term in terms: |
| 232 |
display.vvvv(f"url lookup connecting to {mask_url(term)}") |
| 233 |
try: |
| 234 |
response = open_url( |
| 235 |
term, validate_certs=self.get_option('validate_certs'), |
| 236 |
use_proxy=self.get_option('use_proxy'), |
| 237 |
url_username=self.get_option('username'), |
| 238 |
url_password=self.get_option('password'), |
| 239 |
headers=self.get_option('headers'), |
| 240 |
force=self.get_option('force'), |
| 241 |
timeout=self.get_option('timeout'), |
| 242 |
http_agent=self.get_option('http_agent'), |
| 243 |
force_basic_auth=self.get_option('force_basic_auth'), |
| 244 |
follow_redirects=self.get_option('follow_redirects'), |
| 245 |
use_gssapi=self.get_option('use_gssapi'), |
| 246 |
unix_socket=self.get_option('unix_socket'), |
| 247 |
ca_path=self.get_option('ca_path'), |
| 248 |
unredirected_headers=self.get_option('unredirected_headers'), |
| 249 |
ciphers=self.get_option('ciphers'), |
| 250 |
use_netrc=self.get_option('use_netrc') |
| 251 |
) |
| 252 |
except HTTPError as e: |
| 253 |
raise AnsibleError(f"Received HTTP error for {mask_url(term)}") from e |
| 254 |
except URLError as e: |
| 255 |
raise AnsibleError(f"Failed lookup url for {mask_url(term)}") from e |
| 256 |
except SSLValidationError as e: |
| 257 |
raise AnsibleError(f"Error validating the server's certificate for {mask_url(term)}") from e |
| 258 |
except ConnectionError as e: |
| 259 |
raise AnsibleError(f"Error connecting to {mask_url(term)}") from e |
| 260 |
|
| 261 |
if self.get_option('split_lines'): |
| 262 |
for line in response.read().splitlines(): |
| 263 |
ret.append(to_text(line)) |
| 264 |
else: |
| 265 |
ret.append(to_text(response.read())) |
| 266 |
return ret |