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-07-04 13:26:25 +02:00
|
|
|
import shutil
|
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
|
|
|
|
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:
|
|
|
|
- Local path to a file to copy to the remote server; can be absolute or relative.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- Remote absolute path where the file should be copied to.
|
|
|
|
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"
|
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- all arguments accepted by the M(file) module also work here
|
|
|
|
required: false
|
|
|
|
examples:
|
|
|
|
- code: copy src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644
|
|
|
|
description: "Example from Ansible Playbooks"
|
|
|
|
- code: copy src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes
|
|
|
|
description: "Copy a new C(ntp.conf) file into place, backing up the original if it differs from the copied version"
|
|
|
|
author: Michael DeHaan
|
|
|
|
'''
|
|
|
|
|
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
|
|
|
|
check_invalid_arguments = False,
|
2012-07-21 23:07:42 +02:00
|
|
|
argument_spec = dict(
|
|
|
|
src=dict(required=True),
|
2012-08-09 20:24:21 +02:00
|
|
|
dest=dict(required=True),
|
|
|
|
backup=dict(default=False, choices=BOOLEANS),
|
2012-07-21 23:07:42 +02:00
|
|
|
)
|
2012-08-07 02:07:02 +02:00
|
|
|
)
|
2012-07-21 23:07:42 +02:00
|
|
|
|
|
|
|
src = os.path.expanduser(module.params['src'])
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
2012-08-09 20:24:21 +02:00
|
|
|
backup = module.boolean(module.params.get('backup', False))
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
md5sum_src = module.md5(src)
|
|
|
|
md5sum_dest = None
|
|
|
|
|
|
|
|
if os.path.exists(dest):
|
|
|
|
if not os.access(dest, os.W_OK):
|
|
|
|
module.fail_json(msg="Destination %s not writable" % (dest))
|
|
|
|
if not os.access(dest, os.R_OK):
|
|
|
|
module.fail_json(msg="Destination %s not readable" % (dest))
|
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)
|
|
|
|
dest = os.path.join(dest, basename)
|
2012-07-21 23:07:42 +02:00
|
|
|
md5sum_dest = module.md5(dest)
|
|
|
|
else:
|
2012-09-25 23:57:32 +02:00
|
|
|
if not os.path.exists(os.path.dirname(dest)):
|
|
|
|
module.fail_json(msg="Destination directory %s does not exist" % (os.path.dirname(dest)))
|
2012-07-21 23:07:42 +02: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-08-09 20:24:21 +02:00
|
|
|
backup_file = None
|
2012-10-18 02:04:52 +02:00
|
|
|
if md5sum_src != md5sum_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()
|
2012-09-11 23:53:43 +02:00
|
|
|
#TODO:pid + epoch should avoid most collisions, hostname/mac for those using nfs?
|
|
|
|
# might be an issue with exceeding path length
|
2012-09-12 20:52:54 +02:00
|
|
|
dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.time())
|
2012-09-11 23:53:43 +02:00
|
|
|
shutil.copyfile(src, dest_tmp)
|
2012-09-12 20:47:06 +02:00
|
|
|
shutil.move(dest_tmp, dest)
|
2012-07-21 23:07:42 +02:00
|
|
|
except shutil.Error:
|
2012-08-07 02:07:02 +02:00
|
|
|
module.fail_json(msg="failed to copy: %s and %s are the same" % (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-08-09 20:24:21 +02:00
|
|
|
res_args = {
|
|
|
|
'dest':dest,
|
|
|
|
'src':src,
|
|
|
|
'md5sum':md5sum_src,
|
|
|
|
'changed':changed
|
|
|
|
}
|
|
|
|
if backup_file:
|
|
|
|
res_args['backup_file'] = backup_file
|
|
|
|
|
|
|
|
module.exit_json(**res_args)
|
2012-07-21 23:07:42 +02:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|