Added hardlink option
Also a bit nicer now with failures Signed-off-by: Brian Coca <briancoca+dev@gmail.com>
This commit is contained in:
parent
0bd5fe9951
commit
9e6a584c3f
1 changed files with 22 additions and 11 deletions
|
@ -48,12 +48,12 @@ options:
|
||||||
- If C(directory), all immediate subdirectories will be created if they
|
- If C(directory), all immediate subdirectories will be created if they
|
||||||
do not exist. If C(file), the file will NOT be created if it does not
|
do not exist. If C(file), the file will NOT be created if it does not
|
||||||
exist, see the M(copy) or M(template) module if you want that behavior.
|
exist, see the M(copy) or M(template) module if you want that behavior.
|
||||||
If C(link), the symbolic link will be created or changed. If C(absent),
|
If C(link), the symbolic link will be created or changed. Use C(hard)
|
||||||
directories will be recursively deleted, and files or symlinks will be
|
for hardlinks. If C(absent), directories will be recursively deleted,
|
||||||
unlinked.
|
and files or symlinks will be unlinked.
|
||||||
required: false
|
required: false
|
||||||
default: file
|
default: file
|
||||||
choices: [ file, link, directory, absent ]
|
choices: [ file, link, directory, hard, absent ]
|
||||||
mode:
|
mode:
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
|
@ -130,6 +130,15 @@ requirements: [ ]
|
||||||
author: Michael DeHaan
|
author: Michael DeHaan
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
def dolink(src, path, state, module):
|
||||||
|
try:
|
||||||
|
if state == 'hard':
|
||||||
|
os.link(src,path)
|
||||||
|
else:
|
||||||
|
os.symlink(src, path)
|
||||||
|
except OSError, e:
|
||||||
|
module.fail_json(path=path, msg='Error while linking: %s' % str(e))
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
# FIXME: pass this around, should not use global
|
# FIXME: pass this around, should not use global
|
||||||
|
@ -137,7 +146,7 @@ def main():
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
state = dict(choices=['file','directory','link','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'),
|
||||||
diff_peek = dict(default=None),
|
diff_peek = dict(default=None),
|
||||||
|
@ -176,8 +185,8 @@ def main():
|
||||||
|
|
||||||
file_args = module.load_file_common_arguments(params)
|
file_args = module.load_file_common_arguments(params)
|
||||||
|
|
||||||
if state == 'link' and (src is None or path is None):
|
if state in ['link','hard'] and (src is None or path is None):
|
||||||
module.fail_json(msg='src and dest are required for "link" state')
|
module.fail_json(msg='src and dest are required for creating links')
|
||||||
elif path is None:
|
elif path is None:
|
||||||
module.fail_json(msg='path is required')
|
module.fail_json(msg='path is required')
|
||||||
|
|
||||||
|
@ -252,7 +261,7 @@ def main():
|
||||||
changed = module.set_file_attributes_if_different(tmp_file_args, changed)
|
changed = module.set_file_attributes_if_different(tmp_file_args, changed)
|
||||||
module.exit_json(path=path, changed=changed)
|
module.exit_json(path=path, changed=changed)
|
||||||
|
|
||||||
elif state == 'link':
|
elif state in ['link','hard']:
|
||||||
|
|
||||||
if os.path.isabs(src):
|
if os.path.isabs(src):
|
||||||
abs_src = src
|
abs_src = src
|
||||||
|
@ -264,7 +273,7 @@ def main():
|
||||||
if prev_state == 'absent':
|
if prev_state == 'absent':
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
module.exit_json(changed=True)
|
module.exit_json(changed=True)
|
||||||
os.symlink(src, path)
|
dolink(src, path, state, module)
|
||||||
changed = True
|
changed = True
|
||||||
elif prev_state == 'link':
|
elif prev_state == 'link':
|
||||||
old_src = os.readlink(path)
|
old_src = os.readlink(path)
|
||||||
|
@ -274,8 +283,10 @@ def main():
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
module.exit_json(changed=True)
|
module.exit_json(changed=True)
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
os.symlink(src, path)
|
dolink(src, path, state, module)
|
||||||
changed = True
|
changed = True
|
||||||
|
elif prev_state == 'file':
|
||||||
|
module.fail_json(dest=path, src=src, msg='Cannot link, file exists at destination')
|
||||||
else:
|
else:
|
||||||
module.fail_json(dest=path, src=src, msg='unexpected position reached')
|
module.fail_json(dest=path, src=src, msg='unexpected position reached')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue