From 4f262779cd93ca358c55cc302a193d1af86e32fe 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 --- files/file | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/files/file b/files/file index bef175873e7..4b59b652c7d 100644 --- a/files/file +++ b/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 ecd8b5d0640a9301a6609fca2d92322d024d7474 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 --- files/file | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/files/file b/files/file index 4b59b652c7d..7f789242df2 100644 --- a/files/file +++ b/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 3f74dd65926aa1ae44a95d5e06c3aea07ca0f133 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 --- files/file | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/files/file b/files/file index 7f789242df2..ac766839f16 100644 --- a/files/file +++ b/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():