2012-02-24 11:15:28 +01:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-24 11:15:28 +01:00
|
|
|
|
2012-02-29 01:08:09 +01:00
|
|
|
# (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/>.
|
|
|
|
|
2012-02-24 11:15:28 +01:00
|
|
|
import os
|
2012-09-11 23:53:43 +02:00
|
|
|
import time
|
2012-02-24 11:15:28 +01:00
|
|
|
|
2012-09-28 21:55:49 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: copy
|
2013-11-19 00:55:49 +01:00
|
|
|
version_added: "historical"
|
2012-09-28 21:55:49 +02:00
|
|
|
short_description: Copies files to remote locations.
|
|
|
|
description:
|
2014-11-11 19:22:27 +01:00
|
|
|
- The M(copy) module copies a file on the local box to remote locations. Use the M(fetch) module to copy files from remote locations to the local box.
|
2012-09-28 21:55:49 +02:00
|
|
|
options:
|
|
|
|
src:
|
|
|
|
description:
|
2013-06-26 20:24:15 +02:00
|
|
|
- Local path to a file to copy to the remote server; can be absolute or relative.
|
2013-10-04 20:58:49 +02:00
|
|
|
If path is a directory, it is copied recursively. In this case, if path ends
|
|
|
|
with "/", only inside contents of that directory are copied to destination.
|
|
|
|
Otherwise, if it does not end with "/", the directory itself with all contents
|
|
|
|
is copied. This behavior is similar to Rsync.
|
2013-03-04 17:32:25 +01:00
|
|
|
required: false
|
2012-09-28 21:55:49 +02:00
|
|
|
default: null
|
|
|
|
aliases: []
|
2013-03-04 17:32:25 +01:00
|
|
|
content:
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.1"
|
2013-03-04 17:32:25 +01:00
|
|
|
description:
|
2013-03-30 20:44:34 +01:00
|
|
|
- When used instead of 'src', sets the contents of a file directly to the specified value.
|
2015-04-15 16:02:30 +02:00
|
|
|
This is for simple values, for anything complex or with formatting please switch to the template module.
|
2013-03-04 17:32:25 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2012-09-28 21:55:49 +02:00
|
|
|
dest:
|
|
|
|
description:
|
2013-10-04 20:58:49 +02:00
|
|
|
- Remote absolute path where the file should be copied to. If src is a directory,
|
|
|
|
this must be a directory too.
|
2012-09-28 21:55:49 +02:00
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
backup:
|
|
|
|
description:
|
|
|
|
- Create a backup file including the timestamp information so you can get
|
|
|
|
the original file back if you somehow clobbered it incorrectly.
|
|
|
|
version_added: "0.7"
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
2013-02-20 13:08:04 +01:00
|
|
|
force:
|
2013-02-08 16:29:52 +01:00
|
|
|
description:
|
2013-02-16 18:36:16 +01:00
|
|
|
- the default is C(yes), which will replace the remote file when contents
|
2015-06-02 23:07:30 +02:00
|
|
|
are different than the source. If C(no), the file will only be transferred
|
2013-02-16 18:36:16 +01:00
|
|
|
if the destination does not exist.
|
2013-02-08 16:29:52 +01:00
|
|
|
version_added: "1.1"
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "yes"
|
2013-02-20 13:08:04 +01:00
|
|
|
aliases: [ "thirsty" ]
|
2014-02-28 08:14:32 +01:00
|
|
|
directory_mode:
|
|
|
|
description:
|
2014-08-12 19:19:13 +02:00
|
|
|
- When doing a recursive copy set the mode for the directories. If this is not set we will use the system
|
|
|
|
defaults. The mode is only set on directories which are newly created, and will not affect those that
|
|
|
|
already existed.
|
2014-02-28 08:14:32 +01:00
|
|
|
required: false
|
|
|
|
version_added: "1.5"
|
2014-06-16 18:47:45 +02:00
|
|
|
extends_documentation_fragment: files
|
2015-06-02 23:07:30 +02:00
|
|
|
extends_documentation_fragment: validate
|
2015-06-15 21:53:30 +02:00
|
|
|
author:
|
|
|
|
- "Ansible Core Team"
|
|
|
|
- "Michael DeHaan"
|
2013-03-26 21:34:16 +01:00
|
|
|
notes:
|
2013-10-04 20:58:49 +02:00
|
|
|
- The "copy" module recursively copy facility does not scale to lots (>hundreds) of files.
|
2014-01-30 23:43:40 +01:00
|
|
|
For alternative, see synchronize module, which is a wrapper around rsync.
|
2012-09-28 21:55:49 +02:00
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Example from Ansible Playbooks
|
|
|
|
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644
|
|
|
|
|
2014-08-26 16:09:08 +02:00
|
|
|
# The same example as above, but using a symbolic mode equivalent to 0644
|
|
|
|
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode="u=rw,g=r,o=r"
|
|
|
|
|
|
|
|
# Another symbolic mode example, adding some permissions and removing others
|
|
|
|
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode="u+rw,g-wx,o-rwx"
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
# Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
|
|
|
|
- copy: src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes
|
|
|
|
|
|
|
|
# Copy a new "sudoers" file into place, after passing validation with visudo
|
2013-06-26 20:24:15 +02:00
|
|
|
- copy: src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
2015-02-13 04:11:32 +01:00
|
|
|
RETURN = '''
|
|
|
|
dest:
|
|
|
|
description: destination file/path
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: string
|
|
|
|
sample: "/path/to/file.txt"
|
|
|
|
src:
|
|
|
|
description: source file used for the copy on the target machine
|
|
|
|
returned: changed
|
|
|
|
type: string
|
|
|
|
sample: "/home/httpd/.ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source"
|
|
|
|
md5sum:
|
|
|
|
description: md5 checksum of the file after running copy
|
|
|
|
returned: when supported
|
|
|
|
type: string
|
2015-03-20 22:00:55 +01:00
|
|
|
sample: "2a5aeecc61dc98c4d780b14b330e3282"
|
2015-02-13 04:11:32 +01:00
|
|
|
checksum:
|
|
|
|
description: checksum of the file after running copy
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: string
|
|
|
|
sample: "6e642bb8dd5c2e027bf21dd923337cbb4214f827"
|
|
|
|
backup_file:
|
|
|
|
description: name of backup file created
|
|
|
|
returned: changed and if backup=yes
|
|
|
|
type: string
|
|
|
|
sample: "/path/to/file.txt.2015-02-12@22:09~"
|
|
|
|
gid:
|
|
|
|
description: group id of the file, after execution
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: int
|
|
|
|
sample: 100
|
|
|
|
group:
|
|
|
|
description: group of the file, after execution
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: string
|
|
|
|
sample: "httpd"
|
|
|
|
owner:
|
|
|
|
description: owner of the file, after execution
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: string
|
|
|
|
sample: "httpd"
|
2015-03-20 22:00:55 +01:00
|
|
|
uid:
|
2015-02-13 04:11:32 +01:00
|
|
|
description: owner id of the file, after execution
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: int
|
|
|
|
sample: 100
|
|
|
|
mode:
|
|
|
|
description: permissions of the target, after execution
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: string
|
|
|
|
sample: "0644"
|
|
|
|
size:
|
|
|
|
description: size of the target, after execution
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: int
|
|
|
|
sample: 1220
|
|
|
|
state:
|
|
|
|
description: permissions of the target, after execution
|
2015-02-19 21:36:36 +01:00
|
|
|
returned: success
|
2015-02-13 04:11:32 +01:00
|
|
|
type: string
|
|
|
|
sample: "file"
|
|
|
|
'''
|
2014-02-28 17:00:54 +01:00
|
|
|
|
2014-02-28 08:14:32 +01:00
|
|
|
def split_pre_existing_dir(dirname):
|
2014-02-28 17:00:54 +01:00
|
|
|
'''
|
|
|
|
Return the first pre-existing directory and a list of the new directories that will be created.
|
|
|
|
'''
|
|
|
|
|
2014-02-28 08:14:32 +01:00
|
|
|
head, tail = os.path.split(dirname)
|
|
|
|
if not os.path.exists(head):
|
|
|
|
(pre_existing_dir, new_directory_list) = split_pre_existing_dir(head)
|
|
|
|
else:
|
|
|
|
return (head, [ tail ])
|
2014-05-01 18:36:42 +02:00
|
|
|
new_directory_list.append(tail)
|
2014-02-28 08:14:32 +01:00
|
|
|
return (pre_existing_dir, new_directory_list)
|
|
|
|
|
2014-02-28 17:00:54 +01:00
|
|
|
|
2014-02-28 08:14:32 +01:00
|
|
|
def adjust_recursive_directory_permissions(pre_existing_dir, new_directory_list, module, directory_args, changed):
|
2014-02-28 17:00:54 +01:00
|
|
|
'''
|
|
|
|
Walk the new directories list and make sure that permissions are as we would expect
|
|
|
|
'''
|
|
|
|
|
2014-02-28 08:14:32 +01:00
|
|
|
if len(new_directory_list) > 0:
|
|
|
|
working_dir = os.path.join(pre_existing_dir, new_directory_list.pop(0))
|
|
|
|
directory_args['path'] = working_dir
|
2014-03-19 03:39:45 +01:00
|
|
|
changed = module.set_fs_attributes_if_different(directory_args, changed)
|
2014-02-28 08:14:32 +01:00
|
|
|
changed = adjust_recursive_directory_permissions(working_dir, new_directory_list, module, directory_args, changed)
|
|
|
|
return changed
|
|
|
|
|
2014-02-28 17:00:54 +01:00
|
|
|
|
2012-07-21 23:07:42 +02:00
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2012-08-02 01:42:31 +02:00
|
|
|
# not checking because of daisy chain to file module
|
2012-07-21 23:07:42 +02:00
|
|
|
argument_spec = dict(
|
2013-03-26 03:19:35 +01:00
|
|
|
src = dict(required=False),
|
2013-06-26 20:24:15 +02:00
|
|
|
original_basename = dict(required=False), # used to handle 'dest is a directory' via template, a slight hack
|
2013-05-10 20:32:33 +02:00
|
|
|
content = dict(required=False, no_log=True),
|
2013-03-26 03:19:35 +01:00
|
|
|
dest = dict(required=True),
|
|
|
|
backup = dict(default=False, type='bool'),
|
|
|
|
force = dict(default=True, aliases=['thirsty'], type='bool'),
|
2013-03-27 04:12:56 +01:00
|
|
|
validate = dict(required=False, type='str'),
|
2014-02-28 17:00:54 +01:00
|
|
|
directory_mode = dict(required=False)
|
2012-10-21 04:51:36 +02:00
|
|
|
),
|
2013-02-04 01:46:25 +01:00
|
|
|
add_file_common_args=True,
|
2014-08-18 23:02:45 +02:00
|
|
|
supports_check_mode=True,
|
2012-08-07 02:07:02 +02:00
|
|
|
)
|
2012-07-21 23:07:42 +02:00
|
|
|
|
2014-02-26 22:18:27 +01:00
|
|
|
src = os.path.expanduser(module.params['src'])
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
2013-02-23 19:59:52 +01:00
|
|
|
backup = module.params['backup']
|
2013-03-13 18:34:52 +01:00
|
|
|
force = module.params['force']
|
2013-03-26 03:19:35 +01:00
|
|
|
original_basename = module.params.get('original_basename',None)
|
2013-03-27 04:12:56 +01:00
|
|
|
validate = module.params.get('validate',None)
|
2014-09-16 19:03:40 +02:00
|
|
|
follow = module.params['follow']
|
2012-10-21 04:51:36 +02:00
|
|
|
|
2012-07-21 23:07:42 +02:00
|
|
|
if not os.path.exists(src):
|
|
|
|
module.fail_json(msg="Source %s failed to transfer" % (src))
|
|
|
|
if not os.access(src, os.R_OK):
|
|
|
|
module.fail_json(msg="Source %s not readable" % (src))
|
|
|
|
|
2014-11-07 06:25:55 +01:00
|
|
|
checksum_src = module.sha1(src)
|
|
|
|
checksum_dest = None
|
|
|
|
# Backwards compat only. This will be None in FIPS mode
|
|
|
|
try:
|
|
|
|
md5sum_src = module.md5(src)
|
|
|
|
except ValueError:
|
|
|
|
md5sum_src = None
|
2012-07-21 23:07:42 +02:00
|
|
|
|
2014-02-28 08:14:32 +01:00
|
|
|
changed = False
|
|
|
|
|
2013-10-04 20:58:49 +02:00
|
|
|
# Special handling for recursive copy - create intermediate dirs
|
2015-04-03 15:06:58 +02:00
|
|
|
if original_basename and dest.endswith(os.sep):
|
2013-10-04 20:58:49 +02:00
|
|
|
dest = os.path.join(dest, original_basename)
|
|
|
|
dirname = os.path.dirname(dest)
|
2015-02-17 00:34:22 +01:00
|
|
|
if not os.path.exists(dirname) and os.path.isabs(dirname):
|
2014-02-28 08:14:32 +01:00
|
|
|
(pre_existing_dir, new_directory_list) = split_pre_existing_dir(dirname)
|
2013-10-04 20:58:49 +02:00
|
|
|
os.makedirs(dirname)
|
2014-02-28 08:14:32 +01:00
|
|
|
directory_args = module.load_file_common_arguments(module.params)
|
|
|
|
directory_mode = module.params["directory_mode"]
|
|
|
|
if directory_mode is not None:
|
|
|
|
directory_args['mode'] = directory_mode
|
2014-02-28 17:00:54 +01:00
|
|
|
else:
|
|
|
|
directory_args['mode'] = None
|
2014-02-28 08:14:32 +01:00
|
|
|
adjust_recursive_directory_permissions(pre_existing_dir, new_directory_list, module, directory_args, changed)
|
2013-10-04 20:58:49 +02:00
|
|
|
|
2012-07-21 23:07:42 +02:00
|
|
|
if os.path.exists(dest):
|
2014-09-16 19:03:40 +02:00
|
|
|
if os.path.islink(dest) and follow:
|
|
|
|
dest = os.path.realpath(dest)
|
2013-03-14 11:27:49 +01:00
|
|
|
if not force:
|
|
|
|
module.exit_json(msg="file already exists", src=src, dest=dest, changed=False)
|
2012-07-31 06:40:50 +02:00
|
|
|
if (os.path.isdir(dest)):
|
2012-10-13 02:07:05 +02:00
|
|
|
basename = os.path.basename(src)
|
2013-03-26 03:19:35 +01:00
|
|
|
if original_basename:
|
|
|
|
basename = original_basename
|
2012-10-13 02:07:05 +02:00
|
|
|
dest = os.path.join(dest, basename)
|
2013-02-17 19:13:20 +01:00
|
|
|
if os.access(dest, os.R_OK):
|
2014-11-07 06:25:55 +01:00
|
|
|
checksum_dest = module.sha1(dest)
|
2012-07-21 23:07:42 +02:00
|
|
|
else:
|
2012-09-25 23:57:32 +02:00
|
|
|
if not os.path.exists(os.path.dirname(dest)):
|
2014-06-18 21:54:44 +02:00
|
|
|
try:
|
|
|
|
# os.path.exists() can return false in some
|
|
|
|
# circumstances where the directory does not have
|
|
|
|
# the execute bit for the current user set, in
|
|
|
|
# which case the stat() call will raise an OSError
|
|
|
|
os.stat(os.path.dirname(dest))
|
|
|
|
except OSError, e:
|
|
|
|
if "permission denied" in str(e).lower():
|
|
|
|
module.fail_json(msg="Destination directory %s is not accessible" % (os.path.dirname(dest)))
|
2012-09-25 23:57:32 +02:00
|
|
|
module.fail_json(msg="Destination directory %s does not exist" % (os.path.dirname(dest)))
|
2013-02-17 19:13:20 +01:00
|
|
|
if not os.access(os.path.dirname(dest), os.W_OK):
|
|
|
|
module.fail_json(msg="Destination %s not writable" % (os.path.dirname(dest)))
|
2012-07-21 23:07:42 +02:00
|
|
|
|
2012-08-09 20:24:21 +02:00
|
|
|
backup_file = None
|
2014-11-07 06:25:55 +01:00
|
|
|
if checksum_src != checksum_dest or os.path.islink(dest):
|
2012-07-21 23:07:42 +02:00
|
|
|
try:
|
2012-08-09 20:24:21 +02:00
|
|
|
if backup:
|
2012-08-09 21:33:05 +02:00
|
|
|
if os.path.exists(dest):
|
2012-09-05 01:49:49 +02:00
|
|
|
backup_file = module.backup_local(dest)
|
2012-10-18 02:04:52 +02:00
|
|
|
# allow for conversion from symlink.
|
|
|
|
if os.path.islink(dest):
|
|
|
|
os.unlink(dest)
|
|
|
|
open(dest, 'w').close()
|
2013-03-27 04:12:56 +01:00
|
|
|
if validate:
|
2014-04-19 04:39:10 +02:00
|
|
|
if "%s" not in validate:
|
|
|
|
module.fail_json(msg="validate must contain %%s: %s" % (validate))
|
2013-05-11 23:21:04 +02:00
|
|
|
(rc,out,err) = module.run_command(validate % src)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err))
|
2013-05-05 20:30:48 +02:00
|
|
|
module.atomic_move(src, dest)
|
2012-07-21 23:07:42 +02:00
|
|
|
except IOError:
|
2012-08-07 02:07:02 +02:00
|
|
|
module.fail_json(msg="failed to copy: %s to %s" % (src, dest))
|
2012-07-21 23:07:42 +02:00
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
|
2012-10-21 04:51:36 +02:00
|
|
|
res_args = dict(
|
2014-11-07 06:25:55 +01:00
|
|
|
dest = dest, src = src, md5sum = md5sum_src, checksum = checksum_src, changed = changed
|
2012-10-21 04:51:36 +02:00
|
|
|
)
|
2012-08-09 20:24:21 +02:00
|
|
|
if backup_file:
|
|
|
|
res_args['backup_file'] = backup_file
|
2012-10-21 04:51:36 +02:00
|
|
|
|
|
|
|
module.params['dest'] = dest
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
2014-03-19 03:39:45 +01:00
|
|
|
res_args['changed'] = module.set_fs_attributes_if_different(file_args, res_args['changed'])
|
2012-10-21 04:51:36 +02:00
|
|
|
|
2012-08-09 20:24:21 +02:00
|
|
|
module.exit_json(**res_args)
|
2012-07-21 23:07:42 +02: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 *
|
2012-07-21 23:07:42 +02:00
|
|
|
main()
|