mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
6.5 KiB
Plaintext
176 lines
6.5 KiB
Plaintext
13 years ago
|
#!/usr/bin/python
|
||
12 years ago
|
# -*- coding: utf-8 -*-
|
||
13 years ago
|
|
||
13 years ago
|
# (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/>.
|
||
|
|
||
13 years ago
|
import os
|
||
12 years ago
|
import time
|
||
13 years ago
|
|
||
12 years ago
|
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:
|
||
12 years ago
|
- Local path to a file to copy to the remote server; can be absolute or relative.
|
||
12 years ago
|
required: false
|
||
12 years ago
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
content:
|
||
12 years ago
|
version_added: "1.1"
|
||
12 years ago
|
description:
|
||
12 years ago
|
- When used instead of 'src', sets the contents of a file directly to the specified value.
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
12 years ago
|
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"
|
||
12 years ago
|
force:
|
||
12 years ago
|
description:
|
||
12 years ago
|
- 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.
|
||
12 years ago
|
version_added: "1.1"
|
||
|
required: false
|
||
|
choices: [ "yes", "no" ]
|
||
|
default: "yes"
|
||
12 years ago
|
aliases: [ "thirsty" ]
|
||
12 years ago
|
validate:
|
||
|
description:
|
||
|
- validation to run before copying into place
|
||
|
required: false
|
||
|
default: ""
|
||
|
version_added: "1.2"
|
||
12 years ago
|
others:
|
||
|
description:
|
||
|
- all arguments accepted by the M(file) module also work here
|
||
|
required: false
|
||
|
examples:
|
||
12 years ago
|
- code: "copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
|
||
12 years ago
|
description: "Example from Ansible Playbooks"
|
||
12 years ago
|
- code: "copy: src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"
|
||
12 years ago
|
description: "Copy a new C(ntp.conf) file into place, backing up the original if it differs from the copied version"
|
||
12 years ago
|
- code: "copy: src=/mine/sudoers dest=/etc/sudoers validate='visudo -c %s'"
|
||
12 years ago
|
description: "Copy a new C(sudoers) file into place, after passing validation with visudo"
|
||
12 years ago
|
author: Michael DeHaan
|
||
12 years ago
|
notes:
|
||
|
- The "copy" module can't be used to recursively copy directory structures to the target machine. Please see the
|
||
|
"Delegation" section of the Advanced Playbooks documentation for a better approach to recursive copies.
|
||
12 years ago
|
'''
|
||
|
|
||
13 years ago
|
def main():
|
||
|
|
||
|
module = AnsibleModule(
|
||
12 years ago
|
# not checking because of daisy chain to file module
|
||
13 years ago
|
argument_spec = dict(
|
||
12 years ago
|
src = dict(required=False),
|
||
|
original_basename = dict(required=False), # used to handle 'dest is a directory' via template, a slight hack
|
||
|
content = dict(required=False),
|
||
|
dest = dict(required=True),
|
||
|
backup = dict(default=False, type='bool'),
|
||
|
force = dict(default=True, aliases=['thirsty'], type='bool'),
|
||
12 years ago
|
validate = dict(required=False, type='str'),
|
||
12 years ago
|
),
|
||
12 years ago
|
add_file_common_args=True,
|
||
12 years ago
|
)
|
||
13 years ago
|
|
||
12 years ago
|
src = os.path.expanduser(module.params['src'])
|
||
|
dest = os.path.expanduser(module.params['dest'])
|
||
12 years ago
|
backup = module.params['backup']
|
||
12 years ago
|
force = module.params['force']
|
||
12 years ago
|
original_basename = module.params.get('original_basename',None)
|
||
12 years ago
|
validate = module.params.get('validate',None)
|
||
12 years ago
|
|
||
13 years ago
|
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):
|
||
12 years ago
|
if not force:
|
||
|
module.exit_json(msg="file already exists", src=src, dest=dest, changed=False)
|
||
12 years ago
|
if (os.path.isdir(dest)):
|
||
12 years ago
|
basename = os.path.basename(src)
|
||
12 years ago
|
if original_basename:
|
||
|
basename = original_basename
|
||
12 years ago
|
dest = os.path.join(dest, basename)
|
||
12 years ago
|
if os.access(dest, os.R_OK):
|
||
|
md5sum_dest = module.md5(dest)
|
||
13 years ago
|
else:
|
||
12 years ago
|
if not os.path.exists(os.path.dirname(dest)):
|
||
|
module.fail_json(msg="Destination directory %s does not exist" % (os.path.dirname(dest)))
|
||
12 years ago
|
if not os.access(os.path.dirname(dest), os.W_OK):
|
||
|
module.fail_json(msg="Destination %s not writable" % (os.path.dirname(dest)))
|
||
13 years ago
|
|
||
12 years ago
|
backup_file = None
|
||
12 years ago
|
if md5sum_src != md5sum_dest or os.path.islink(dest):
|
||
13 years ago
|
try:
|
||
12 years ago
|
if backup:
|
||
12 years ago
|
if os.path.exists(dest):
|
||
12 years ago
|
backup_file = module.backup_local(dest)
|
||
12 years ago
|
# allow for conversion from symlink.
|
||
|
if os.path.islink(dest):
|
||
|
os.unlink(dest)
|
||
|
open(dest, 'w').close()
|
||
12 years ago
|
if validate:
|
||
12 years ago
|
(rc,out,err) = module.run_command(validate % src)
|
||
12 years ago
|
if rc != 0:
|
||
|
module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err))
|
||
12 years ago
|
if not module.atomic_move(src, dest):
|
||
|
try:
|
||
|
os.unlink(src) # cleanup tmp files on failure
|
||
|
except OSError, e:
|
||
|
sys.stderr.write("failed to clean up tmp file %s: %s\n" % (src, e))
|
||
13 years ago
|
except IOError:
|
||
12 years ago
|
module.fail_json(msg="failed to copy: %s to %s" % (src, dest))
|
||
13 years ago
|
changed = True
|
||
|
else:
|
||
|
changed = False
|
||
|
|
||
12 years ago
|
res_args = dict(
|
||
|
dest = dest, src = src, md5sum = md5sum_src, changed = changed
|
||
|
)
|
||
12 years ago
|
if backup_file:
|
||
|
res_args['backup_file'] = backup_file
|
||
12 years ago
|
|
||
|
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'])
|
||
|
|
||
12 years ago
|
module.exit_json(**res_args)
|
||
13 years ago
|
|
||
|
# this is magic, see lib/ansible/module_common.py
|
||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||
|
main()
|