2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Romeo Theriault <romeot () hawaii.edu>
|
2017-07-29 09:35:25 +00:00
|
|
|
# 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
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['stableinterface'],
|
|
|
|
'supported_by': 'core'}
|
|
|
|
|
2017-07-18 10:13:14 +00:00
|
|
|
DOCUMENTATION = r'''
|
2014-09-26 01:01:01 +00:00
|
|
|
---
|
|
|
|
module: uri
|
|
|
|
short_description: Interacts with webservices
|
|
|
|
description:
|
|
|
|
- Interacts with HTTP and HTTPS web services and supports Digest, Basic and WSSE
|
|
|
|
HTTP authentication mechanisms.
|
2017-06-26 15:26:53 +00:00
|
|
|
- For Windows targets, use the M(win_uri) module instead.
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: "1.1"
|
|
|
|
options:
|
|
|
|
url:
|
|
|
|
description:
|
|
|
|
- HTTP or HTTPS URL in the form (http|https)://host.domain[:port]/path
|
|
|
|
required: true
|
|
|
|
dest:
|
|
|
|
description:
|
2016-02-05 18:15:39 +00:00
|
|
|
- path of where to download the file to (if desired). If I(dest) is a
|
|
|
|
directory, the basename of the file on the remote server will be used.
|
2014-09-26 01:01:01 +00:00
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- username for the module to use for Digest, Basic or WSSE authentication.
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- password for the module to use for Digest, Basic or WSSE authentication.
|
|
|
|
body:
|
|
|
|
description:
|
2016-02-05 18:15:39 +00:00
|
|
|
- The body of the http request/response to the web service. If C(body_format) is set
|
2016-10-13 14:47:50 +00:00
|
|
|
to 'json' it will take an already formatted JSON string or convert a data structure
|
2016-02-05 18:15:39 +00:00
|
|
|
into JSON.
|
2015-03-27 10:11:25 +00:00
|
|
|
body_format:
|
|
|
|
description:
|
2016-02-05 18:15:39 +00:00
|
|
|
- The serialization format of the body. When set to json, encodes the
|
|
|
|
body argument, if needed, and automatically sets the Content-Type header accordingly.
|
2017-01-13 18:19:38 +00:00
|
|
|
As of C(2.3) it is possible to override the `Content-Type` header, when
|
|
|
|
set to json via the I(headers) option.
|
2016-12-08 02:33:38 +00:00
|
|
|
choices: [ "raw", "json" ]
|
2015-03-27 10:11:25 +00:00
|
|
|
default: raw
|
2015-07-23 19:52:11 +00:00
|
|
|
version_added: "2.0"
|
2014-09-26 01:01:01 +00:00
|
|
|
method:
|
|
|
|
description:
|
2015-10-19 14:13:45 +00:00
|
|
|
- The HTTP method of the request or response. It MUST be uppercase.
|
2015-07-09 09:39:46 +00:00
|
|
|
choices: [ "GET", "POST", "PUT", "HEAD", "DELETE", "OPTIONS", "PATCH", "TRACE", "CONNECT", "REFRESH" ]
|
2014-09-26 01:01:01 +00:00
|
|
|
default: "GET"
|
|
|
|
return_content:
|
|
|
|
description:
|
2017-07-26 20:21:53 +00:00
|
|
|
- Whether or not to return the body of the response as a "content" key in
|
2016-02-05 18:15:39 +00:00
|
|
|
the dictionary result. If the reported Content-type is
|
|
|
|
"application/json", then the JSON is additionally loaded into a key
|
|
|
|
called C(json) in the dictionary results.
|
2017-07-18 10:13:14 +00:00
|
|
|
type: bool
|
|
|
|
default: 'no'
|
2014-09-26 01:01:01 +00:00
|
|
|
force_basic_auth:
|
|
|
|
description:
|
2016-02-05 18:15:39 +00:00
|
|
|
- The library used by the uri module only sends authentication information when a webservice
|
2014-09-26 01:01:01 +00:00
|
|
|
responds to an initial request with a 401 status. Since some basic auth services do not properly
|
|
|
|
send a 401, logins will fail. This option forces the sending of the Basic authentication header
|
|
|
|
upon initial request.
|
2017-07-18 10:13:14 +00:00
|
|
|
type: bool
|
|
|
|
default: 'no'
|
2014-09-26 01:01:01 +00:00
|
|
|
follow_redirects:
|
|
|
|
description:
|
|
|
|
- Whether or not the URI module should follow redirects. C(all) will follow all redirects.
|
2016-02-05 18:15:39 +00:00
|
|
|
C(safe) will follow only "safe" redirects, where "safe" means that the client is only
|
2014-09-26 01:01:01 +00:00
|
|
|
doing a GET or HEAD on the URI to which it is being redirected. C(none) will not follow
|
2016-02-05 18:15:39 +00:00
|
|
|
any redirects. Note that C(yes) and C(no) choices are accepted for backwards compatibility,
|
2014-09-26 01:01:01 +00:00
|
|
|
where C(yes) is the equivalent of C(all) and C(no) is the equivalent of C(safe). C(yes) and C(no)
|
|
|
|
are deprecated and will be removed in some future version of Ansible.
|
|
|
|
choices: [ "all", "safe", "none" ]
|
|
|
|
default: "safe"
|
|
|
|
creates:
|
|
|
|
description:
|
|
|
|
- a filename, when it already exists, this step will not be run.
|
|
|
|
removes:
|
|
|
|
description:
|
|
|
|
- a filename, when it does not exist, this step will not be run.
|
|
|
|
status_code:
|
|
|
|
description:
|
2016-02-05 18:15:39 +00:00
|
|
|
- A valid, numeric, HTTP status code that signifies success of the
|
|
|
|
request. Can also be comma separated list of status codes.
|
2014-09-26 01:01:01 +00:00
|
|
|
default: 200
|
|
|
|
timeout:
|
|
|
|
description:
|
2016-02-05 18:15:39 +00:00
|
|
|
- The socket level timeout in seconds
|
2014-09-26 01:01:01 +00:00
|
|
|
default: 30
|
|
|
|
HEADER_:
|
|
|
|
description:
|
|
|
|
- Any parameter starting with "HEADER_" is a sent with your request as a header.
|
|
|
|
For example, HEADER_Content-Type="application/json" would send the header
|
|
|
|
"Content-Type" along with your request with a value of "application/json".
|
2017-12-19 22:36:02 +00:00
|
|
|
This option is deprecated as of C(2.1) and will be removed in Ansible-2.9.
|
|
|
|
Use I(headers) instead.
|
2016-02-05 18:15:39 +00:00
|
|
|
headers:
|
|
|
|
description:
|
2017-01-13 18:19:38 +00:00
|
|
|
- Add custom HTTP headers to a request in the format of a YAML hash. As
|
|
|
|
of C(2.3) supplying C(Content-Type) here will override the header
|
|
|
|
generated by supplying C(json) for I(body_format).
|
2016-02-05 18:15:39 +00:00
|
|
|
version_added: '2.1'
|
2014-09-26 01:01:01 +00:00
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- all arguments accepted by the M(file) module also work here
|
2015-05-28 22:02:28 +00:00
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only
|
|
|
|
set to C(no) used on personally controlled sites using self-signed
|
|
|
|
certificates. Prior to 1.9.2 the code defaulted to C(no).
|
2017-07-18 10:13:14 +00:00
|
|
|
type: bool
|
2015-05-28 22:02:28 +00:00
|
|
|
default: 'yes'
|
|
|
|
version_added: '1.9.2'
|
2017-04-07 16:54:37 +00:00
|
|
|
client_cert:
|
|
|
|
description:
|
|
|
|
- PEM formatted certificate chain file to be used for SSL client
|
|
|
|
authentication. This file can also include the key as well, and if
|
|
|
|
the key is included, I(client_key) is not required
|
2017-07-18 10:13:14 +00:00
|
|
|
version_added: '2.4'
|
2017-04-07 16:54:37 +00:00
|
|
|
client_key:
|
|
|
|
description:
|
|
|
|
- PEM formatted file that contains your private key to be used for SSL
|
|
|
|
client authentication. If I(client_cert) contains both the certificate
|
|
|
|
and key, this option is not required.
|
2017-07-18 10:13:14 +00:00
|
|
|
version_added: '2.4'
|
2016-02-23 17:53:02 +00:00
|
|
|
notes:
|
2017-07-18 10:13:14 +00:00
|
|
|
- The dependency on httplib2 was removed in Ansible 2.1.
|
|
|
|
- The module returns all the HTTP headers in lower-case.
|
2017-06-26 15:26:53 +00:00
|
|
|
- For Windows targets, use the M(win_uri) module instead.
|
2017-07-18 10:13:14 +00:00
|
|
|
author:
|
|
|
|
- Romeo Theriault (@romeotheriault)
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
2017-07-18 10:13:14 +00:00
|
|
|
EXAMPLES = r'''
|
2016-11-16 17:24:21 +00:00
|
|
|
- name: Check that you can connect (GET) to a page and it returns a status 200
|
|
|
|
uri:
|
2016-12-02 15:27:26 +00:00
|
|
|
url: http://www.example.com
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-02-05 18:15:39 +00:00
|
|
|
# Check that a page returns a status 200 and fail if the word AWESOME is not
|
|
|
|
# in the page contents.
|
2016-11-16 17:24:21 +00:00
|
|
|
- uri:
|
|
|
|
url: http://www.example.com
|
|
|
|
return_content: yes
|
2014-09-26 01:01:01 +00:00
|
|
|
register: webpage
|
|
|
|
|
2016-11-16 17:24:21 +00:00
|
|
|
- name: Fail if AWESOME is not in the page content
|
|
|
|
fail:
|
2015-12-02 20:01:16 +00:00
|
|
|
when: "'AWESOME' not in webpage.content"
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
|
2016-11-16 17:24:21 +00:00
|
|
|
- name: Create a JIRA issue
|
|
|
|
uri:
|
2016-02-05 18:15:39 +00:00
|
|
|
url: https://your.jira.example.com/rest/api/2/issue/
|
2015-05-28 05:27:46 +00:00
|
|
|
method: POST
|
2016-02-05 18:15:39 +00:00
|
|
|
user: your_username
|
|
|
|
password: your_pass
|
2015-05-28 05:27:46 +00:00
|
|
|
body: "{{ lookup('file','issue.json') }}"
|
2016-02-05 18:15:39 +00:00
|
|
|
force_basic_auth: yes
|
2015-05-28 05:27:46 +00:00
|
|
|
status_code: 201
|
2016-02-05 18:15:39 +00:00
|
|
|
body_format: json
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Login to a form based webpage, then use the returned cookie to
|
|
|
|
# access the app in later tasks
|
2015-05-28 22:02:28 +00:00
|
|
|
|
2015-05-28 05:27:46 +00:00
|
|
|
- uri:
|
2016-02-28 10:51:17 +00:00
|
|
|
url: https://your.form.based.auth.example.com/index.php
|
2015-05-28 05:27:46 +00:00
|
|
|
method: POST
|
2016-02-05 18:15:39 +00:00
|
|
|
body: "name=your_username&password=your_password&enter=Sign%20in"
|
2015-05-28 05:27:46 +00:00
|
|
|
status_code: 302
|
2017-02-14 19:31:12 +00:00
|
|
|
headers:
|
|
|
|
Content-Type: "application/x-www-form-urlencoded"
|
2015-05-28 22:02:28 +00:00
|
|
|
register: login
|
2015-05-28 05:27:46 +00:00
|
|
|
|
|
|
|
- uri:
|
|
|
|
url: https://your.form.based.auth.example.com/dashboard.php
|
|
|
|
method: GET
|
|
|
|
return_content: yes
|
2017-02-14 19:31:12 +00:00
|
|
|
headers:
|
|
|
|
Cookie: "{{login.set_cookie}}"
|
2015-05-28 22:02:28 +00:00
|
|
|
|
2016-11-16 17:24:21 +00:00
|
|
|
- name: Queue build of a project in Jenkins
|
|
|
|
uri:
|
2016-02-05 18:15:39 +00:00
|
|
|
url: "http://{{ jenkins.host }}/job/{{ jenkins.job }}/build?token={{ jenkins.token }}"
|
2015-05-28 05:27:46 +00:00
|
|
|
method: GET
|
|
|
|
user: "{{ jenkins.user }}"
|
|
|
|
password: "{{ jenkins.password }}"
|
|
|
|
force_basic_auth: yes
|
|
|
|
status_code: 201
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
2017-07-18 10:13:14 +00:00
|
|
|
RETURN = r'''
|
|
|
|
# The return information includes all the HTTP headers in lower-case.
|
|
|
|
msg:
|
|
|
|
description: The HTTP message from the request
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
sample: OK (unknown bytes)
|
|
|
|
redirected:
|
|
|
|
description: Whether the request was redirected
|
|
|
|
returned: always
|
|
|
|
type: bool
|
|
|
|
sample: false
|
|
|
|
status:
|
|
|
|
description: The HTTP status code from the request
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
sample: 200
|
|
|
|
url:
|
|
|
|
description: The actual URL used for the request
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
sample: https://www.ansible.com/
|
|
|
|
'''
|
|
|
|
|
2016-10-24 05:17:06 +00:00
|
|
|
import cgi
|
|
|
|
import datetime
|
2017-07-29 09:35:25 +00:00
|
|
|
import json
|
2016-10-24 05:17:06 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2017-07-29 09:35:25 +00:00
|
|
|
import traceback
|
2016-10-24 05:17:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
import ansible.module_utils.six as six
|
2017-07-29 09:35:25 +00:00
|
|
|
from ansible.module_utils._text import to_native, to_text
|
2016-10-24 05:17:06 +00:00
|
|
|
from ansible.module_utils.urls import fetch_url, url_argument_spec
|
|
|
|
|
2017-12-14 21:54:59 +00:00
|
|
|
JSON_CANDIDATES = ('text', 'json', 'javascript')
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
def write_file(module, url, dest, content):
|
|
|
|
# create a tempfile with some test content
|
2018-02-20 13:40:45 +00:00
|
|
|
fd, tmpsrc = tempfile.mkstemp(dir=getattr(module, 'tmpdir', None))
|
2014-09-26 01:01:01 +00:00
|
|
|
f = open(tmpsrc, 'wb')
|
|
|
|
try:
|
|
|
|
f.write(content)
|
2017-07-29 09:35:25 +00:00
|
|
|
except Exception as e:
|
2014-09-26 01:01:01 +00:00
|
|
|
os.remove(tmpsrc)
|
2017-07-29 09:35:25 +00:00
|
|
|
module.fail_json(msg="failed to create temporary content file: %s" % to_native(e),
|
|
|
|
exception=traceback.format_exc())
|
2014-09-26 01:01:01 +00:00
|
|
|
f.close()
|
2016-02-05 18:15:39 +00:00
|
|
|
|
2017-07-18 10:13:14 +00:00
|
|
|
checksum_src = None
|
|
|
|
checksum_dest = None
|
2016-02-05 18:15:39 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
# raise an error if there is no tmpsrc file
|
|
|
|
if not os.path.exists(tmpsrc):
|
|
|
|
os.remove(tmpsrc)
|
2017-07-18 10:13:14 +00:00
|
|
|
module.fail_json(msg="Source '%s' does not exist" % tmpsrc)
|
2014-09-26 01:01:01 +00:00
|
|
|
if not os.access(tmpsrc, os.R_OK):
|
|
|
|
os.remove(tmpsrc)
|
2017-07-18 10:13:14 +00:00
|
|
|
module.fail_json(msg="Source '%s' not readable" % tmpsrc)
|
2014-11-07 05:25:55 +00:00
|
|
|
checksum_src = module.sha1(tmpsrc)
|
2016-02-05 18:15:39 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
# check if there is no dest file
|
|
|
|
if os.path.exists(dest):
|
|
|
|
# raise an error if copy has no permission on dest
|
|
|
|
if not os.access(dest, os.W_OK):
|
|
|
|
os.remove(tmpsrc)
|
2017-07-18 10:13:14 +00:00
|
|
|
module.fail_json(msg="Destination '%s' not writable" % dest)
|
2014-09-26 01:01:01 +00:00
|
|
|
if not os.access(dest, os.R_OK):
|
|
|
|
os.remove(tmpsrc)
|
2017-07-18 10:13:14 +00:00
|
|
|
module.fail_json(msg="Destination '%s' not readable" % dest)
|
2014-11-07 05:25:55 +00:00
|
|
|
checksum_dest = module.sha1(dest)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
|
|
|
if not os.access(os.path.dirname(dest), os.W_OK):
|
|
|
|
os.remove(tmpsrc)
|
2017-07-18 10:13:14 +00:00
|
|
|
module.fail_json(msg="Destination dir '%s' not writable" % os.path.dirname(dest))
|
2014-11-07 05:25:55 +00:00
|
|
|
|
|
|
|
if checksum_src != checksum_dest:
|
2014-09-26 01:01:01 +00:00
|
|
|
try:
|
|
|
|
shutil.copyfile(tmpsrc, dest)
|
2017-07-29 09:35:25 +00:00
|
|
|
except Exception as e:
|
2014-09-26 01:01:01 +00:00
|
|
|
os.remove(tmpsrc)
|
2017-07-29 09:35:25 +00:00
|
|
|
module.fail_json(msg="failed to copy %s to %s: %s" % (tmpsrc, dest, to_native(e)),
|
|
|
|
exception=traceback.format_exc())
|
2014-11-07 05:25:55 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
os.remove(tmpsrc)
|
|
|
|
|
|
|
|
|
|
|
|
def url_filename(url):
|
2016-06-04 15:48:28 +00:00
|
|
|
fn = os.path.basename(six.moves.urllib.parse.urlsplit(url)[2])
|
2014-09-26 01:01:01 +00:00
|
|
|
if fn == '':
|
|
|
|
return 'index.html'
|
|
|
|
return fn
|
|
|
|
|
|
|
|
|
2016-02-05 18:15:39 +00:00
|
|
|
def absolute_location(url, location):
|
|
|
|
"""Attempts to create an absolute URL based on initial URL, and
|
|
|
|
next URL, specifically in the case of a ``Location`` header.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if '://' in location:
|
|
|
|
return location
|
|
|
|
|
|
|
|
elif location.startswith('/'):
|
2016-06-04 15:48:28 +00:00
|
|
|
parts = six.moves.urllib.parse.urlsplit(url)
|
2016-02-05 18:15:39 +00:00
|
|
|
base = url.replace(parts[2], '')
|
|
|
|
return '%s%s' % (base, location)
|
|
|
|
|
|
|
|
elif not location.startswith('/'):
|
|
|
|
base = os.path.dirname(url)
|
|
|
|
return '%s/%s' % (base, location)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
else:
|
2016-02-05 18:15:39 +00:00
|
|
|
return location
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-02-05 18:15:39 +00:00
|
|
|
def uri(module, url, dest, body, body_format, method, headers, socket_timeout):
|
2014-09-26 01:01:01 +00:00
|
|
|
# is dest is set and is a directory, let's check if we get redirected and
|
|
|
|
# set the filename from that url
|
|
|
|
redirected = False
|
2016-02-05 18:15:39 +00:00
|
|
|
redir_info = {}
|
2014-09-26 01:01:01 +00:00
|
|
|
r = {}
|
|
|
|
if dest is not None:
|
2016-03-07 21:34:22 +00:00
|
|
|
# Stash follow_redirects, in this block we don't want to follow
|
|
|
|
# we'll reset back to the supplied value soon
|
|
|
|
follow_redirects = module.params['follow_redirects']
|
|
|
|
module.params['follow_redirects'] = False
|
2014-09-26 01:01:01 +00:00
|
|
|
if os.path.isdir(dest):
|
|
|
|
# first check if we are redirected to a file download
|
2016-02-05 18:15:39 +00:00
|
|
|
_, redir_info = fetch_url(module, url, data=body,
|
|
|
|
headers=headers,
|
2016-03-07 21:34:22 +00:00
|
|
|
method=method,
|
2016-02-05 18:15:39 +00:00
|
|
|
timeout=socket_timeout)
|
|
|
|
# if we are redirected, update the url with the location header,
|
|
|
|
# and update dest with the new url filename
|
|
|
|
if redir_info['status'] in (301, 302, 303, 307):
|
|
|
|
url = redir_info['location']
|
2014-09-26 01:01:01 +00:00
|
|
|
redirected = True
|
|
|
|
dest = os.path.join(dest, url_filename(url))
|
|
|
|
# if destination file already exist, only download if file newer
|
|
|
|
if os.path.exists(dest):
|
|
|
|
t = datetime.datetime.utcfromtimestamp(os.path.getmtime(dest))
|
|
|
|
tstamp = t.strftime('%a, %d %b %Y %H:%M:%S +0000')
|
|
|
|
headers['If-Modified-Since'] = tstamp
|
|
|
|
|
2016-03-07 21:34:22 +00:00
|
|
|
# Reset follow_redirects back to the stashed value
|
|
|
|
module.params['follow_redirects'] = follow_redirects
|
|
|
|
|
2016-02-05 18:15:39 +00:00
|
|
|
resp, info = fetch_url(module, url, data=body, headers=headers,
|
|
|
|
method=method, timeout=socket_timeout)
|
2016-04-25 18:19:03 +00:00
|
|
|
|
2016-02-05 18:15:39 +00:00
|
|
|
try:
|
|
|
|
content = resp.read()
|
|
|
|
except AttributeError:
|
2016-04-25 18:19:03 +00:00
|
|
|
# there was no content, but the error read()
|
|
|
|
# may have been stored in the info as 'body'
|
|
|
|
content = info.pop('body', '')
|
|
|
|
|
|
|
|
r['redirected'] = redirected or info['url'] != url
|
|
|
|
r.update(redir_info)
|
|
|
|
r.update(info)
|
|
|
|
|
2016-02-05 18:15:39 +00:00
|
|
|
return r, content, dest
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
def main():
|
2016-02-05 18:15:39 +00:00
|
|
|
argument_spec = url_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
2017-07-18 10:13:14 +00:00
|
|
|
dest=dict(type='path'),
|
|
|
|
url_username=dict(type='str', aliases=['user']),
|
|
|
|
url_password=dict(type='str', aliases=['password'], no_log=True),
|
|
|
|
body=dict(type='raw'),
|
|
|
|
body_format=dict(type='str', default='raw', choices=['raw', 'json']),
|
|
|
|
method=dict(type='str', default='GET', choices=['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'PATCH', 'TRACE', 'CONNECT', 'REFRESH']),
|
|
|
|
return_content=dict(type='bool', default='no'),
|
|
|
|
follow_redirects=dict(type='str', default='safe', choices=['all', 'safe', 'none', 'yes', 'no']),
|
|
|
|
creates=dict(type='path'),
|
|
|
|
removes=dict(type='path'),
|
|
|
|
status_code=dict(type='list', default=[200]),
|
|
|
|
timeout=dict(type='int', default=30),
|
|
|
|
headers=dict(type='dict', default={})
|
2016-02-05 18:15:39 +00:00
|
|
|
))
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
module = AnsibleModule(
|
2016-02-05 18:15:39 +00:00
|
|
|
argument_spec=argument_spec,
|
2017-12-19 22:36:02 +00:00
|
|
|
# TODO: Remove check_invalid_arguments in 2.9
|
2014-09-26 01:01:01 +00:00
|
|
|
check_invalid_arguments=False,
|
|
|
|
add_file_common_args=True
|
|
|
|
)
|
|
|
|
|
2017-07-18 10:13:14 +00:00
|
|
|
url = module.params['url']
|
2014-09-26 01:01:01 +00:00
|
|
|
body = module.params['body']
|
2016-02-11 06:09:20 +00:00
|
|
|
body_format = module.params['body_format'].lower()
|
2014-09-26 01:01:01 +00:00
|
|
|
method = module.params['method']
|
|
|
|
dest = module.params['dest']
|
|
|
|
return_content = module.params['return_content']
|
|
|
|
creates = module.params['creates']
|
|
|
|
removes = module.params['removes']
|
|
|
|
status_code = [int(x) for x in list(module.params['status_code'])]
|
|
|
|
socket_timeout = module.params['timeout']
|
|
|
|
|
2016-02-05 18:15:39 +00:00
|
|
|
dict_headers = module.params['headers']
|
2015-03-27 10:11:25 +00:00
|
|
|
|
|
|
|
if body_format == 'json':
|
2016-10-13 14:47:50 +00:00
|
|
|
# Encode the body unless its a string, then assume it is pre-formatted JSON
|
2017-01-13 19:16:21 +00:00
|
|
|
if not isinstance(body, six.string_types):
|
2016-02-11 06:09:20 +00:00
|
|
|
body = json.dumps(body)
|
2017-01-13 18:19:38 +00:00
|
|
|
lower_header_keys = [key.lower() for key in dict_headers]
|
|
|
|
if 'content-type' not in lower_header_keys:
|
2017-01-30 23:01:47 +00:00
|
|
|
dict_headers['Content-Type'] = 'application/json'
|
2015-03-27 10:11:25 +00:00
|
|
|
|
2017-12-19 22:36:02 +00:00
|
|
|
# TODO: Deprecated section. Remove in Ansible 2.9
|
2016-02-05 18:15:39 +00:00
|
|
|
# Grab all the http headers. Need this hack since passing multi-values is
|
|
|
|
# currently a bit ugly. (e.g. headers='{"Content-Type":"application/json"}')
|
2016-08-31 15:16:01 +00:00
|
|
|
for key, value in six.iteritems(module.params):
|
2014-09-26 01:01:01 +00:00
|
|
|
if key.startswith("HEADER_"):
|
2017-12-19 22:36:02 +00:00
|
|
|
module.deprecate('Supplying headers via HEADER_* is deprecated. Please use `headers` to'
|
|
|
|
' supply headers for the request', version='2.9')
|
2014-09-26 01:01:01 +00:00
|
|
|
skey = key.replace("HEADER_", "")
|
|
|
|
dict_headers[skey] = value
|
2017-12-19 22:36:02 +00:00
|
|
|
# End deprecated section
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if creates is not None:
|
|
|
|
# do not run the command if the line contains creates=filename
|
|
|
|
# and the filename already exists. This allows idempotence
|
|
|
|
# of uri executions.
|
|
|
|
if os.path.exists(creates):
|
2017-07-18 10:13:14 +00:00
|
|
|
module.exit_json(stdout="skipped, since '%s' exists" % creates, changed=False, rc=0)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if removes is not None:
|
|
|
|
# do not run the command if the line contains removes=filename
|
|
|
|
# and the filename do not exists. This allows idempotence
|
|
|
|
# of uri executions.
|
|
|
|
if not os.path.exists(removes):
|
2017-07-18 10:13:14 +00:00
|
|
|
module.exit_json(stdout="skipped, since '%s' does not exist" % removes, changed=False, rc=0)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Make the request
|
2016-02-05 18:15:39 +00:00
|
|
|
resp, content, dest = uri(module, url, dest, body, body_format, method,
|
|
|
|
dict_headers, socket_timeout)
|
2014-09-26 01:01:01 +00:00
|
|
|
resp['status'] = int(resp['status'])
|
|
|
|
|
|
|
|
# Write the file out if requested
|
|
|
|
if dest is not None:
|
|
|
|
if resp['status'] == 304:
|
|
|
|
changed = False
|
|
|
|
else:
|
|
|
|
write_file(module, url, dest, content)
|
|
|
|
# allow file attribute changes
|
|
|
|
changed = True
|
|
|
|
module.params['path'] = dest
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
file_args['path'] = dest
|
|
|
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
|
|
|
resp['path'] = dest
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
|
2017-06-12 06:55:19 +00:00
|
|
|
# Transmogrify the headers, replacing '-' with '_', since variables don't
|
2016-02-05 18:15:39 +00:00
|
|
|
# work with dashes.
|
2016-10-24 14:01:00 +00:00
|
|
|
# In python3, the headers are title cased. Lowercase them to be
|
|
|
|
# compatible with the python2 behaviour.
|
2014-09-26 01:01:01 +00:00
|
|
|
uresp = {}
|
2016-08-31 15:16:01 +00:00
|
|
|
for key, value in six.iteritems(resp):
|
2016-10-24 14:01:00 +00:00
|
|
|
ukey = key.replace("-", "_").lower()
|
2014-09-26 01:01:01 +00:00
|
|
|
uresp[ukey] = value
|
|
|
|
|
2016-02-05 18:15:39 +00:00
|
|
|
try:
|
|
|
|
uresp['location'] = absolute_location(url, uresp['location'])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2015-05-14 21:19:15 +00:00
|
|
|
# Default content_encoding to try
|
|
|
|
content_encoding = 'utf-8'
|
2016-10-24 14:01:00 +00:00
|
|
|
if 'content_type' in uresp:
|
|
|
|
content_type, params = cgi.parse_header(uresp['content_type'])
|
2015-05-14 21:19:15 +00:00
|
|
|
if 'charset' in params:
|
|
|
|
content_encoding = params['charset']
|
2016-10-24 05:17:06 +00:00
|
|
|
u_content = to_text(content, encoding=content_encoding)
|
2017-12-14 21:54:59 +00:00
|
|
|
if any(candidate in content_type for candidate in JSON_CANDIDATES):
|
2014-09-26 01:01:01 +00:00
|
|
|
try:
|
2015-05-14 21:19:15 +00:00
|
|
|
js = json.loads(u_content)
|
2014-09-26 01:01:01 +00:00
|
|
|
uresp['json'] = js
|
|
|
|
except:
|
|
|
|
pass
|
2015-05-14 21:19:15 +00:00
|
|
|
else:
|
2016-10-24 05:17:06 +00:00
|
|
|
u_content = to_text(content, encoding=content_encoding)
|
2015-05-14 21:19:15 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
if resp['status'] not in status_code:
|
2017-12-20 04:33:11 +00:00
|
|
|
uresp['msg'] = 'Status code was %s and not %s: %s' % (resp['status'], status_code, uresp.get('msg', ''))
|
2016-02-05 18:15:39 +00:00
|
|
|
module.fail_json(content=u_content, **uresp)
|
2014-09-26 01:01:01 +00:00
|
|
|
elif return_content:
|
2015-05-14 21:19:15 +00:00
|
|
|
module.exit_json(changed=changed, content=u_content, **uresp)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
|
|
|
module.exit_json(changed=changed, **uresp)
|
|
|
|
|
|
|
|
|
2015-05-14 21:19:15 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|