From 2b4688a0bd00b31be92ce9094da5f1dc74af5b99 Mon Sep 17 00:00:00 2001 From: Mike Grozak Date: Thu, 6 Feb 2014 17:20:32 +0100 Subject: [PATCH 1/3] Started the development of the 'chdir' functionality for file module - creation symlinks without the base directory prefix --- library/files/file | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/files/file b/library/files/file index bef175873e..4b59b652c7 100644 --- a/library/files/file +++ b/library/files/file @@ -149,6 +149,7 @@ def main(): original_basename = dict(required=False), # Internal use only, for recursive ops recurse = dict(default='no', type='bool'), force = dict(required=False,default=False,type='bool'), + chdir = dict(required=False,default=False), diff_peek = dict(default=None), validate = dict(required=False, default=None), ), @@ -159,8 +160,16 @@ def main(): params = module.params state = params['state'] force = params['force'] + + params['chdir'] = chdir = os.path.expanduser(params['chdir']) params['path'] = path = os.path.expanduser(params['path']) + if state == 'link' and chdir is not None and os.isdir(chdir): + os.chdir(chdir) + # catch exception permission deny, no directory, etc + # save current working directory, chdir to it at the end of the module + # or before any escape + # short-circuit for diff_peek if params.get('diff_peek', None) is not None: appears_binary = False From d3b544e8427e8f2410e6d4fb63518b4db32137f4 Mon Sep 17 00:00:00 2001 From: Mike Grozak Date: Fri, 7 Feb 2014 14:13:41 +0100 Subject: [PATCH 2/3] Found a way how to have symlinks towards files without absolute paths in prefixes; fixed incorrect force setting on the hard links, which can not point to non-existing files --- library/files/file | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/library/files/file b/library/files/file index 4b59b652c7..7f789242df 100644 --- a/library/files/file +++ b/library/files/file @@ -149,7 +149,6 @@ def main(): original_basename = dict(required=False), # Internal use only, for recursive ops recurse = dict(default='no', type='bool'), force = dict(required=False,default=False,type='bool'), - chdir = dict(required=False,default=False), diff_peek = dict(default=None), validate = dict(required=False, default=None), ), @@ -161,15 +160,8 @@ def main(): state = params['state'] force = params['force'] - params['chdir'] = chdir = os.path.expanduser(params['chdir']) params['path'] = path = os.path.expanduser(params['path']) - if state == 'link' and chdir is not None and os.isdir(chdir): - os.chdir(chdir) - # catch exception permission deny, no directory, etc - # save current working directory, chdir to it at the end of the module - # or before any escape - # short-circuit for diff_peek if params.get('diff_peek', None) is not None: appears_binary = False @@ -298,7 +290,7 @@ def main(): else: module.fail_json(msg="absolute paths are required") - if not os.path.exists(abs_src) and not force: + if not os.path.exists(abs_src): module.fail_json(path=path, src=src, msg='src file does not exist') if prev_state == 'absent': From f386e137fe03aff6242018dde5a297ce2f86db07 Mon Sep 17 00:00:00 2001 From: Mike Grozak Date: Fri, 7 Feb 2014 14:29:20 +0100 Subject: [PATCH 3/3] Updated the force parameter description; added one example how to create the symlinks pointing to non-absolute paths --- library/files/file | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/library/files/file b/library/files/file index 7f789242df..ac766839f1 100644 --- a/library/files/file +++ b/library/files/file @@ -123,8 +123,7 @@ options: 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 is a file (so, we need to unlink the + - 'force the creation of the symlinks when the destination exists and is a file (so, we need to unlink the "path" file and create symlink to the "src" file in place of it).' notes: - See also M(copy), M(template), M(assemble) @@ -135,6 +134,10 @@ author: Michael DeHaan EXAMPLES = ''' - file: path=/etc/foo.conf owner=foo group=foo mode=0644 - file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link +- file: path=/tmp/{{ item.path }} dest={{ item.dest }} state=link + with_items: + - { path: 'x', dest: 'y' } + - { path: 'z', dest: 'k' } ''' def main():