diff --git a/files/file b/files/file index 5fbb9ecc2dc..1fe3364fcf9 100644 --- a/files/file +++ b/files/file @@ -120,6 +120,20 @@ options: version_added: "1.1" description: - 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: - See also M(copy), M(template), M(assemble) requirements: [ ] @@ -150,6 +164,7 @@ def main(): state = dict(choices=['file','directory','link','hard','absent'], default='file'), path = dict(aliases=['dest', 'name'], required=True), recurse = dict(default='no', type='bool'), + force = dict(required=False,default=False,type='bool'), diff_peek = dict(default=None), validate = dict(required=False, default=None), ), @@ -159,6 +174,7 @@ def main(): params = module.params state = params['state'] + force = params['force'] params['path'] = path = os.path.expanduser(params['path']) # short-circuit for diff_peek @@ -226,7 +242,10 @@ def main(): module.exit_json(path=path, changed=True) 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': module.exit_json(path=path, changed=False) @@ -268,7 +287,8 @@ def main(): abs_src = src else: 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') if prev_state == 'absent': @@ -287,7 +307,14 @@ def main(): dolink(src, path, state, module) changed = True 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: module.fail_json(dest=path, src=src, msg='unexpected position reached')