1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge branch 'files_force' of git://github.com/rhaido/ansible into devel

This commit is contained in:
Michael DeHaan 2013-06-30 19:24:37 -04:00
commit c9b6f0d0f2

View file

@ -120,6 +120,20 @@ options:
version_added: "1.1" version_added: "1.1"
description: description:
- recursively set the specified file attributes (applies only to state=directory) - recursively set the specified file attributes (applies only to state=directory)
force:
required: false
default: "no"
choices: [ "yes", "no" ]
description:
- force the creation of the symlinks in two cases: the source file does
not exist (but will appear later); the destination exists and a file (so, we need to unlink the
"path" file and create symlink to the "src" file in place of it).
examples:
- code: "file: path=/etc/foo.conf owner=foo group=foo mode=0644"
description: Example from Ansible Playbooks
- code: "file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link"
- code: "action: file state=link path=/etc/localtime src=/usr/share/zoneinfo/Europe/Zurich force=yes"
description: Force /etc/locatime be the symbolic link to /usr/share/zoneinfo/Europe/Zurich
notes: notes:
- See also M(copy), M(template), M(assemble) - See also M(copy), M(template), M(assemble)
requirements: [ ] requirements: [ ]
@ -150,6 +164,7 @@ def main():
state = dict(choices=['file','directory','link','hard','absent'], default='file'), state = dict(choices=['file','directory','link','hard','absent'], default='file'),
path = dict(aliases=['dest', 'name'], required=True), path = dict(aliases=['dest', 'name'], required=True),
recurse = dict(default='no', type='bool'), recurse = dict(default='no', type='bool'),
force = dict(required=False,default=False,type='bool'),
diff_peek = dict(default=None), diff_peek = dict(default=None),
validate = dict(required=False, default=None), validate = dict(required=False, default=None),
), ),
@ -159,6 +174,7 @@ def main():
params = module.params params = module.params
state = params['state'] state = params['state']
force = params['force']
params['path'] = path = os.path.expanduser(params['path']) params['path'] = path = os.path.expanduser(params['path'])
# short-circuit for diff_peek # short-circuit for diff_peek
@ -226,7 +242,10 @@ def main():
module.exit_json(path=path, changed=True) module.exit_json(path=path, changed=True)
if prev_state != 'absent' and prev_state != state: if prev_state != 'absent' and prev_state != state:
module.fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, src)) if force and prev_state == 'file' and state == 'link':
pass
else:
module.fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, src))
if prev_state == 'absent' and state == 'absent': if prev_state == 'absent' and state == 'absent':
module.exit_json(path=path, changed=False) module.exit_json(path=path, changed=False)
@ -268,7 +287,8 @@ def main():
abs_src = src abs_src = src
else: else:
module.fail_json(msg="absolute paths are required") module.fail_json(msg="absolute paths are required")
if not os.path.exists(abs_src):
if not os.path.exists(abs_src) and not force:
module.fail_json(path=path, src=src, msg='src file does not exist') module.fail_json(path=path, src=src, msg='src file does not exist')
if prev_state == 'absent': if prev_state == 'absent':
@ -287,7 +307,14 @@ def main():
dolink(src, path, state, module) dolink(src, path, state, module)
changed = True changed = True
elif prev_state == 'file': elif prev_state == 'file':
module.fail_json(dest=path, src=src, msg='Cannot link, file exists at destination') if not force:
module.fail_json(dest=path, src=src, msg='Cannot link, file exists at destination')
else:
if module.check_mode:
module.exit_json(changed=True)
os.unlink(path)
dolink(src, path, state, module)
changed = True
else: else:
module.fail_json(dest=path, src=src, msg='unexpected position reached') module.fail_json(dest=path, src=src, msg='unexpected position reached')