diff options
author | Brian Coca <brian.coca+git@gmail.com> | 2016-03-25 08:13:44 -0700 |
---|---|---|
committer | Brian Coca <brian.coca+git@gmail.com> | 2016-03-30 18:56:33 -0700 |
commit | 4e8da8ed092be3b558cb8c31f3852377a98ad509 (patch) | |
tree | b1ec86624cd411214836d7acf59b97b8a1a4abd9 | |
parent | 918c11d18d0acbcc61b454eae1d567144d79a973 (diff) | |
download | ansible-4e8da8ed092be3b558cb8c31f3852377a98ad509.tar.gz |
moved 'path exists' function to shell
now it will work with powershell/winrm
-rw-r--r-- | lib/ansible/plugins/action/__init__.py | 7 | ||||
-rw-r--r-- | lib/ansible/plugins/action/script.py | 8 | ||||
-rw-r--r-- | lib/ansible/plugins/shell/__init__.py | 4 | ||||
-rw-r--r-- | lib/ansible/plugins/shell/powershell.py | 16 |
4 files changed, 29 insertions, 6 deletions
diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 5a0273128d..35bfe8e81e 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -92,6 +92,13 @@ class ActionBase(with_metaclass(ABCMeta, object)): ) return results + def _remote_file_exists(self, path): + cmd = self._connection._shell.exists(path) + result = self._low_level_execute_command(cmd=cmd, sudoable=True) + if result['rc'] == 0: + return True + return False + def _configure_module(self, module_name, module_args, task_vars=None): ''' Handles the loading and templating of the module code through the diff --git a/lib/ansible/plugins/action/script.py b/lib/ansible/plugins/action/script.py index 5b0f324dfc..e7eb03f9f0 100644 --- a/lib/ansible/plugins/action/script.py +++ b/lib/ansible/plugins/action/script.py @@ -46,9 +46,7 @@ class ActionModule(ActionBase): # do not run the command if the line contains creates=filename # and the filename already exists. This allows idempotence # of command executions. - res = self._execute_module(module_name='stat', module_args=dict(path=creates), task_vars=task_vars, tmp=tmp, persist_files=True) - stat = res.get('stat', None) - if stat and stat.get('exists', False): + if self._remote_file_exists(creates): return dict(skipped=True, msg=("skipped, since %s exists" % creates)) removes = self._task.args.get('removes') @@ -56,9 +54,7 @@ class ActionModule(ActionBase): # do not run the command if the line contains removes=filename # and the filename does not exist. This allows idempotence # of command executions. - res = self._execute_module(module_name='stat', module_args=dict(path=removes), task_vars=task_vars, tmp=tmp, persist_files=True) - stat = res.get('stat', None) - if stat and not stat.get('exists', False): + if self._remote_file_exists(removes): return dict(skipped=True, msg=("skipped, since %s does not exist" % removes)) # the script name is the first item in the raw params, so we split it diff --git a/lib/ansible/plugins/shell/__init__.py b/lib/ansible/plugins/shell/__init__.py index 50530c6b69..b3823f444b 100644 --- a/lib/ansible/plugins/shell/__init__.py +++ b/lib/ansible/plugins/shell/__init__.py @@ -63,6 +63,10 @@ class ShellBase(object): cmd += '-r ' return cmd + "%s %s" % (path, self._SHELL_REDIRECT_ALLNULL) + def exists(self, path): + cmd = ['test', '-e', pipes.quote(path)] + return ' '.join(cmd) + def mkdtemp(self, basefile=None, system=False, mode=None): if not basefile: basefile = 'ansible-tmp-%s-%s' % (time.time(), random.randint(0, 2**48)) diff --git a/lib/ansible/plugins/shell/powershell.py b/lib/ansible/plugins/shell/powershell.py index b06a046820..7cf2aded7f 100644 --- a/lib/ansible/plugins/shell/powershell.py +++ b/lib/ansible/plugins/shell/powershell.py @@ -94,6 +94,22 @@ class ShellModule(object): script = 'Write-Host "%s"' % self._escape(user_home_path) return self._encode_script(script) + def exists(self, path): + path = self._escape(self._unquote(path)) + script = ''' + If (Test-Path "%s") + { + $res = 0; + } + Else + { + $res = 1; + } + Write-Host "$res"; + Exit $res; + ''' % path + return self._encode_script(script) + def checksum(self, path, *args, **kwargs): path = self._escape(self._unquote(path)) script = ''' |