2013-02-15 23:32:31 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Romeo Theriault <romeot () hawaii.edu>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# see examples/playbooks/uri.yml
|
|
|
|
|
2015-05-14 23:19:15 +02:00
|
|
|
import cgi
|
2013-02-15 23:32:31 +01:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2013-04-10 00:14:57 +02:00
|
|
|
import datetime
|
2015-10-28 16:50:47 +01:00
|
|
|
|
2013-02-15 23:32:31 +01:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: uri
|
|
|
|
short_description: Interacts with webservices
|
|
|
|
description:
|
2013-03-30 20:53:28 +01:00
|
|
|
- Interacts with HTTP and HTTPS web services and supports Digest, Basic and WSSE
|
|
|
|
HTTP authentication mechanisms.
|
2013-02-15 23:32:31 +01:00
|
|
|
version_added: "1.1"
|
|
|
|
options:
|
|
|
|
url:
|
|
|
|
description:
|
|
|
|
- HTTP or HTTPS URL in the form (http|https)://host.domain[:port]/path
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
dest:
|
|
|
|
description:
|
2016-02-05 19:15:39 +01: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.
|
2013-02-15 23:32:31 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- username for the module to use for Digest, Basic or WSSE authentication.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- password for the module to use for Digest, Basic or WSSE authentication.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
body:
|
|
|
|
description:
|
2016-02-05 19:15:39 +01:00
|
|
|
- The body of the http request/response to the web service. If C(body_format) is set
|
|
|
|
to 'json' it will take an already formated JSON string or convert a data structure
|
|
|
|
into JSON.
|
2013-02-15 23:32:31 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2015-03-27 11:11:25 +01:00
|
|
|
body_format:
|
|
|
|
description:
|
2016-02-05 19:15:39 +01: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.
|
2015-03-27 11:11:25 +01:00
|
|
|
required: false
|
2015-05-12 13:29:00 +02:00
|
|
|
choices: [ "raw", "json" ]
|
2015-03-27 11:11:25 +01:00
|
|
|
default: raw
|
2015-07-23 21:52:11 +02:00
|
|
|
version_added: "2.0"
|
2013-02-15 23:32:31 +01:00
|
|
|
method:
|
|
|
|
description:
|
2015-10-19 16:13:45 +02:00
|
|
|
- The HTTP method of the request or response. It MUST be uppercase.
|
2013-02-15 23:32:31 +01:00
|
|
|
required: false
|
2015-07-09 11:39:46 +02:00
|
|
|
choices: [ "GET", "POST", "PUT", "HEAD", "DELETE", "OPTIONS", "PATCH", "TRACE", "CONNECT", "REFRESH" ]
|
2013-02-15 23:32:31 +01:00
|
|
|
default: "GET"
|
|
|
|
return_content:
|
|
|
|
description:
|
2016-02-05 19:15:39 +01:00
|
|
|
- Whether or not to return the body of the request as a "content" key in
|
|
|
|
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.
|
2013-02-15 23:32:31 +01:00
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
2013-02-19 00:03:19 +01:00
|
|
|
default: "no"
|
2013-02-15 23:32:31 +01:00
|
|
|
force_basic_auth:
|
|
|
|
description:
|
2016-02-05 19:15:39 +01:00
|
|
|
- The library used by the uri module only sends authentication information when a webservice
|
2013-02-15 23:32:31 +01: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.
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
2013-02-19 00:03:19 +01:00
|
|
|
default: "no"
|
2013-02-15 23:32:31 +01:00
|
|
|
follow_redirects:
|
|
|
|
description:
|
2014-01-30 01:04:53 +01:00
|
|
|
- Whether or not the URI module should follow redirects. C(all) will follow all redirects.
|
2016-02-05 19:15:39 +01:00
|
|
|
C(safe) will follow only "safe" redirects, where "safe" means that the client is only
|
2014-01-30 01:04:53 +01:00
|
|
|
doing a GET or HEAD on the URI to which it is being redirected. C(none) will not follow
|
2016-02-05 19:15:39 +01:00
|
|
|
any redirects. Note that C(yes) and C(no) choices are accepted for backwards compatibility,
|
2014-01-30 01:04:53 +01: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.
|
2013-02-15 23:32:31 +01:00
|
|
|
required: false
|
2014-01-30 01:04:53 +01:00
|
|
|
choices: [ "all", "safe", "none" ]
|
|
|
|
default: "safe"
|
2013-02-15 23:32:31 +01:00
|
|
|
creates:
|
|
|
|
description:
|
|
|
|
- a filename, when it already exists, this step will not be run.
|
|
|
|
required: false
|
|
|
|
removes:
|
|
|
|
description:
|
|
|
|
- a filename, when it does not exist, this step will not be run.
|
|
|
|
required: false
|
|
|
|
status_code:
|
|
|
|
description:
|
2016-02-05 19:15:39 +01:00
|
|
|
- A valid, numeric, HTTP status code that signifies success of the
|
|
|
|
request. Can also be comma separated list of status codes.
|
2013-02-15 23:32:31 +01:00
|
|
|
required: false
|
|
|
|
default: 200
|
2013-03-03 02:27:09 +01:00
|
|
|
timeout:
|
|
|
|
description:
|
2016-02-05 19:15:39 +01:00
|
|
|
- The socket level timeout in seconds
|
2013-03-03 02:27:09 +01:00
|
|
|
required: false
|
2013-03-18 04:07:15 +01:00
|
|
|
default: 30
|
2013-02-15 23:32:31 +01:00
|
|
|
HEADER_:
|
2013-03-30 20:53:28 +01:00
|
|
|
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".
|
2016-02-05 19:15:39 +01:00
|
|
|
This option is deprecated as of C(2.1) and may be removed in a future
|
|
|
|
release. Use I(headers) instead.
|
2013-02-15 23:32:31 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2016-02-05 19:15:39 +01:00
|
|
|
headers:
|
|
|
|
description:
|
|
|
|
- Add custom HTTP headers to a request in the format of a YAML hash
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: '2.1'
|
2013-02-15 23:32:31 +01:00
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- all arguments accepted by the M(file) module also work here
|
|
|
|
required: false
|
2015-05-29 00:02:28 +02: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).
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
|
|
|
version_added: '1.9.2'
|
2016-02-23 18:53:02 +01:00
|
|
|
notes:
|
|
|
|
- The dependency on httplib2 was removed in Ansible 2.1
|
2015-06-15 21:53:30 +02:00
|
|
|
author: "Romeo Theriault (@romeotheriault)"
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
2013-02-19 00:03:19 +01:00
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Check that you can connect (GET) to a page and it returns a status 200
|
|
|
|
- uri: url=http://www.example.com
|
2013-02-19 00:03:19 +01:00
|
|
|
|
2016-02-05 19:15:39 +01:00
|
|
|
# Check that a page returns a status 200 and fail if the word AWESOME is not
|
|
|
|
# in the page contents.
|
2013-06-14 11:53:43 +02:00
|
|
|
- action: uri url=http://www.example.com return_content=yes
|
|
|
|
register: webpage
|
2013-02-19 00:03:19 +01:00
|
|
|
|
2013-06-17 08:53:46 +02:00
|
|
|
- action: fail
|
2015-12-02 21:01:16 +01:00
|
|
|
when: "'AWESOME' not in webpage.content"
|
2013-02-19 00:03:19 +01:00
|
|
|
|
|
|
|
|
2014-04-02 23:32:44 +02:00
|
|
|
# Create a JIRA issue
|
2015-05-28 07:27:46 +02:00
|
|
|
- uri:
|
2016-02-05 19:15:39 +01:00
|
|
|
url: https://your.jira.example.com/rest/api/2/issue/
|
2015-05-28 07:27:46 +02:00
|
|
|
method: POST
|
2016-02-05 19:15:39 +01:00
|
|
|
user: your_username
|
|
|
|
password: your_pass
|
2015-05-28 07:27:46 +02:00
|
|
|
body: "{{ lookup('file','issue.json') }}"
|
2016-02-05 19:15:39 +01:00
|
|
|
force_basic_auth: yes
|
2015-05-28 07:27:46 +02:00
|
|
|
status_code: 201
|
2016-02-05 19:15:39 +01:00
|
|
|
body_format: json
|
2013-06-14 11:53:43 +02:00
|
|
|
|
|
|
|
# Login to a form based webpage, then use the returned cookie to
|
2014-04-02 23:32:44 +02:00
|
|
|
# access the app in later tasks
|
2015-05-29 00:02:28 +02:00
|
|
|
|
2015-05-28 07:27:46 +02:00
|
|
|
- uri:
|
2016-02-28 11:51:17 +01:00
|
|
|
url: https://your.form.based.auth.example.com/index.php
|
2015-05-28 07:27:46 +02:00
|
|
|
method: POST
|
2016-02-05 19:15:39 +01:00
|
|
|
body: "name=your_username&password=your_password&enter=Sign%20in"
|
2015-05-28 07:27:46 +02:00
|
|
|
status_code: 302
|
|
|
|
HEADER_Content-Type: "application/x-www-form-urlencoded"
|
2015-05-29 00:02:28 +02:00
|
|
|
register: login
|
2015-05-28 07:27:46 +02:00
|
|
|
|
|
|
|
- uri:
|
|
|
|
url: https://your.form.based.auth.example.com/dashboard.php
|
|
|
|
method: GET
|
|
|
|
return_content: yes
|
|
|
|
HEADER_Cookie: "{{login.set_cookie}}"
|
2015-05-29 00:02:28 +02:00
|
|
|
|
2014-03-31 14:32:07 +02:00
|
|
|
# Queue build of a project in Jenkins:
|
2015-05-28 07:27:46 +02:00
|
|
|
- uri:
|
2016-02-05 19:15:39 +01:00
|
|
|
url: "http://{{ jenkins.host }}/job/{{ jenkins.job }}/build?token={{ jenkins.token }}"
|
2015-05-28 07:27:46 +02:00
|
|
|
method: GET
|
|
|
|
user: "{{ jenkins.user }}"
|
|
|
|
password: "{{ jenkins.password }}"
|
|
|
|
force_basic_auth: yes
|
|
|
|
status_code: 201
|
2014-04-02 23:32:44 +02:00
|
|
|
|
2013-02-15 23:32:31 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
def write_file(module, url, dest, content):
|
|
|
|
# create a tempfile with some test content
|
|
|
|
fd, tmpsrc = tempfile.mkstemp()
|
|
|
|
f = open(tmpsrc, 'wb')
|
|
|
|
try:
|
|
|
|
f.write(content)
|
|
|
|
except Exception, err:
|
|
|
|
os.remove(tmpsrc)
|
|
|
|
module.fail_json(msg="failed to create temporary content file: %s" % str(err))
|
|
|
|
f.close()
|
2016-02-05 19:15:39 +01:00
|
|
|
|
2014-11-07 06:25:55 +01:00
|
|
|
checksum_src = None
|
|
|
|
checksum_dest = None
|
2016-02-05 19:15:39 +01:00
|
|
|
|
2013-02-15 23:32:31 +01:00
|
|
|
# raise an error if there is no tmpsrc file
|
|
|
|
if not os.path.exists(tmpsrc):
|
|
|
|
os.remove(tmpsrc)
|
|
|
|
module.fail_json(msg="Source %s does not exist" % (tmpsrc))
|
|
|
|
if not os.access(tmpsrc, os.R_OK):
|
|
|
|
os.remove(tmpsrc)
|
|
|
|
module.fail_json( msg="Source %s not readable" % (tmpsrc))
|
2014-11-07 06:25:55 +01:00
|
|
|
checksum_src = module.sha1(tmpsrc)
|
2016-02-05 19:15:39 +01:00
|
|
|
|
2013-02-15 23:32:31 +01: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)
|
2016-02-05 19:15:39 +01:00
|
|
|
module.fail_json(msg="Destination %s not writable" % (dest))
|
2013-02-15 23:32:31 +01:00
|
|
|
if not os.access(dest, os.R_OK):
|
|
|
|
os.remove(tmpsrc)
|
2016-02-05 19:15:39 +01:00
|
|
|
module.fail_json(msg="Destination %s not readable" % (dest))
|
2014-11-07 06:25:55 +01:00
|
|
|
checksum_dest = module.sha1(dest)
|
2013-02-15 23:32:31 +01:00
|
|
|
else:
|
|
|
|
if not os.access(os.path.dirname(dest), os.W_OK):
|
|
|
|
os.remove(tmpsrc)
|
2016-02-05 19:15:39 +01:00
|
|
|
module.fail_json(msg="Destination dir %s not writable" % (os.path.dirname(dest)))
|
2014-11-07 06:25:55 +01:00
|
|
|
|
|
|
|
if checksum_src != checksum_dest:
|
2013-02-15 23:32:31 +01:00
|
|
|
try:
|
|
|
|
shutil.copyfile(tmpsrc, dest)
|
|
|
|
except Exception, err:
|
|
|
|
os.remove(tmpsrc)
|
|
|
|
module.fail_json(msg="failed to copy %s to %s: %s" % (tmpsrc, dest, str(err)))
|
2014-11-07 06:25:55 +01:00
|
|
|
|
2013-02-15 23:32:31 +01:00
|
|
|
os.remove(tmpsrc)
|
|
|
|
|
|
|
|
|
|
|
|
def url_filename(url):
|
|
|
|
fn = os.path.basename(urlparse.urlsplit(url)[2])
|
|
|
|
if fn == '':
|
|
|
|
return 'index.html'
|
|
|
|
return fn
|
|
|
|
|
|
|
|
|
2016-02-05 19:15:39 +01: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('/'):
|
|
|
|
parts = urlparse.urlsplit(url)
|
|
|
|
base = url.replace(parts[2], '')
|
|
|
|
return '%s%s' % (base, location)
|
|
|
|
|
|
|
|
elif not location.startswith('/'):
|
|
|
|
base = os.path.dirname(url)
|
|
|
|
return '%s/%s' % (base, location)
|
2013-02-15 23:32:31 +01:00
|
|
|
|
2014-01-30 01:04:53 +01:00
|
|
|
else:
|
2016-02-05 19:15:39 +01:00
|
|
|
return location
|
|
|
|
|
2013-04-10 00:14:57 +02:00
|
|
|
|
2016-02-05 19:15:39 +01:00
|
|
|
def uri(module, url, dest, body, body_format, method, headers, socket_timeout):
|
2013-04-10 00:14:57 +02: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 19:15:39 +01:00
|
|
|
redir_info = {}
|
2013-04-10 00:14:57 +02:00
|
|
|
r = {}
|
|
|
|
if dest is not None:
|
2016-03-07 22:34:22 +01: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
|
2013-04-10 00:14:57 +02:00
|
|
|
dest = os.path.expanduser(dest)
|
|
|
|
if os.path.isdir(dest):
|
|
|
|
# first check if we are redirected to a file download
|
2016-02-05 19:15:39 +01:00
|
|
|
_, redir_info = fetch_url(module, url, data=body,
|
|
|
|
headers=headers,
|
2016-03-07 22:34:22 +01:00
|
|
|
method=method,
|
2016-02-05 19:15:39 +01: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']
|
2013-04-10 00:14:57 +02: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 22:34:22 +01:00
|
|
|
# Reset follow_redirects back to the stashed value
|
|
|
|
module.params['follow_redirects'] = follow_redirects
|
|
|
|
|
2016-02-05 19:15:39 +01:00
|
|
|
resp, info = fetch_url(module, url, data=body, headers=headers,
|
|
|
|
method=method, timeout=socket_timeout)
|
|
|
|
r['redirected'] = redirected or info['url'] != url
|
|
|
|
r.update(redir_info)
|
|
|
|
r.update(info)
|
|
|
|
try:
|
|
|
|
content = resp.read()
|
|
|
|
except AttributeError:
|
|
|
|
content = ''
|
|
|
|
return r, content, dest
|
|
|
|
|
2013-02-15 23:32:31 +01:00
|
|
|
|
|
|
|
def main():
|
2016-02-05 19:15:39 +01:00
|
|
|
argument_spec = url_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
|
|
|
dest = dict(required=False, default=None, type='path'),
|
|
|
|
url_username = dict(required=False, default=None, aliases=['user']),
|
|
|
|
url_password = dict(required=False, default=None, aliases=['password']),
|
2016-02-29 16:20:39 +01:00
|
|
|
body = dict(required=False, default=None, type='raw'),
|
2016-02-05 19:15:39 +01:00
|
|
|
body_format = dict(required=False, default='raw', choices=['raw', 'json']),
|
|
|
|
method = dict(required=False, default='GET', choices=['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'PATCH', 'TRACE', 'CONNECT', 'REFRESH']),
|
|
|
|
return_content = dict(required=False, default='no', type='bool'),
|
|
|
|
follow_redirects = dict(required=False, default='safe', choices=['all', 'safe', 'none', 'yes', 'no']),
|
|
|
|
creates = dict(required=False, default=None, type='path'),
|
|
|
|
removes = dict(required=False, default=None, type='path'),
|
|
|
|
status_code = dict(required=False, default=[200], type='list'),
|
|
|
|
timeout = dict(required=False, default=30, type='int'),
|
|
|
|
headers = dict(required=False, type='dict')
|
|
|
|
))
|
2013-02-15 23:32:31 +01:00
|
|
|
|
|
|
|
module = AnsibleModule(
|
2016-02-05 19:15:39 +01:00
|
|
|
argument_spec=argument_spec,
|
2013-02-15 23:32:31 +01:00
|
|
|
check_invalid_arguments=False,
|
|
|
|
add_file_common_args=True
|
|
|
|
)
|
|
|
|
|
|
|
|
url = module.params['url']
|
|
|
|
body = module.params['body']
|
2016-02-11 07:09:20 +01:00
|
|
|
body_format = module.params['body_format'].lower()
|
2013-02-15 23:32:31 +01:00
|
|
|
method = module.params['method']
|
|
|
|
dest = module.params['dest']
|
2013-02-23 19:59:52 +01:00
|
|
|
return_content = module.params['return_content']
|
2013-02-15 23:32:31 +01:00
|
|
|
creates = module.params['creates']
|
|
|
|
removes = module.params['removes']
|
2014-03-24 15:23:18 +01:00
|
|
|
status_code = [int(x) for x in list(module.params['status_code'])]
|
2013-03-03 02:27:09 +01:00
|
|
|
socket_timeout = module.params['timeout']
|
2013-02-15 23:32:31 +01:00
|
|
|
|
2016-02-05 19:15:39 +01:00
|
|
|
dict_headers = module.params['headers']
|
2015-03-27 11:11:25 +01:00
|
|
|
|
|
|
|
if body_format == 'json':
|
2016-02-11 07:09:20 +01:00
|
|
|
# Encode the body unless its a string, then assume it is preformatted JSON
|
|
|
|
if not isinstance(body, basestring):
|
|
|
|
body = json.dumps(body)
|
2015-03-27 11:11:25 +01:00
|
|
|
dict_headers['Content-Type'] = 'application/json'
|
|
|
|
|
2016-02-05 19:15:39 +01: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"}')
|
2013-02-15 23:32:31 +01:00
|
|
|
for key, value in module.params.iteritems():
|
|
|
|
if key.startswith("HEADER_"):
|
|
|
|
skey = key.replace("HEADER_", "")
|
|
|
|
dict_headers[skey] = value
|
|
|
|
|
|
|
|
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):
|
2016-02-05 19:15:39 +01:00
|
|
|
module.exit_json(stdout="skipped, since %s exists" % creates,
|
|
|
|
changed=False, stderr=False, rc=0)
|
2013-02-15 23:32:31 +01: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):
|
2015-02-17 22:19:22 +01:00
|
|
|
module.exit_json(stdout="skipped, since %s does not exist" % removes, changed=False, stderr=False, rc=0)
|
2013-02-15 23:32:31 +01:00
|
|
|
|
|
|
|
# Make the request
|
2016-02-05 19:15:39 +01:00
|
|
|
resp, content, dest = uri(module, url, dest, body, body_format, method,
|
|
|
|
dict_headers, socket_timeout)
|
2013-10-12 22:10:40 +02:00
|
|
|
resp['status'] = int(resp['status'])
|
2013-02-15 23:32:31 +01:00
|
|
|
|
|
|
|
# Write the file out if requested
|
|
|
|
if dest is not None:
|
2013-10-12 22:10:40 +02:00
|
|
|
if resp['status'] == 304:
|
2013-04-10 00:14:57 +02:00
|
|
|
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
|
2014-03-19 03:39:45 +01:00
|
|
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
2013-04-10 00:14:57 +02:00
|
|
|
resp['path'] = dest
|
2013-04-29 20:59:06 +02:00
|
|
|
else:
|
|
|
|
changed = False
|
2013-02-15 23:32:31 +01:00
|
|
|
|
2016-02-05 19:15:39 +01:00
|
|
|
# Transmogrify the headers, replacing '-' with '_', since variables dont
|
|
|
|
# work with dashes.
|
2013-02-15 23:32:31 +01:00
|
|
|
uresp = {}
|
|
|
|
for key, value in resp.iteritems():
|
2015-03-27 11:11:25 +01:00
|
|
|
ukey = key.replace("-", "_")
|
2013-02-15 23:32:31 +01:00
|
|
|
uresp[ukey] = value
|
|
|
|
|
2016-02-05 19:15:39 +01:00
|
|
|
try:
|
|
|
|
uresp['location'] = absolute_location(url, uresp['location'])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2015-05-14 23:19:15 +02:00
|
|
|
# Default content_encoding to try
|
|
|
|
content_encoding = 'utf-8'
|
2013-02-18 14:43:02 +01:00
|
|
|
if 'content_type' in uresp:
|
2015-05-14 23:19:15 +02:00
|
|
|
content_type, params = cgi.parse_header(uresp['content_type'])
|
|
|
|
if 'charset' in params:
|
|
|
|
content_encoding = params['charset']
|
2015-10-19 19:15:13 +02:00
|
|
|
u_content = unicode(content, content_encoding, errors='replace')
|
2015-05-14 23:19:15 +02:00
|
|
|
if content_type.startswith('application/json') or \
|
|
|
|
content_type.startswith('text/json'):
|
2013-02-18 14:43:02 +01:00
|
|
|
try:
|
2015-05-14 23:19:15 +02:00
|
|
|
js = json.loads(u_content)
|
2013-02-18 14:43:02 +01:00
|
|
|
uresp['json'] = js
|
|
|
|
except:
|
|
|
|
pass
|
2015-05-14 23:19:15 +02:00
|
|
|
else:
|
2015-10-19 19:15:13 +02:00
|
|
|
u_content = unicode(content, content_encoding, errors='replace')
|
2015-05-14 23:19:15 +02:00
|
|
|
|
2014-03-24 15:23:18 +01:00
|
|
|
if resp['status'] not in status_code:
|
2016-02-05 19:15:39 +01:00
|
|
|
uresp['msg'] = 'Status code was not %s: %s' % (status_code, uresp.get('msg', ''))
|
|
|
|
module.fail_json(content=u_content, **uresp)
|
2013-02-15 23:32:31 +01:00
|
|
|
elif return_content:
|
2015-05-14 23:19:15 +02:00
|
|
|
module.exit_json(changed=changed, content=u_content, **uresp)
|
2013-02-15 23:32:31 +01:00
|
|
|
else:
|
2013-04-10 00:14:57 +02:00
|
|
|
module.exit_json(changed=changed, **uresp)
|
2013-02-15 23:32:31 +01:00
|
|
|
|
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2016-02-05 19:15:39 +01:00
|
|
|
from ansible.module_utils.urls import *
|
|
|
|
|
2015-05-14 23:19:15 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|