def test_module_utils_basic_get_distribution(self): from ansible.module_utils.basic import get_distribution with patch('platform.system', return_value='Foo'): self.assertEqual(get_distribution(), None) with patch('platform.system', return_value='Linux'): with patch('platform.linux_distribution', return_value=("foo", "1", "One")): self.assertEqual(get_distribution(), "Foo") with patch('os.path.isfile', return_value=True): def _dist(distname='', version='', id='', supported_dists=(), full_distribution_name=1): if supported_dists != (): return ("AmazonFooBar", "", "") else: return ("", "", "") with patch('platform.linux_distribution', side_effect=_dist): self.assertEqual(get_distribution(), "Amazonfoobar") def _dist(distname='', version='', id='', supported_dists=(), full_distribution_name=1): if supported_dists != (): return ("Bar", "2", "Two") else: return ("", "", "") with patch('platform.linux_distribution', side_effect=_dist): self.assertEqual(get_distribution(), "Bar") with patch('platform.linux_distribution', side_effect=Exception("boo")): with patch('platform.dist', return_value=("bar", "2", "Two")): self.assertEqual(get_distribution(), "Bar")
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): ''' Fetches a file from an HTTP/FTP server using urllib2. Requires the module environment ''' if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', None) force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects) info.update(r.info()) info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError: e = get_exception() distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json(msg='%s. You can also install python-ssl from EPEL' % str(e)) else: module.fail_json(msg='%s' % str(e)) except (ConnectionError, ValueError): e = get_exception() module.fail_json(msg=str(e)) except urllib_error.HTTPError: e = get_exception() try: body = e.read() except AttributeError: body = '' info.update(dict(msg=str(e), body=body, **e.info())) info['status'] = e.code except urllib_error.URLError: e = get_exception() code = int(getattr(e, 'code', -1)) info.update(dict(msg="Request failed: %s" % str(e), status=code)) except socket.error: e = get_exception() info.update(dict(msg="Connection failure: %s" % str(e), status=-1)) except Exception: e = get_exception() info.update(dict(msg="An unknown error occurred: %s" % str(e), status=-1)) return r, info
def unimplemented_error(self): platform = get_platform() distribution = get_distribution() if distribution is not None: msg_platform = '%s (%s)' % (platform, distribution) else: msg_platform = platform self.module.fail_json( msg='hostname module cannot be used on platform %s' % msg_platform)
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): ''' Fetches a file from an HTTP/FTP server using urllib2. Requires the module environment ''' if not HAS_URLLIB2: module.fail_json(msg='urllib2 is not installed') elif not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', None) force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects) info.update(r.info()) info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.getcode())) except NoSSLError, e: distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json(msg='%s. You can also install python-ssl from EPEL' % str(e)) else: module.fail_json(msg='%s' % str(e))
def test_distro_unknown(self): with patch('ansible.module_utils.distro.id', return_value=""): assert get_distribution() == "OtherLinux"
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): '''Sends a request via HTTP(S) or FTP (needs the module as parameter) :arg module: The AnsibleModule (used to get username, password etc. (s.b.). :arg url: The url to use. :kwarg data: The data to be sent (in case of POST/PUT). :kwarg headers: A dict with the request headers. :kwarg method: "POST", "PUT", etc. :kwarg boolean use_proxy: Default: True :kwarg boolean force: If True: Do not get a cached copy (Default: False) :kwarg last_mod_time: Default: None :kwarg int timeout: Default: 10 :returns: A tuple of (**response**, **info**). Use ``response.body()`` to read the data. The **info** contains the 'status' and other meta data. When a HttpError (status > 400) occurred then ``info['body']`` contains the error response data:: Example:: data={...} resp, info = fetch_url("http://example.com", data=module.jsonify(data) header={Content-type': 'application/json'}, method="POST") status_code = info["status"] body = resp.read() if status_code >= 400 : body = info['body'] ''' if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', None) force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects) info.update(r.info()) info.update( dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError: e = get_exception() distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json( msg='%s. You can also install python-ssl from EPEL' % str(e)) else: module.fail_json(msg='%s' % str(e)) except (ConnectionError, ValueError): e = get_exception() module.fail_json(msg=str(e)) except urllib_error.HTTPError: e = get_exception() try: body = e.read() except AttributeError: body = '' info.update(dict(msg=str(e), body=body, **e.info())) info['status'] = e.code except urllib_error.URLError: e = get_exception() code = int(getattr(e, 'code', -1)) info.update(dict(msg="Request failed: %s" % str(e), status=code)) except socket.error: e = get_exception() info.update(dict(msg="Connection failure: %s" % str(e), status=-1)) except Exception: e = get_exception() info.update( dict(msg="An unknown error occurred: %s" % str(e), status=-1)) return r, info
def __init__(self, module): super(NosystemdTimezone, self).__init__(module) # Validate given timezone if 'name' in self.value: tzfile = self._verify_timezone() planned_tz = self.value['name']['planned'] # `--remove-destination` is needed if /etc/localtime is a symlink so # that it overwrites it instead of following it. self.update_timezone = ['%s --remove-destination %s /etc/localtime' % (self.module.get_bin_path('cp', required=True), tzfile)] self.update_hwclock = self.module.get_bin_path('hwclock', required=True) distribution = get_distribution() self.conf_files['name'] = '/etc/timezone' self.regexps['name'] = re.compile(r'^([^\s]+)', re.MULTILINE) self.tzline_format = '%s\n' # Distribution-specific configurations if self.module.get_bin_path('dpkg-reconfigure') is not None: # Debian/Ubuntu if 'name' in self.value: self.update_timezone = ['%s -sf %s /etc/localtime' % (self.module.get_bin_path('ln', required=True), tzfile), '%s --frontend noninteractive tzdata' % self.module.get_bin_path('dpkg-reconfigure', required=True)] self.conf_files['hwclock'] = '/etc/default/rcS' elif distribution == 'Alpine' or distribution == 'Gentoo': self.conf_files['hwclock'] = '/etc/conf.d/hwclock' if distribution == 'Alpine': self.update_timezone = ['%s -z %s' % (self.module.get_bin_path('setup-timezone', required=True), planned_tz)] else: # RHEL/CentOS/SUSE if self.module.get_bin_path('tzdata-update') is not None: # tzdata-update cannot update the timezone if /etc/localtime is # a symlink so we have to use cp to update the time zone which # was set above. if not os.path.islink('/etc/localtime'): self.update_timezone = [self.module.get_bin_path('tzdata-update', required=True)] # else: # self.update_timezone = 'cp --remove-destination ...' <- configured above self.conf_files['name'] = '/etc/sysconfig/clock' self.conf_files['hwclock'] = '/etc/sysconfig/clock' try: f = open(self.conf_files['name'], 'r') except IOError as err: if self._allow_ioerror(err, 'name'): # If the config file doesn't exist detect the distribution and set regexps. if distribution == 'SuSE': # For SUSE self.regexps['name'] = self.dist_regexps['SuSE'] self.tzline_format = self.dist_tzline_format['SuSE'] else: # For RHEL/CentOS self.regexps['name'] = self.dist_regexps['redhat'] self.tzline_format = self.dist_tzline_format['redhat'] else: self.abort('could not read configuration file "%s"' % self.conf_files['name']) else: # The key for timezone might be `ZONE` or `TIMEZONE` # (the former is used in RHEL/CentOS and the latter is used in SUSE linux). # So check the content of /etc/sysconfig/clock and decide which key to use. sysconfig_clock = f.read() f.close() if re.search(r'^TIMEZONE\s*=', sysconfig_clock, re.MULTILINE): # For SUSE self.regexps['name'] = self.dist_regexps['SuSE'] self.tzline_format = self.dist_tzline_format['SuSE'] else: # For RHEL/CentOS self.regexps['name'] = self.dist_regexps['redhat'] self.tzline_format = self.dist_tzline_format['redhat']
def test_distro_amazon_linux(self): with patch('ansible.module_utils.distro.name', return_value="Amazon Linux AMI"): assert get_distribution() == "Amazon"
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): '''Sends a request via HTTP(S) or FTP (needs the module as parameter) :arg module: The AnsibleModule (used to get username, password etc. (s.b.). :arg url: The url to use. :kwarg data: The data to be sent (in case of POST/PUT). :kwarg headers: A dict with the request headers. :kwarg method: "POST", "PUT", etc. :kwarg boolean use_proxy: Default: True :kwarg boolean force: If True: Do not get a cached copy (Default: False) :kwarg last_mod_time: Default: None :kwarg int timeout: Default: 10 :returns: A tuple of (**response**, **info**). Use ``response.body()`` to read the data. The **info** contains the 'status' and other meta data. When a HttpError (status > 400) occurred then ``info['body']`` contains the error response data:: Example:: data={...} resp, info = fetch_url("http://example.com", data=module.jsonify(data) header={Content-type': 'application/json'}, method="POST") status_code = info["status"] body = resp.read() if status_code >= 400 : body = info['body'] ''' if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', None) force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects) info.update(r.info()) info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError: e = get_exception() distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json(msg='%s. You can also install python-ssl from EPEL' % str(e)) else: module.fail_json(msg='%s' % str(e)) except (ConnectionError, ValueError): e = get_exception() module.fail_json(msg=str(e)) except urllib_error.HTTPError: e = get_exception() try: body = e.read() except AttributeError: body = '' info.update(dict(msg=str(e), body=body, **e.info())) info['status'] = e.code except urllib_error.URLError: e = get_exception() code = int(getattr(e, 'code', -1)) info.update(dict(msg="Request failed: %s" % str(e), status=code)) except socket.error: e = get_exception() info.update(dict(msg="Connection failure: %s" % str(e), status=-1)) except Exception: e = get_exception() info.update(dict(msg="An unknown error occurred: %s" % str(e), status=-1)) return r, info
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): ''' Fetches a file from an HTTP/FTP server using urllib2. Requires the module environment ''' if not HAS_URLLIB2: module.fail_json(msg='urllib2 is not installed') elif not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', None) force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects) info.update(r.info()) info.update( dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.getcode())) except NoSSLError: e = get_exception() distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json( msg='%s. You can also install python-ssl from EPEL' % str(e)) else: module.fail_json(msg='%s' % str(e)) except (ConnectionError, ValueError): e = get_exception() module.fail_json(msg=str(e)) except urllib2.HTTPError: e = get_exception() try: body = e.read() except AttributeError: body = '' info.update(dict(msg=str(e), body=body, **e.info())) info['status'] = e.code except urllib2.URLError: e = get_exception() code = int(getattr(e, 'code', -1)) info.update(dict(msg="Request failed: %s" % str(e), status=code)) except socket.error: e = get_exception() info.update(dict(msg="Connection failure: %s" % str(e), status=-1)) except Exception: e = get_exception() info.update( dict(msg="An unknown error occurred: %s" % str(e), status=-1)) return r, info
def fetch_url(module, url, method=None, timeout=10, follow_redirects=True): if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # ensure we use proper tempdir old_tempdir = tempfile.tempdir tempfile.tempdir = module.tmpdir r = None info = dict(url=url, status=-1) try: r = open_url(url, method=method, timeout=timeout, follow_redirects=follow_redirects) # Lowercase keys, to conform to py2 behavior, so that py3 and py2 are # predictable info.update(dict((k.lower(), v) for k, v in r.info().items())) # Don't be lossy, append header values for duplicate headers # In Py2 there is nothing that needs done, py2 does this for us if PY3: temp_headers = {} for name, value in r.headers.items(): # The same as above, lower case keys to match py2 behavior, and # create more consistent results name = name.lower() if name in temp_headers: temp_headers[name] = ', '.join((temp_headers[name], value)) else: temp_headers[name] = value info.update(temp_headers) # finally update the result with a message about the fetch info.update( dict(msg='OK (%s bytes)' % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError as e: distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json( msg='%s. You can also install python-ssl from EPEL' % to_native(e), **info) else: module.fail_json(msg='%s' % to_native(e), **info) except (ConnectionError, ValueError) as e: module.fail_json(msg=to_native(e), **info) except urllib_error.HTTPError as e: try: body = e.read() except AttributeError: body = '' # Try to add exception info to the output but don't fail if we can't try: # Lowercase keys, to conform to py2 behavior, so that py3 and py2 # are predictable info.update(dict((k.lower(), v) for k, v in e.info().items())) except Exception: pass info.update({'msg': to_native(e), 'body': body, 'status': e.code}) except urllib_error.URLError as e: code = int(getattr(e, 'code', -1)) info.update(dict(msg='Request failed: %s' % to_native(e), status=code)) except socket.error as e: info.update( dict(msg='Connection failure: %s' % to_native(e), status=-1)) except httplib.BadStatusLine as e: info.update( dict( msg=('Connection failure: connection was closed before a valid' ' response was received: %s') % to_native(e.line), status=-1)) except Exception as e: info.update(dict(msg='An unknown error occurred: %s' % to_native(e), status=-1), exception=traceback.format_exc()) finally: tempfile.tempdir = old_tempdir return r, info
def test_distro_known(self): with patch('ansible.module_utils.distro.id', return_value="alpine"): assert get_distribution() == "Alpine" with patch('ansible.module_utils.distro.id', return_value="arch"): assert get_distribution() == "Arch" with patch('ansible.module_utils.distro.id', return_value="centos"): assert get_distribution() == "Centos" with patch('ansible.module_utils.distro.id', return_value="clear-linux-os"): assert get_distribution() == "Clear-linux-os" with patch('ansible.module_utils.distro.id', return_value="coreos"): assert get_distribution() == "Coreos" with patch('ansible.module_utils.distro.id', return_value="debian"): assert get_distribution() == "Debian" with patch('ansible.module_utils.distro.id', return_value="flatcar"): assert get_distribution() == "Flatcar" with patch('ansible.module_utils.distro.id', return_value="linuxmint"): assert get_distribution() == "Linuxmint" with patch('ansible.module_utils.distro.id', return_value="opensuse"): assert get_distribution() == "Opensuse" with patch('ansible.module_utils.distro.id', return_value="oracle"): assert get_distribution() == "Oracle" with patch('ansible.module_utils.distro.id', return_value="raspian"): assert get_distribution() == "Raspian" with patch('ansible.module_utils.distro.id', return_value="rhel"): assert get_distribution() == "Redhat" with patch('ansible.module_utils.distro.id', return_value="ubuntu"): assert get_distribution() == "Ubuntu" with patch('ansible.module_utils.distro.id', return_value="virtuozzo"): assert get_distribution() == "Virtuozzo" with patch('ansible.module_utils.distro.id', return_value="foo"): assert get_distribution() == "Foo"
def test_distro_amazon_part_of_another_name(self): with patch('ansible.module_utils.distro.name', return_value="AmazonFooBar"): assert get_distribution() == "Amazonfoobar"
def test_distro_amazon_linux_long(self): with patch('ansible.module_utils.distro.id', return_value="amazon"): assert get_distribution() == "Amazon"
def fetch_url(module, url, method=None, timeout=10, follow_redirects=True): if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # ensure we use proper tempdir old_tempdir = tempfile.tempdir tempfile.tempdir = getattr(module, 'tmpdir', old_tempdir) r = None info = dict(url=url) try: r = open_url( url, method=method, timeout=timeout, follow_redirects=follow_redirects) info.update(r.info()) # finally update the result with a message about the fetch info.update( dict( msg='OK (%s bytes)' % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError as e: distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json(msg='%s. You can also install python-ssl from EPEL' % to_native(e)) else: module.fail_json(msg='%s' % to_native(e)) except (ConnectionError, ValueError) as e: module.fail_json(msg=to_native(e)) except urllib_error.HTTPError as e: try: body = e.read() except AttributeError: body = '' # Try to add exception info to the output but don't fail if we can't try: info.update(dict(**e.info())) except: pass info.update({'msg': to_native(e), 'body': body, 'status': e.code}) except urllib_error.URLError as e: code = int(getattr(e, 'code', -1)) info.update(dict(msg='Request failed: %s' % to_native(e), status=code)) except socket.error as e: info.update( dict(msg='Connection failure: %s' % to_native(e), status=-1)) except Exception as e: info.update( dict(msg='An unknown error occurred: %s' % to_native(e), status=-1), exception=traceback.format_exc()) finally: tempfile.tempdir = old_tempdir return r, info
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): """Sends a request via HTTP(S) or FTP (needs the module as parameter) :arg module: The AnsibleModule (used to get username, password etc. (s.b.). :arg url: The url to use. :kwarg data: The data to be sent (in case of POST/PUT). :kwarg headers: A dict with the request headers. :kwarg method: "POST", "PUT", etc. :kwarg boolean use_proxy: Default: True :kwarg boolean force: If True: Do not get a cached copy (Default: False) :kwarg last_mod_time: Default: None :kwarg int timeout: Default: 10 :returns: A tuple of (**response**, **info**). Use ``response.read()`` to read the data. The **info** contains the 'status' and other meta data. When a HttpError (status > 400) occurred then ``info['body']`` contains the error response data:: Example:: data={...} resp, info = fetch_url(module, "http://example.com", data=module.jsonify(data), headers={'Content-type': 'application/json'}, method="POST") status_code = info["status"] body = resp.read() if status_code >= 400 : body = info['body'] """ if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # ensure we use proper tempdir old_tempdir = tempfile.tempdir tempfile.tempdir = module.tmpdir # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', 'ansible-httpget') force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') client_cert = module.params.get('client_cert') client_key = module.params.get('client_key') cookies = cookiejar.LWPCookieJar() r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects, client_cert=client_cert, client_key=client_key, cookies=cookies) # Lowercase keys, to conform to py2 behavior, so that py3 and py2 are predictable info.update(dict((k.lower(), v) for k, v in r.info().items())) # Don't be lossy, append header values for duplicate headers # In Py2 there is nothing that needs done, py2 does this for us if PY3: temp_headers = {} for name, value in r.headers.items(): # The same as above, lower case keys to match py2 behavior, and create more consistent results name = name.lower() if name in temp_headers: temp_headers[name] = ', '.join((temp_headers[name], value)) else: temp_headers[name] = value info.update(temp_headers) # parse the cookies into a nice dictionary cookie_list = [] cookie_dict = dict() # Python sorts cookies in order of most specific (ie. longest) path first. See ``CookieJar._cookie_attrs`` # Cookies with the same path are reversed from response order. # This code makes no assumptions about that, and accepts the order given by python for cookie in cookies: cookie_dict[cookie.name] = cookie.value cookie_list.append((cookie.name, cookie.value)) info['cookies_string'] = '; '.join('%s=%s' % c for c in cookie_list) info['cookies'] = cookie_dict # finally update the result with a message about the fetch info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError as e: distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json(msg='%s. You can also install python-ssl from EPEL' % to_native(e)) else: module.fail_json(msg='%s' % to_native(e)) except (ConnectionError, ValueError) as e: module.fail_json(msg=to_native(e)) except urllib_error.HTTPError as e: try: body = e.read() except AttributeError: body = '' # Try to add exception info to the output but don't fail if we can't try: info.update(dict(**e.info())) except: pass info.update({'msg': to_native(e), 'body': body, 'status': e.code}) except urllib_error.URLError as e: code = int(getattr(e, 'code', -1)) info.update(dict(msg="Request failed: %s" % to_native(e), status=code)) except socket.error as e: info.update(dict(msg="Connection failure: %s" % to_native(e), status=-1)) except httplib.BadStatusLine as e: info.update(dict(msg="Connection failure: connection was closed before a valid response was received: %s" % to_native(e.line), status=-1)) except Exception as e: info.update(dict(msg="An unknown error occurred: %s" % to_native(e), status=-1), exception=traceback.format_exc()) finally: tempfile.tempdir = old_tempdir return r, info
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): """Sends a request via HTTP(S) or FTP (needs the module as parameter) :arg module: The AnsibleModule (used to get username, password etc. (s.b.). :arg url: The url to use. :kwarg data: The data to be sent (in case of POST/PUT). :kwarg headers: A dict with the request headers. :kwarg method: "POST", "PUT", etc. :kwarg boolean use_proxy: Default: True :kwarg boolean force: If True: Do not get a cached copy (Default: False) :kwarg last_mod_time: Default: None :kwarg int timeout: Default: 10 :returns: A tuple of (**response**, **info**). Use ``response.body()`` to read the data. The **info** contains the 'status' and other meta data. When a HttpError (status > 400) occurred then ``info['body']`` contains the error response data:: Example:: data={...} resp, info = fetch_url(module, "http://example.com", data=module.jsonify(data) header={Content-type': 'application/json'}, method="POST") status_code = info["status"] body = resp.read() if status_code >= 400 : body = info['body'] """ if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', 'ansible-httpget') force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') client_cert = module.params.get('client_cert') client_key = module.params.get('client_key') cookies = cookiejar.LWPCookieJar() r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects, client_cert=client_cert, client_key=client_key, cookies=cookies) info.update(r.info()) # parse the cookies into a nice dictionary cookie_dict = dict() for cookie in cookies: cookie_dict[cookie.name] = cookie.value info['cookies'] = cookie_dict # finally update the result with a message about the fetch info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError as e: distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json(msg='%s. You can also install python-ssl from EPEL' % to_native(e)) else: module.fail_json(msg='%s' % to_native(e)) except (ConnectionError, ValueError) as e: module.fail_json(msg=to_native(e)) except urllib_error.HTTPError as e: try: body = e.read() except AttributeError: body = '' # Try to add exception info to the output but don't fail if we can't try: info.update(dict(**e.info())) except: pass info.update({'msg': to_native(e), 'body': body, 'status': e.code}) except urllib_error.URLError as e: code = int(getattr(e, 'code', -1)) info.update(dict(msg="Request failed: %s" % to_native(e), status=code)) except socket.error as e: info.update(dict(msg="Connection failure: %s" % to_native(e), status=-1)) except Exception as e: info.update(dict(msg="An unknown error occurred: %s" % to_native(e), status=-1), exception=traceback.format_exc()) return r, info
def fetch_url(module, url, method=None, timeout=10, follow_redirects=True): if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # ensure we use proper tempdir old_tempdir = tempfile.tempdir tempfile.tempdir = getattr(module, 'tmpdir', old_tempdir) r = None info = dict(url=url) try: r = open_url(url, method=method, timeout=timeout, follow_redirects=follow_redirects) info.update(r.info()) # finally update the result with a message about the fetch info.update( dict(msg='OK (%s bytes)' % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError as e: distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json( msg='%s. You can also install python-ssl from EPEL' % to_native(e)) else: module.fail_json(msg='%s' % to_native(e)) except (ConnectionError, ValueError) as e: module.fail_json(msg=to_native(e)) except urllib_error.HTTPError as e: try: body = e.read() except AttributeError: body = '' # Try to add exception info to the output but don't fail if we can't try: info.update(dict(**e.info())) except: pass info.update({'msg': to_native(e), 'body': body, 'status': e.code}) except urllib_error.URLError as e: code = int(getattr(e, 'code', -1)) info.update(dict(msg='Request failed: %s' % to_native(e), status=code)) except socket.error as e: info.update( dict(msg='Connection failure: %s' % to_native(e), status=-1)) except Exception as e: info.update(dict(msg='An unknown error occurred: %s' % to_native(e), status=-1), exception=traceback.format_exc()) finally: tempfile.tempdir = old_tempdir return r, info
def test_get_distribution_not_linux(): """If it's not Linux, then it has no distribution""" with patch('platform.system', return_value='Foo'): assert get_distribution() is None
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): """Sends a request via HTTP(S) or FTP (needs the module as parameter) :arg module: The AnsibleModule (used to get username, password etc. (s.b.). :arg url: The url to use. :kwarg data: The data to be sent (in case of POST/PUT). :kwarg headers: A dict with the request headers. :kwarg method: "POST", "PUT", etc. :kwarg boolean use_proxy: Default: True :kwarg boolean force: If True: Do not get a cached copy (Default: False) :kwarg last_mod_time: Default: None :kwarg int timeout: Default: 10 :returns: A tuple of (**response**, **info**). Use ``response.read()`` to read the data. The **info** contains the 'status' and other meta data. When a HttpError (status > 400) occurred then ``info['body']`` contains the error response data:: Example:: data={...} resp, info = fetch_url(module, "http://example.com", data=module.jsonify(data) header={'Content-type': 'application/json'}, method="POST") status_code = info["status"] body = resp.read() if status_code >= 400 : body = info['body'] """ if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # ensure we use proper tempdir old_tempdir = tempfile.tempdir tempfile.tempdir = module.tmpdir # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', 'ansible-httpget') force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') client_cert = module.params.get('client_cert') client_key = module.params.get('client_key') cookies = cookiejar.LWPCookieJar() r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects, client_cert=client_cert, client_key=client_key, cookies=cookies) # Lowercase keys, to conform to py2 behavior, so that py3 and py2 are predictable info.update(dict((k.lower(), v) for k, v in r.info().items())) # Don't be lossy, append header values for duplicate headers # In Py2 there is nothing that needs done, py2 does this for us if PY3: temp_headers = {} for name, value in r.headers.items(): # The same as above, lower case keys to match py2 behavior, and create more consistent results name = name.lower() if name in temp_headers: temp_headers[name] = ', '.join((temp_headers[name], value)) else: temp_headers[name] = value info.update(temp_headers) # parse the cookies into a nice dictionary cookie_list = [] cookie_dict = dict() # Python sorts cookies in order of most specific (ie. longest) path first. See ``CookieJar._cookie_attrs`` # Cookies with the same path are reversed from response order. # This code makes no assumptions about that, and accepts the order given by python for cookie in cookies: cookie_dict[cookie.name] = cookie.value cookie_list.append((cookie.name, cookie.value)) info['cookies_string'] = '; '.join('%s=%s' % c for c in cookie_list) info['cookies'] = cookie_dict # finally update the result with a message about the fetch info.update( dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError as e: distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json( msg='%s. You can also install python-ssl from EPEL' % to_native(e)) else: module.fail_json(msg='%s' % to_native(e)) except (ConnectionError, ValueError) as e: module.fail_json(msg=to_native(e)) except urllib_error.HTTPError as e: try: body = e.read() except AttributeError: body = '' # Try to add exception info to the output but don't fail if we can't try: info.update(dict(**e.info())) except: pass info.update({'msg': to_native(e), 'body': body, 'status': e.code}) except urllib_error.URLError as e: code = int(getattr(e, 'code', -1)) info.update(dict(msg="Request failed: %s" % to_native(e), status=code)) except socket.error as e: info.update( dict(msg="Connection failure: %s" % to_native(e), status=-1)) except httplib.BadStatusLine as e: info.update( dict( msg= "Connection failure: connection was closed before a valid response was received: %s" % to_native(e.line), status=-1)) except Exception as e: info.update(dict(msg="An unknown error occurred: %s" % to_native(e), status=-1), exception=traceback.format_exc()) finally: tempfile.tempdir = old_tempdir return r, info
def fetch_url(module, url, data=None, headers=None, method=None, use_proxy=True, force=False, last_mod_time=None, timeout=10): """Sends a request via HTTP(S) or FTP (needs the module as parameter) :arg module: The AnsibleModule (used to get username, password etc. (s.b.). :arg url: The url to use. :kwarg data: The data to be sent (in case of POST/PUT). :kwarg headers: A dict with the request headers. :kwarg method: "POST", "PUT", etc. :kwarg boolean use_proxy: Default: True :kwarg boolean force: If True: Do not get a cached copy (Default: False) :kwarg last_mod_time: Default: None :kwarg int timeout: Default: 10 :returns: A tuple of (**response**, **info**). Use ``response.body()`` to read the data. The **info** contains the 'status' and other meta data. When a HttpError (status > 400) occurred then ``info['body']`` contains the error response data:: Example:: data={...} resp, info = fetch_url(module, "http://example.com", data=module.jsonify(data) header={Content-type': 'application/json'}, method="POST") status_code = info["status"] body = resp.read() if status_code >= 400 : body = info['body'] """ if not HAS_URLPARSE: module.fail_json(msg='urlparse is not installed') # Get validate_certs from the module params validate_certs = module.params.get('validate_certs', True) username = module.params.get('url_username', '') password = module.params.get('url_password', '') http_agent = module.params.get('http_agent', 'ansible-httpget') force_basic_auth = module.params.get('force_basic_auth', '') follow_redirects = module.params.get('follow_redirects', 'urllib2') client_cert = module.params.get('client_cert') client_key = module.params.get('client_key') cookies = cookiejar.LWPCookieJar() r = None info = dict(url=url) try: r = open_url(url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=username, url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth, follow_redirects=follow_redirects, client_cert=client_cert, client_key=client_key, cookies=cookies) info.update(r.info()) # parse the cookies into a nice dictionary cookie_dict = dict() for cookie in cookies: cookie_dict[cookie.name] = cookie.value info['cookies'] = cookie_dict # finally update the result with a message about the fetch info.update( dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code)) except NoSSLError as e: distribution = get_distribution() if distribution is not None and distribution.lower() == 'redhat': module.fail_json( msg='%s. You can also install python-ssl from EPEL' % to_native(e)) else: module.fail_json(msg='%s' % to_native(e)) except (ConnectionError, ValueError) as e: module.fail_json(msg=to_native(e)) except urllib_error.HTTPError as e: try: body = e.read() except AttributeError: body = '' # Try to add exception info to the output but don't fail if we can't try: info.update(dict(**e.info())) except: pass info.update({'msg': to_native(e), 'body': body, 'status': e.code}) except urllib_error.URLError as e: code = int(getattr(e, 'code', -1)) info.update(dict(msg="Request failed: %s" % to_native(e), status=code)) except socket.error as e: info.update( dict(msg="Connection failure: %s" % to_native(e), status=-1)) except Exception as e: info.update(dict(msg="An unknown error occurred: %s" % to_native(e), status=-1), exception=traceback.format_exc()) return r, info
def test_distro_known(self): with patch('ansible.module_utils.distro.name', return_value="foo"): assert get_distribution() == "Foo"