mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 14:22:46 +00:00
Allow modules to be categorized, and also sort them when generating the documentation.
This commit is contained in:
42
library/network/fetch
Normal file
42
library/network/fetch
Normal file
@@ -0,0 +1,42 @@
|
||||
# this is a virtual module that is entirely implemented server side
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: fetch
|
||||
short_description: Fetches a file from remote nodes
|
||||
description:
|
||||
- This module works like M(copy), but in reverse. It is used for fetching
|
||||
files from remote machines and storing them locally in a file tree,
|
||||
organized by hostname. Note that this module is written to transfer
|
||||
log files that might not be present, so a missing remote file won't
|
||||
be an error unless fail_on_missing is set to 'yes'.
|
||||
version_added: "0.2"
|
||||
options:
|
||||
src:
|
||||
description:
|
||||
- The file on the remote system to fetch. This I(must) be a file, not a
|
||||
directory. Recursive fetching may be supported in a later release.
|
||||
required: true
|
||||
default: null
|
||||
aliases: []
|
||||
dest:
|
||||
description:
|
||||
- A directory to save the file into. For example, if the I(dest)
|
||||
directory is C(/backup) a I(src) file named C(/etc/profile) on host
|
||||
C(host.example.com), would be saved into
|
||||
C(/backup/host.example.com/etc/profile)
|
||||
required: true
|
||||
default: null
|
||||
fail_on_missing:
|
||||
version_added: "1.1"
|
||||
description:
|
||||
- Makes it fails when the source file is missing.
|
||||
required: false
|
||||
choices: [ "yes", "no" ]
|
||||
default: "no"
|
||||
examples:
|
||||
- code: "fetch: src=/var/log/messages dest=/home/logtree"
|
||||
description: "Example from Ansible Playbooks"
|
||||
requirements: []
|
||||
author: Michael DeHaan
|
||||
'''
|
||||
290
library/network/get_url
Normal file
290
library/network/get_url
Normal file
@@ -0,0 +1,290 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# (c) 2012, Jan-Piet Mens <jpmens () gmail.com>
|
||||
#
|
||||
# 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/get_url.yml
|
||||
|
||||
import shutil
|
||||
import datetime
|
||||
import tempfile
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: get_url
|
||||
short_description: Downloads files from HTTP, HTTPS, or FTP to node
|
||||
description:
|
||||
- Downloads files from HTTP, HTTPS, or FTP to the remote server. The remote
|
||||
server I(must) have direct access to the remote resource.
|
||||
- By default, if an environment variable C(<protocol>_proxy) is set on
|
||||
the target host, requests will be sent through that proxy. This
|
||||
behaviour can be overriden by setting a variable for this task
|
||||
(see `setting the environment
|
||||
<http://ansible.cc/docs/playbooks2.html#setting-the-environment-and-working-with-proxies>`_),
|
||||
or by using the use_proxy option.
|
||||
version_added: "0.6"
|
||||
options:
|
||||
url:
|
||||
description:
|
||||
- HTTP, HTTPS, or FTP URL in the form (http|https|ftp)://[user[:pass]]@host.domain[:port]/path
|
||||
required: true
|
||||
default: null
|
||||
aliases: []
|
||||
dest:
|
||||
description:
|
||||
- absolute path of where to download the file to.
|
||||
- If I(dest) is a directory, the basename of the file on the remote server will be used. If a directory, C(force=yes) must also be set.
|
||||
required: true
|
||||
default: null
|
||||
force:
|
||||
description:
|
||||
- if C(yes), will download the file every time and replace the
|
||||
file if the contents change. If C(no), the file will only be downloaded if
|
||||
the destination does not exist. Generally should be C(yes) only for small
|
||||
local files. prior to 0.6, acts if C(yes) by default.
|
||||
version_added: "0.7"
|
||||
required: false
|
||||
choices: [ "yes", "no" ]
|
||||
default: "no"
|
||||
aliases: [ "thirsty" ]
|
||||
use_proxy:
|
||||
description:
|
||||
- if C(no), it will not use a proxy, even if one is defined by
|
||||
in an environment variable on the target hosts.
|
||||
required: false
|
||||
default: yes
|
||||
choices: [yes, no]
|
||||
others:
|
||||
description:
|
||||
- all arguments accepted by the M(file) module also work here
|
||||
required: false
|
||||
notes:
|
||||
- This module doesn't yet support configuration for proxies.
|
||||
# informational: requirements for nodes
|
||||
requirements: [ urllib2, urlparse ]
|
||||
author: Jan-Piet Mens
|
||||
'''
|
||||
|
||||
EXAMPLES='''
|
||||
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440
|
||||
'''
|
||||
|
||||
|
||||
HAS_URLLIB2 = True
|
||||
try:
|
||||
import urllib2
|
||||
except ImportError:
|
||||
HAS_URLLIB2 = False
|
||||
HAS_URLPARSE = True
|
||||
|
||||
try:
|
||||
import urlparse
|
||||
import socket
|
||||
except ImportError:
|
||||
HAS_URLPARSE=False
|
||||
|
||||
# ==============================================================
|
||||
# url handling
|
||||
|
||||
def url_filename(url):
|
||||
fn = os.path.basename(urlparse.urlsplit(url)[2])
|
||||
if fn == '':
|
||||
return 'index.html'
|
||||
return fn
|
||||
|
||||
def url_do_get(module, url, dest, use_proxy):
|
||||
"""
|
||||
Get url and return request and info
|
||||
Credits: http://stackoverflow.com/questions/7006574/how-to-download-file-from-ftp
|
||||
"""
|
||||
|
||||
USERAGENT = 'ansible-httpget'
|
||||
info = dict(url=url, dest=dest)
|
||||
r = None
|
||||
handlers = []
|
||||
|
||||
parsed = urlparse.urlparse(url)
|
||||
|
||||
if '@' in parsed[1]:
|
||||
credentials, netloc = parsed[1].split('@', 1)
|
||||
if ':' in credentials:
|
||||
username, password = credentials.split(':', 1)
|
||||
else:
|
||||
username = credentials
|
||||
password = ''
|
||||
parsed = list(parsed)
|
||||
parsed[1] = netloc
|
||||
|
||||
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
|
||||
# this creates a password manager
|
||||
passman.add_password(None, netloc, username, password)
|
||||
# because we have put None at the start it will always
|
||||
# use this username/password combination for urls
|
||||
# for which `theurl` is a super-url
|
||||
|
||||
authhandler = urllib2.HTTPBasicAuthHandler(passman)
|
||||
# create the AuthHandler
|
||||
handlers.append(authhandler)
|
||||
|
||||
#reconstruct url without credentials
|
||||
url = urlparse.urlunparse(parsed)
|
||||
|
||||
if not use_proxy:
|
||||
proxyhandler = urllib2.ProxyHandler({})
|
||||
handlers.append(proxyhandler)
|
||||
|
||||
opener = urllib2.build_opener(*handlers)
|
||||
urllib2.install_opener(opener)
|
||||
request = urllib2.Request(url)
|
||||
request.add_header('User-agent', USERAGENT)
|
||||
|
||||
if os.path.exists(dest) and not module.params['force']:
|
||||
t = datetime.datetime.utcfromtimestamp(os.path.getmtime(dest))
|
||||
tstamp = t.strftime('%a, %d %b %Y %H:%M:%S +0000')
|
||||
request.add_header('If-Modified-Since', tstamp)
|
||||
|
||||
try:
|
||||
r = urllib2.urlopen(request)
|
||||
info.update(r.info())
|
||||
info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), status=200))
|
||||
except urllib2.HTTPError, e:
|
||||
# Must not fail_json() here so caller can handle HTTP 304 unmodified
|
||||
info.update(dict(msg=str(e), status=e.code))
|
||||
return r, info
|
||||
except urllib2.URLError, e:
|
||||
code = getattr(e, 'code', -1)
|
||||
module.fail_json(msg="Request failed: %s" % str(e), status_code=code)
|
||||
|
||||
return r, info
|
||||
|
||||
def url_get(module, url, dest, use_proxy):
|
||||
"""
|
||||
Download url and store at dest.
|
||||
If dest is a directory, determine filename from url.
|
||||
Return (tempfile, info about the request)
|
||||
"""
|
||||
|
||||
req, info = url_do_get(module, url, dest, use_proxy)
|
||||
|
||||
# TODO: should really handle 304, but how? src file could exist (and be newer) but empty
|
||||
if info['status'] == 304:
|
||||
module.exit_json(url=url, dest=dest, changed=False, msg=info.get('msg', ''))
|
||||
|
||||
# create a temporary file and copy content to do md5-based replacement
|
||||
if info['status'] != 200:
|
||||
module.fail_json(msg="Request failed", status_code=info['status'], response=info['msg'], url=url, dest=dest)
|
||||
|
||||
fd, tempname = tempfile.mkstemp()
|
||||
f = os.fdopen(fd, 'wb')
|
||||
try:
|
||||
shutil.copyfileobj(req, f)
|
||||
except Exception, err:
|
||||
os.remove(tempname)
|
||||
module.fail_json(msg="failed to create temporary content file: %s" % str(err))
|
||||
f.close()
|
||||
req.close()
|
||||
return tempname, info
|
||||
|
||||
# ==============================================================
|
||||
# main
|
||||
|
||||
def main():
|
||||
|
||||
# does this really happen on non-ancient python?
|
||||
if not HAS_URLLIB2:
|
||||
module.fail_json(msg="urllib2 is not installed")
|
||||
if not HAS_URLPARSE:
|
||||
module.fail_json(msg="urlparse is not installed")
|
||||
|
||||
module = AnsibleModule(
|
||||
# not checking because of daisy chain to file module
|
||||
argument_spec = dict(
|
||||
url = dict(required=True),
|
||||
dest = dict(required=True),
|
||||
force = dict(default='no', aliases=['thirsty'], type='bool'),
|
||||
use_proxy = dict(default='yes', type='bool')
|
||||
),
|
||||
add_file_common_args=True
|
||||
)
|
||||
|
||||
url = module.params['url']
|
||||
dest = os.path.expanduser(module.params['dest'])
|
||||
force = module.params['force']
|
||||
use_proxy = module.params['use_proxy']
|
||||
|
||||
if os.path.isdir(dest):
|
||||
dest = os.path.join(dest, url_filename(url))
|
||||
|
||||
if not force:
|
||||
if os.path.exists(dest):
|
||||
module.exit_json(msg="file already exists", dest=dest, url=url, changed=False)
|
||||
|
||||
# download to tmpsrc
|
||||
tmpsrc, info = url_get(module, url, dest, use_proxy)
|
||||
md5sum_src = None
|
||||
md5sum_dest = None
|
||||
|
||||
# raise an error if there is no tmpsrc file
|
||||
if not os.path.exists(tmpsrc):
|
||||
os.remove(tmpsrc)
|
||||
module.fail_json(msg="Request failed", status_code=info['status'], response=info['msg'])
|
||||
if not os.access(tmpsrc, os.R_OK):
|
||||
os.remove(tmpsrc)
|
||||
module.fail_json( msg="Source %s not readable" % (tmpsrc))
|
||||
md5sum_src = module.md5(tmpsrc)
|
||||
|
||||
# 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)
|
||||
module.fail_json( msg="Destination %s not writable" % (dest))
|
||||
if not os.access(dest, os.R_OK):
|
||||
os.remove(tmpsrc)
|
||||
module.fail_json( msg="Destination %s not readable" % (dest))
|
||||
md5sum_dest = module.md5(dest)
|
||||
else:
|
||||
if not os.access(os.path.dirname(dest), os.W_OK):
|
||||
os.remove(tmpsrc)
|
||||
module.fail_json( msg="Destination %s not writable" % (os.path.dirname(dest)))
|
||||
|
||||
if md5sum_src != md5sum_dest:
|
||||
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)))
|
||||
changed = True
|
||||
else:
|
||||
changed = False
|
||||
|
||||
os.remove(tmpsrc)
|
||||
|
||||
# allow file attribute changes
|
||||
module.params['path'] = dest
|
||||
file_args = module.load_file_common_arguments(module.params)
|
||||
file_args['path'] = dest
|
||||
changed = module.set_file_attributes_if_different(file_args, changed)
|
||||
|
||||
# Mission complete
|
||||
module.exit_json(url=url, dest=dest, src=tmpsrc, md5sum=md5sum_src,
|
||||
changed=changed, msg=info.get('msg', ''))
|
||||
|
||||
# this is magic, see lib/ansible/module_common.py
|
||||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||
main()
|
||||
74
library/network/slurp
Normal file
74
library/network/slurp
Normal file
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
||||
#
|
||||
# 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/>.
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: slurp
|
||||
short_description: Slurps a file from remote nodes
|
||||
description:
|
||||
- This module works like M(fetch). It is used for fetching a base64-
|
||||
encoded blob containing the data in a remote file.
|
||||
options:
|
||||
src:
|
||||
description:
|
||||
- The file on the remote system to fetch. This I(must) be a file, not a
|
||||
directory.
|
||||
required: true
|
||||
default: null
|
||||
aliases: []
|
||||
examples:
|
||||
- code: |
|
||||
ansible host -m slurp -a 'src=/tmp/xx'
|
||||
host | success >> {
|
||||
"content": "aGVsbG8gQW5zaWJsZSB3b3JsZAo=",
|
||||
"encoding": "base64"
|
||||
}
|
||||
description: "Example using C(/usr/bin/ansible)"
|
||||
notes:
|
||||
- "See also: M(fetch)"
|
||||
requirements: []
|
||||
author: Michael DeHaan
|
||||
'''
|
||||
|
||||
import base64
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
src = dict(required=True, aliases=['path']),
|
||||
),
|
||||
supports_check_mode=True
|
||||
)
|
||||
source = module.params['src']
|
||||
|
||||
if not os.path.exists(source):
|
||||
module.fail_json(msg="file not found: %s" % source)
|
||||
if not os.access(source, os.R_OK):
|
||||
module.fail_json(msg="file is not readable: %s" % source)
|
||||
|
||||
data = base64.b64encode(file(source).read())
|
||||
|
||||
module.exit_json(content=data, encoding='base64')
|
||||
|
||||
# this is magic, see lib/ansible/module_common.py
|
||||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||
|
||||
main()
|
||||
|
||||
421
library/network/uri
Normal file
421
library/network/uri
Normal file
@@ -0,0 +1,421 @@
|
||||
#!/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
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
import base64
|
||||
import datetime
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
import simplejson as json
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: uri
|
||||
short_description: Interacts with webservices
|
||||
description:
|
||||
- Interacts with HTTP and HTTPS web services and supports Digest, Basic and WSSE
|
||||
HTTP authentication mechanisms.
|
||||
version_added: "1.1"
|
||||
options:
|
||||
url:
|
||||
description:
|
||||
- HTTP or HTTPS URL in the form (http|https)://host.domain[:port]/path
|
||||
required: true
|
||||
default: null
|
||||
aliases: []
|
||||
dest:
|
||||
description:
|
||||
- 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.
|
||||
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:
|
||||
- The body of the http request/response to the web service.
|
||||
required: false
|
||||
default: null
|
||||
method:
|
||||
description:
|
||||
- The HTTP method of the request or response.
|
||||
required: false
|
||||
choices: [ "GET", "POST", "PUT", "HEAD", "DELETE", "OPTIONS" ]
|
||||
default: "GET"
|
||||
return_content:
|
||||
description:
|
||||
- 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.
|
||||
required: false
|
||||
choices: [ "yes", "no" ]
|
||||
default: "no"
|
||||
force_basic_auth:
|
||||
description:
|
||||
- httplib2, the library used by the uri module only sends authentication information when a webservice
|
||||
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" ]
|
||||
default: "no"
|
||||
follow_redirects:
|
||||
description:
|
||||
- Whether or not the URI module should follow all redirects.
|
||||
required: false
|
||||
choices: [ "yes", "no" ]
|
||||
default: "no"
|
||||
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:
|
||||
- A valid, numeric, HTTP status code that signifies success of the request.
|
||||
required: false
|
||||
default: 200
|
||||
timeout:
|
||||
description:
|
||||
- The socket level timeout in seconds
|
||||
required: false
|
||||
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".
|
||||
required: false
|
||||
default: null
|
||||
others:
|
||||
description:
|
||||
- all arguments accepted by the M(file) module also work here
|
||||
required: false
|
||||
examples:
|
||||
- code: "uri: url=http://www.awesome.com"
|
||||
description: "Check that you can connect (GET) to a page and it returns a status 200"
|
||||
|
||||
- code: |
|
||||
action: uri url=http://www.awesome.com return_content=yes
|
||||
register: webpage
|
||||
|
||||
action: fail
|
||||
when_string: '"AWESOME" not in "${webpage.content}"'
|
||||
|
||||
description: Check that a page returns a status 200 and fail if the word AWESOME is not in the page contents.
|
||||
|
||||
- code: |
|
||||
action: >
|
||||
uri url=https://your.jira.server.com/rest/api/2/issue/
|
||||
method=POST user=your_username password=your_pass
|
||||
body='$FILE(issue.json)' force_basic_auth=yes
|
||||
status_code=201 HEADER_Content-Type="application/json"
|
||||
description: "Create a JIRA issue."
|
||||
|
||||
- code: |
|
||||
action: >
|
||||
uri url=https://your.form.based.auth.app.com/index.php
|
||||
method=POST body="name=your_username&password=your_password&enter=Sign%20in"
|
||||
status_code=302 HEADER_Content-Type="application/x-www-form-urlencoded"
|
||||
register: login
|
||||
|
||||
action: uri url=https://your.form.based.auth.app.com/dashboard.php method=GET return_content=yes HEADER_Cookie="${login.set_cookie}"
|
||||
description: "Login to a form based webpage, then use the cookie that got returned to access the app in later tasks."
|
||||
|
||||
# informational: requirements for nodes
|
||||
requirements: [ urlparse, httplib2 ]
|
||||
author: Romeo Theriault
|
||||
'''
|
||||
|
||||
HAS_HTTPLIB2 = True
|
||||
try:
|
||||
import httplib2
|
||||
except ImportError:
|
||||
HAS_HTTPLIB2 = False
|
||||
|
||||
HAS_URLPARSE = True
|
||||
|
||||
try:
|
||||
import urlparse
|
||||
import socket
|
||||
except ImportError:
|
||||
HAS_URLPARSE = False
|
||||
|
||||
|
||||
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()
|
||||
|
||||
md5sum_src = None
|
||||
md5sum_dest = None
|
||||
|
||||
# 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))
|
||||
md5sum_src = module.md5(tmpsrc)
|
||||
|
||||
# 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)
|
||||
module.fail_json( msg="Destination %s not writable" % (dest))
|
||||
if not os.access(dest, os.R_OK):
|
||||
os.remove(tmpsrc)
|
||||
module.fail_json( msg="Destination %s not readable" % (dest))
|
||||
md5sum_dest = module.md5(dest)
|
||||
else:
|
||||
if not os.access(os.path.dirname(dest), os.W_OK):
|
||||
os.remove(tmpsrc)
|
||||
module.fail_json( msg="Destination dir %s not writable" % (os.path.dirname(dest)))
|
||||
|
||||
if md5sum_src != md5sum_dest:
|
||||
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)))
|
||||
|
||||
os.remove(tmpsrc)
|
||||
|
||||
|
||||
def url_filename(url):
|
||||
fn = os.path.basename(urlparse.urlsplit(url)[2])
|
||||
if fn == '':
|
||||
return 'index.html'
|
||||
return fn
|
||||
|
||||
|
||||
def uri(module, url, dest, user, password, body, method, headers, redirects, socket_timeout):
|
||||
# To debug
|
||||
#httplib2.debug = 4
|
||||
|
||||
# Create a Http object and set some default options.
|
||||
h = httplib2.Http(disable_ssl_certificate_validation=True, timeout=socket_timeout)
|
||||
h.follow_all_redirects = redirects
|
||||
h.forward_authorization_headers = True
|
||||
|
||||
# If they have a username or password verify they have both, then add them to the request
|
||||
if user is not None and password is None:
|
||||
module.fail_json(msg="Both a username and password need to be set.")
|
||||
if password is not None and user is None:
|
||||
module.fail_json(msg="Both a username and password need to be set.")
|
||||
if user is not None and password is not None:
|
||||
h.add_credentials(user, password)
|
||||
|
||||
# is dest is set and is a directory, let's check if we get redirected and
|
||||
# set the filename from that url
|
||||
redirected = False
|
||||
resp_redir = {}
|
||||
r = {}
|
||||
if dest is not None:
|
||||
dest = os.path.expanduser(dest)
|
||||
if os.path.isdir(dest):
|
||||
# first check if we are redirected to a file download
|
||||
h.follow_redirects=False
|
||||
# Try the request
|
||||
try:
|
||||
resp_redir, content_redir = h.request(url, method=method, body=body, headers=headers)
|
||||
# if we are redirected, update the url with the location header,
|
||||
# and update dest with the new url filename
|
||||
except:
|
||||
pass
|
||||
if resp_redir['status'] in ["301", "302", "303", "307"]:
|
||||
url = resp_redir['location']
|
||||
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
|
||||
|
||||
# do safe redirects now, including 307
|
||||
h.follow_redirects=True
|
||||
|
||||
# Make the request, or try to :)
|
||||
try:
|
||||
resp, content = h.request(url, method=method, body=body, headers=headers)
|
||||
r['redirected'] = redirected
|
||||
r.update(resp_redir)
|
||||
r.update(resp)
|
||||
return r, content, dest
|
||||
except httplib2.RedirectMissingLocation:
|
||||
module.fail_json(msg="A 3xx redirect response code was provided but no Location: header was provided to point to the new location.")
|
||||
except httplib2.RedirectLimit:
|
||||
module.fail_json(msg="The maximum number of redirections was reached without coming to a final URI.")
|
||||
except httplib2.ServerNotFoundError:
|
||||
module.fail_json(msg="Unable to resolve the host name given.")
|
||||
except httplib2.RelativeURIError:
|
||||
module.fail_json(msg="A relative, as opposed to an absolute URI, was passed in.")
|
||||
except httplib2.FailedToDecompressContent:
|
||||
module.fail_json(msg="The headers claimed that the content of the response was compressed but the decompression algorithm applied to the content failed.")
|
||||
except httplib2.UnimplementedDigestAuthOptionError:
|
||||
module.fail_json(msg="The server requested a type of Digest authentication that we are unfamiliar with.")
|
||||
except httplib2.UnimplementedHmacDigestAuthOptionError:
|
||||
module.fail_json(msg="The server requested a type of HMACDigest authentication that we are unfamiliar with.")
|
||||
except httplib2.UnimplementedHmacDigestAuthOptionError:
|
||||
module.fail_json(msg="The server requested a type of HMACDigest authentication that we are unfamiliar with.")
|
||||
except socket.error, e:
|
||||
module.fail_json(msg="Socket error: %s to %s" % (e, url))
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
url = dict(required=True),
|
||||
dest = dict(required=False, default=None),
|
||||
user = dict(required=False, default=None),
|
||||
password = dict(required=False, default=None),
|
||||
body = dict(required=False, default=None),
|
||||
method = dict(required=False, default='GET', choices=['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'OPTIONS']),
|
||||
return_content = dict(required=False, default='no', type='bool'),
|
||||
force_basic_auth = dict(required=False, default='no', type='bool'),
|
||||
follow_redirects = dict(required=False, default='no', type='bool'),
|
||||
creates = dict(required=False, default=None),
|
||||
removes = dict(required=False, default=None),
|
||||
status_code = dict(required=False, default="200"),
|
||||
timeout = dict(required=False, default=30, type='int'),
|
||||
),
|
||||
check_invalid_arguments=False,
|
||||
add_file_common_args=True
|
||||
)
|
||||
|
||||
if not HAS_HTTPLIB2:
|
||||
module.fail_json(msg="httplib2 is not installed")
|
||||
if not HAS_URLPARSE:
|
||||
module.fail_json(msg="urlparse is not installed")
|
||||
|
||||
url = module.params['url']
|
||||
user = module.params['user']
|
||||
password = module.params['password']
|
||||
body = module.params['body']
|
||||
method = module.params['method']
|
||||
dest = module.params['dest']
|
||||
return_content = module.params['return_content']
|
||||
force_basic_auth = module.params['force_basic_auth']
|
||||
follow_redirects = module.params['follow_redirects']
|
||||
creates = module.params['creates']
|
||||
removes = module.params['removes']
|
||||
status_code = module.params['status_code']
|
||||
socket_timeout = module.params['timeout']
|
||||
|
||||
# Grab all the http headers. Need this hack since passing multi-values is currently a bit ugly. (e.g. headers='{"Content-Type":"application/json"}')
|
||||
dict_headers = {}
|
||||
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.
|
||||
creates = os.path.expanduser(creates)
|
||||
if os.path.exists(creates):
|
||||
module.exit_json(stdout="skipped, since %s exists" % creates, skipped=True, changed=False, stderr=False, rc=0)
|
||||
|
||||
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.
|
||||
v = os.path.expanduser(removes)
|
||||
if not os.path.exists(removes):
|
||||
module.exit_json(stdout="skipped, since %s does not exist" % removes, skipped=True, changed=False, stderr=False, rc=0)
|
||||
|
||||
|
||||
# httplib2 only sends authentication after the server asks for it with a 401.
|
||||
# Some 'basic auth' servies fail to send a 401 and require the authentication
|
||||
# up front. This creates the Basic authentication header and sends it immediately.
|
||||
if force_basic_auth:
|
||||
dict_headers["Authorization"] = "Basic {0}".format(base64.b64encode("{0}:{1}".format(user, password)))
|
||||
|
||||
# Redirects
|
||||
if follow_redirects:
|
||||
redirects = True
|
||||
else:
|
||||
redirects = False
|
||||
|
||||
# Make the request
|
||||
resp, content, dest = uri(module, url, dest, user, password, body, method, dict_headers, redirects, socket_timeout)
|
||||
|
||||
# Write the file out if requested
|
||||
if dest is not None:
|
||||
if resp['status'] == "304":
|
||||
status_code = "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_file_attributes_if_different(file_args, changed)
|
||||
resp['path'] = dest
|
||||
|
||||
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
|
||||
uresp = {}
|
||||
for key, value in resp.iteritems():
|
||||
ukey = key.replace("-", "_")
|
||||
uresp[ukey] = value
|
||||
|
||||
if 'content_type' in uresp:
|
||||
if uresp['content_type'].startswith('application/json'):
|
||||
try:
|
||||
js = json.loads(content)
|
||||
uresp['json'] = js
|
||||
except:
|
||||
pass
|
||||
if resp['status'] != status_code:
|
||||
module.fail_json(msg="Status code was not " + status_code, content=content, **uresp)
|
||||
elif return_content:
|
||||
module.exit_json(changed=changed, content=content, **uresp)
|
||||
else:
|
||||
module.exit_json(changed=changed, **uresp)
|
||||
|
||||
|
||||
# this is magic, see lib/ansible/module_common.py
|
||||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||
main()
|
||||
Reference in New Issue
Block a user