added force parameter to symlink invocation, in order to force the creation in case of already existing destination with the type of 'file'

This commit is contained in:
Mike Grozak 2013-05-30 12:53:24 +02:00
parent 995ef374db
commit 57154b04bb

View file

@ -150,6 +150,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 +160,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 +228,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)
@ -287,7 +292,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')