2012-02-24 10:15:28 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-24 10:15:28 +00:00
|
|
|
|
2012-02-29 00:08:09 +00: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 10:15:28 +00:00
|
|
|
import os
|
2012-09-11 21:53:43 +00:00
|
|
|
import time
|
2012-02-24 10:15:28 +00:00
|
|
|
|
2012-09-28 19:55:49 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: copy
|
2013-11-18 23:55:49 +00:00
|
|
|
version_added: "historical"
|
2012-09-28 19:55:49 +00:00
|
|
|
short_description: Copies files to remote locations.
|
|
|
|
description:
|
|
|
|
- The M(copy) module copies a file on the local box to remote locations.
|
|
|
|
options:
|
|
|
|
src:
|
|
|
|
description:
|
2013-06-26 18:24:15 +00:00
|
|
|
- Local path to a file to copy to the remote server; can be absolute or relative.
|
2013-10-04 18:58:49 +00: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 16:32:25 +00:00
|
|
|
required: false
|
2012-09-28 19:55:49 +00:00
|
|
|
default: null
|
|
|
|
aliases: []
|
2013-03-04 16:32:25 +00:00
|
|
|
content:
|
2013-03-30 19:44:34 +00:00
|
|
|
version_added: "1.1"
|
2013-03-04 16:32:25 +00:00
|
|
|
description:
|
2013-03-30 19:44:34 +00:00
|
|
|
- When used instead of 'src', sets the contents of a file directly to the specified value.
|
2013-03-04 16:32:25 +00:00
|
|
|
required: false
|
|
|
|
default: null
|
2012-09-28 19:55:49 +00:00
|
|
|
dest:
|
|
|
|
description:
|
2013-10-04 18:58:49 +00: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 19:55:49 +00: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 12:08:04 +00:00
|
|
|
force:
|
2013-02-08 15:29:52 +00:00
|
|
|
description:
|
2013-02-16 17:36:16 +00:00
|
|
|
- the default is C(yes), which will replace the remote file when contents
|
|
|
|
are different than the source. If C(no), the file will only be transferred
|
|
|
|
if the destination does not exist.
|
2013-02-08 15:29:52 +00:00
|
|
|
version_added: "1.1"
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "yes"
|
2013-02-20 12:08:04 +00:00
|
|
|
aliases: [ "thirsty" ]
|
2013-03-27 03:12:56 +00:00
|
|
|
validate:
|
|
|
|
description:
|
2013-10-10 01:21:43 +00:00
|
|
|
- The validation command to run before copying into place. The path to the file to
|
|
|
|
validate is passed in via '%s' which must be present as in the visudo example below.
|
2013-03-27 03:12:56 +00:00
|
|
|
required: false
|
|
|
|
default: ""
|
|
|
|
version_added: "1.2"
|
2012-09-28 19:55:49 +00:00
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- all arguments accepted by the M(file) module also work here
|
|
|
|
required: false
|
|
|
|
author: Michael DeHaan
|
2013-03-26 20:34:16 +00:00
|
|
|
notes:
|
2013-10-04 18:58:49 +00:00
|
|
|
- The "copy" module recursively copy facility does not scale to lots (>hundreds) of files.
|
|
|
|
For alternative, see "Delegation" section of the Advanced Playbooks documentation.
|
2012-09-28 19:55:49 +00:00
|
|
|
'''
|
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Example from Ansible Playbooks
|
|
|
|
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644
|
|
|
|
|
|
|
|
# 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 18:24:15 +00:00
|
|
|
- copy: src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'
|
2013-06-14 09:53:43 +00:00
|
|
|
'''
|
|
|
|
|
2012-07-21 21:07:42 +00:00
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2012-08-01 23:42:31 +00:00
|
|
|
# not checking because of daisy chain to file module
|
2012-07-21 21:07:42 +00:00
|
|
|
argument_spec = dict(
|
2013-03-26 02:19:35 +00:00
|
|
|
src = dict(required=False),
|
2013-06-26 18:24:15 +00:00
|
|
|
original_basename = dict(required=False), # used to handle 'dest is a directory' via template, a slight hack
|
2013-05-10 18:32:33 +00:00
|
|
|
content = dict(required=False, no_log=True),
|
2013-03-26 02:19:35 +00:00
|
|
|
dest = dict(required=True),
|
|
|
|
backup = dict(default=False, type='bool'),
|
|
|
|
force = dict(default=True, aliases=['thirsty'], type='bool'),
|
2013-03-27 03:12:56 +00:00
|
|
|
validate = dict(required=False, type='str'),
|
2012-10-21 02:51:36 +00:00
|
|
|
),
|
2013-02-04 00:46:25 +00:00
|
|
|
add_file_common_args=True,
|
2012-08-07 00:07:02 +00:00
|
|
|
)
|
2012-07-21 21:07:42 +00:00
|
|
|
|
2013-03-13 17:34:52 +00:00
|
|
|
src = os.path.expanduser(module.params['src'])
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
2013-02-23 18:59:52 +00:00
|
|
|
backup = module.params['backup']
|
2013-03-13 17:34:52 +00:00
|
|
|
force = module.params['force']
|
2013-03-26 02:19:35 +00:00
|
|
|
original_basename = module.params.get('original_basename',None)
|
2013-03-27 03:12:56 +00:00
|
|
|
validate = module.params.get('validate',None)
|
2012-10-21 02:51:36 +00:00
|
|
|
|
2012-07-21 21:07:42 +00: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))
|
|
|
|
|
|
|
|
md5sum_src = module.md5(src)
|
|
|
|
md5sum_dest = None
|
|
|
|
|
2013-10-04 18:58:49 +00:00
|
|
|
# Special handling for recursive copy - create intermediate dirs
|
|
|
|
if original_basename and dest.endswith("/"):
|
|
|
|
dest = os.path.join(dest, original_basename)
|
|
|
|
dirname = os.path.dirname(dest)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
|
|
|
|
2012-07-21 21:07:42 +00:00
|
|
|
if os.path.exists(dest):
|
2013-03-14 10:27:49 +00:00
|
|
|
if not force:
|
|
|
|
module.exit_json(msg="file already exists", src=src, dest=dest, changed=False)
|
2012-07-31 04:40:50 +00:00
|
|
|
if (os.path.isdir(dest)):
|
2012-10-13 00:07:05 +00:00
|
|
|
basename = os.path.basename(src)
|
2013-03-26 02:19:35 +00:00
|
|
|
if original_basename:
|
|
|
|
basename = original_basename
|
2012-10-13 00:07:05 +00:00
|
|
|
dest = os.path.join(dest, basename)
|
2013-02-17 18:13:20 +00:00
|
|
|
if os.access(dest, os.R_OK):
|
|
|
|
md5sum_dest = module.md5(dest)
|
2012-07-21 21:07:42 +00:00
|
|
|
else:
|
2012-09-25 21:57:32 +00:00
|
|
|
if not os.path.exists(os.path.dirname(dest)):
|
|
|
|
module.fail_json(msg="Destination directory %s does not exist" % (os.path.dirname(dest)))
|
2013-02-17 18:13:20 +00: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 21:07:42 +00:00
|
|
|
|
2012-08-09 18:24:21 +00:00
|
|
|
backup_file = None
|
2012-10-18 00:04:52 +00:00
|
|
|
if md5sum_src != md5sum_dest or os.path.islink(dest):
|
2012-07-21 21:07:42 +00:00
|
|
|
try:
|
2012-08-09 18:24:21 +00:00
|
|
|
if backup:
|
2012-08-09 19:33:05 +00:00
|
|
|
if os.path.exists(dest):
|
2012-09-04 23:49:49 +00:00
|
|
|
backup_file = module.backup_local(dest)
|
2012-10-18 00:04:52 +00:00
|
|
|
# allow for conversion from symlink.
|
|
|
|
if os.path.islink(dest):
|
|
|
|
os.unlink(dest)
|
|
|
|
open(dest, 'w').close()
|
2013-03-27 03:12:56 +00:00
|
|
|
if validate:
|
2013-05-11 21:21:04 +00: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 18:30:48 +00:00
|
|
|
module.atomic_move(src, dest)
|
2012-07-21 21:07:42 +00:00
|
|
|
except IOError:
|
2012-08-07 00:07:02 +00:00
|
|
|
module.fail_json(msg="failed to copy: %s to %s" % (src, dest))
|
2012-07-21 21:07:42 +00:00
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
|
2012-10-21 02:51:36 +00:00
|
|
|
res_args = dict(
|
|
|
|
dest = dest, src = src, md5sum = md5sum_src, changed = changed
|
|
|
|
)
|
2012-08-09 18:24:21 +00:00
|
|
|
if backup_file:
|
|
|
|
res_args['backup_file'] = backup_file
|
2012-10-21 02:51:36 +00:00
|
|
|
|
|
|
|
module.params['dest'] = dest
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
res_args['changed'] = module.set_file_attributes_if_different(file_args, res_args['changed'])
|
|
|
|
|
2012-08-09 18:24:21 +00:00
|
|
|
module.exit_json(**res_args)
|
2012-07-21 21:07:42 +00:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
2013-12-02 20:11:23 +00:00
|
|
|
from ansible.module_utils.basic import *
|
2012-07-21 21:07:42 +00:00
|
|
|
main()
|