From 4716b779f687fc5fc060aaa7e12cf8c33b0668c9 Mon Sep 17 00:00:00 2001 From: Mike Grozak Date: Thu, 30 May 2013 12:53:24 +0200 Subject: [PATCH 1/4] added force parameter to symlink invocation, in order to force the creation in case of already existing destination with the type of 'file' --- files/file | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/files/file b/files/file index 22716935c8b..341afdc35c0 100644 --- a/files/file +++ b/files/file @@ -150,6 +150,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 +160,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 +228,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) @@ -287,7 +292,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') From ee813d3bd73c66f626ea0b51519ed77037d00468 Mon Sep 17 00:00:00 2001 From: Mike Grozak Date: Thu, 30 May 2013 13:05:10 +0200 Subject: [PATCH 2/4] added possibility to force symlinks to none-existent destination (which might appear later) --- files/file | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/files/file b/files/file index 341afdc35c0..ab0ab6ec1d2 100644 --- a/files/file +++ b/files/file @@ -273,7 +273,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': From 58618eef8d9510b7eea5b881f6a2e467154c351f Mon Sep 17 00:00:00 2001 From: Mike Grozak Date: Tue, 4 Jun 2013 12:06:08 +0200 Subject: [PATCH 3/4] Updated documentaion for the file module --- files/file | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/files/file b/files/file index ab0ab6ec1d2..9538853c241 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" + choises: [ "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: [ ] From da2fb08d474b46fd1b657bca477f73ff12954770 Mon Sep 17 00:00:00 2001 From: Mike Grozak Date: Wed, 12 Jun 2013 07:36:43 +0200 Subject: [PATCH 4/4] Fixed typo --- files/file | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/file b/files/file index 9538853c241..75798268a7b 100644 --- a/files/file +++ b/files/file @@ -123,7 +123,7 @@ options: force: required: false default: "no" - choises: [ "yes", "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