summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coca <brian.coca+git@gmail.com>2016-03-25 08:13:44 -0700
committerBrian Coca <brian.coca+git@gmail.com>2016-03-25 12:56:48 -0700
commit28d20dbe53bffd666005e60d6687d801f0bf0f75 (patch)
tree334e2fc0eaf5178ffc1452c2e75c87fe6539c239
parent81d9066ee1e6070b4bb0d2ab74773f2e810e1bc5 (diff)
downloadansible-28d20dbe53bffd666005e60d6687d801f0bf0f75.tar.gz
moved 'path exists' function to shell
now it will work with powershell/winrm
-rw-r--r--lib/ansible/plugins/action/__init__.py7
-rw-r--r--lib/ansible/plugins/action/script.py10
-rw-r--r--lib/ansible/plugins/shell/__init__.py4
-rw-r--r--lib/ansible/plugins/shell/powershell.py16
4 files changed, 29 insertions, 8 deletions
diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py
index 2ba0650de3..2bb5b7546e 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 c16fc227c4..d8a9ebd5be 100644
--- a/lib/ansible/plugins/action/script.py
+++ b/lib/ansible/plugins/action/script.py
@@ -26,12 +26,6 @@ from ansible.plugins.action import ActionBase
class ActionModule(ActionBase):
TRANSFERS_FILES = True
- def _get_remote_raw_stat(self, path):
- cmd = ['test', '-e', path]
- result = self._low_level_execute_command(cmd=' '.join(cmd), sudoable=True)
- if result['rc'] == 0:
- return True
- return False
def run(self, tmp=None, task_vars=None):
''' handler for file transfer operations '''
@@ -54,7 +48,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.
- if self._get_remote_raw_stat(creates):
+ if self._remote_file_exists(creates):
return dict(skipped=True, msg=("skipped, since %s exists" % creates))
removes = self._task.args.get('removes')
@@ -62,7 +56,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.
- if self._get_remote_raw_stat(removes):
+ 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 daa011e3da..c3e843f90b 100644
--- a/lib/ansible/plugins/shell/__init__.py
+++ b/lib/ansible/plugins/shell/__init__.py
@@ -94,6 +94,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 569df09d44..d72361e431 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 = '''