diff --git a/files/file b/files/file index afffe4e9b99..88c3c4d3242 100644 --- a/files/file +++ b/files/file @@ -142,7 +142,7 @@ def main(): module = AnsibleModule( argument_spec = dict( - state = dict(choices=['file','directory','link','hard','touch','absent'], default='file'), + state = dict(choices=['file','directory','link','hard','touch','absent'], default=None), path = dict(aliases=['dest', 'name'], required=True), recurse = dict(default='no', type='bool'), force = dict(required=False,default=False,type='bool'), @@ -171,6 +171,30 @@ def main(): pass module.exit_json(path=path, changed=False, appears_binary=appears_binary) + prev_state = 'absent' + + if os.path.lexists(path): + if os.path.islink(path): + prev_state = 'link' + elif os.path.isdir(path): + prev_state = 'directory' + elif os.stat(path).st_nlink > 1: + prev_state = 'hard' + else: + # could be many other things, but defaulting to file + prev_state = 'file' + + if prev_state is not None and state is None: + if not params.get('src', None): + # idempotent exit if state is not specified + module.exit_json(path=path, changed=False) + else: + # src is defined, need to process other operations + state = prev_state + elif state is None: + # set default state to file + state = 'file' + # source is both the source of a symlink or an informational passing of the src for a template module # or copy module, even if this module never uses it, it is needed to key off some things @@ -190,19 +214,6 @@ def main(): changed = False - prev_state = 'absent' - - if os.path.lexists(path): - if os.path.islink(path): - prev_state = 'link' - elif os.path.isdir(path): - prev_state = 'directory' - elif os.stat(path).st_nlink > 1: - prev_state = 'hard' - else: - # could be many other things, but defaulting to file - prev_state = 'file' - recurse = params['recurse'] if recurse and state == 'file' and prev_state == 'directory':