|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Copyright: (c) 2025, Pablo Rodriguez <[email protected]> |
| 4 | +# Apache License Version 2.0 (see LICENSE) |
| 5 | + |
| 6 | +__metaclass__ = type |
| 7 | + |
| 8 | +DOCUMENTATION = r""" |
| 9 | +--- |
| 10 | +module: url_request |
| 11 | +short_description: Downloads/fetches the content of a SPNEGO secured URL |
| 12 | +extends_documentation_fragment: |
| 13 | + - files |
| 14 | +
|
| 15 | +description: |
| 16 | +- Downloads/fetches the content of a SPNEGO secured URL |
| 17 | +- A kerberos ticket should be already issued |
| 18 | +
|
| 19 | +author: |
| 20 | + - Adrian Fusco (@afuscoar) |
| 21 | + - Pablo Rodriguez (@pablintino) |
| 22 | +
|
| 23 | +options: |
| 24 | + url: |
| 25 | + description: |
| 26 | + - The URL to retrieve the content from |
| 27 | + required: True |
| 28 | + type: str |
| 29 | + verify_ssl: |
| 30 | + description: |
| 31 | + - Enables/disables using TLS to reach the URL |
| 32 | + required: False |
| 33 | + type: bool |
| 34 | + default: true |
| 35 | + dest: |
| 36 | + description: |
| 37 | + - Path to the destination file/dir where the content should be downloaded |
| 38 | + - If not provided the content won't be written into disk |
| 39 | + required: False |
| 40 | + type: str |
| 41 | +
|
| 42 | +""" |
| 43 | + |
| 44 | +EXAMPLES = r""" |
| 45 | +- name: Get some content |
| 46 | + url_request: |
| 47 | + url: "http://someurl.local/resource" |
| 48 | + dest: "{{ ansible_user_dir }}/content.raw" |
| 49 | + mode: "0644" |
| 50 | + register: _fetched_content |
| 51 | +
|
| 52 | +- name: Show base64 content |
| 53 | + debug: |
| 54 | + msg: "{{ _fetched_content.response_b64 }}" |
| 55 | +""" |
| 56 | + |
| 57 | +RETURN = r""" |
| 58 | +status_code: |
| 59 | + description: HTTP response code |
| 60 | + type: int |
| 61 | + returned: returned request |
| 62 | +content_type: |
| 63 | + description: HTTP response Content-Type header content |
| 64 | + type: str |
| 65 | + returned: returned request |
| 66 | +headers: |
| 67 | + description: HTTP response headers |
| 68 | + type: dict |
| 69 | + returned: returned request |
| 70 | +response_b64: |
| 71 | + description: Returned content base64 encoded |
| 72 | + type: str |
| 73 | + returned: successful request |
| 74 | +response_json: |
| 75 | + description: Returned content as a dict |
| 76 | + type: str |
| 77 | + returned: successful request that returned application/json |
| 78 | +path: |
| 79 | + description: Written file path |
| 80 | + type: str |
| 81 | + returned: successful request |
| 82 | +""" |
| 83 | + |
| 84 | +import base64 |
| 85 | +import os.path |
| 86 | +import re |
| 87 | + |
| 88 | +from ansible.module_utils.basic import AnsibleModule |
| 89 | + |
| 90 | + |
| 91 | +try: |
| 92 | + from requests import get |
| 93 | + |
| 94 | + python_requests_installed = True |
| 95 | +except ImportError: |
| 96 | + python_requests_installed = False |
| 97 | +try: |
| 98 | + from requests_kerberos import HTTPKerberosAuth, OPTIONAL |
| 99 | + |
| 100 | + python_requests_kerberos_installed = True |
| 101 | +except ImportError: |
| 102 | + python_requests_kerberos_installed = False |
| 103 | + |
| 104 | + |
| 105 | +def main(): |
| 106 | + module_args = { |
| 107 | + "url": {"type": "str", "required": True}, |
| 108 | + "verify_ssl": {"type": "bool", "default": True}, |
| 109 | + "dest": {"type": "str", "required": False}, |
| 110 | + } |
| 111 | + |
| 112 | + result = { |
| 113 | + "changed": False, |
| 114 | + } |
| 115 | + |
| 116 | + module = AnsibleModule( |
| 117 | + argument_spec=module_args, supports_check_mode=False, add_file_common_args=True |
| 118 | + ) |
| 119 | + |
| 120 | + if not python_requests_installed: |
| 121 | + module.fail_json(msg="requests required for this module.") |
| 122 | + |
| 123 | + if not python_requests_kerberos_installed: |
| 124 | + module.fail_json(msg="requests_kerberos required for this module.") |
| 125 | + |
| 126 | + url = module.params["url"] |
| 127 | + verify_ssl = module.params["verify_ssl"] |
| 128 | + |
| 129 | + auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL) |
| 130 | + try: |
| 131 | + response = get(url=url, auth=auth, verify=verify_ssl, allow_redirects=True) |
| 132 | + |
| 133 | + result["status_code"] = response.status_code |
| 134 | + result["headers"] = dict(response.headers) |
| 135 | + result["content_type"] = response.headers.get("Content-Type", None) |
| 136 | + |
| 137 | + if response.status_code < 200 or response.status_code >= 300: |
| 138 | + module.fail_json( |
| 139 | + msg=f"Error fetching the information {response.status_code}: {response.text}" |
| 140 | + ) |
| 141 | + |
| 142 | + result["response_b64"] = base64.b64encode(response.content) |
| 143 | + if "application/json" in result["content_type"]: |
| 144 | + try: |
| 145 | + result["response_json"] = response.json() |
| 146 | + except ValueError as e: |
| 147 | + module.fail_json(msg=f"Error with the JSON response: {str(e)}") |
| 148 | + |
| 149 | + if "dest" in module.params: |
| 150 | + dest = module.params["dest"] |
| 151 | + if ( |
| 152 | + os.path.exists(dest) |
| 153 | + and os.path.isdir(dest) |
| 154 | + and "content-disposition" in response.headers |
| 155 | + ): |
| 156 | + # Destination is a directory but the filename is available in Content-Disposition |
| 157 | + filename = re.findall( |
| 158 | + "filename=(.+)", response.headers["content-disposition"] |
| 159 | + ) |
| 160 | + dest = filename[0] if filename else None |
| 161 | + elif os.path.exists(dest) and os.path.isdir(dest): |
| 162 | + # Destination is a directory but we cannot guess the filename from Content-Disposition |
| 163 | + dest = None |
| 164 | + |
| 165 | + if not dest: |
| 166 | + # Reached if dest points to a directory and: |
| 167 | + # - Content-Disposition not available |
| 168 | + # - Cannot extract the filename part from the Content-Disposition header |
| 169 | + module.fail_json( |
| 170 | + msg="Destination points to a directory and the filename cannot be retrieved from the response" |
| 171 | + ) |
| 172 | + |
| 173 | + exists = os.path.exists(dest) |
| 174 | + original_sha1 = module.sha1(dest) if exists else None |
| 175 | + with open(dest, mode="wb") as file: |
| 176 | + file.write(response.content) |
| 177 | + file_args = module.load_file_common_arguments(module.params, path=dest) |
| 178 | + result["changed"] = ( |
| 179 | + (not exists) |
| 180 | + or (module.sha1(dest) != original_sha1) |
| 181 | + or module.set_fs_attributes_if_different(file_args, result["changed"]) |
| 182 | + ) |
| 183 | + result["path"] = dest |
| 184 | + |
| 185 | + except Exception as e: |
| 186 | + module.fail_json(msg=f"Error fetching the information: {str(e)}") |
| 187 | + |
| 188 | + module.exit_json(**result) |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + main() |
0 commit comments