From 3ea9174ed7f98cce5b627b5025326b47192c69b8 Mon Sep 17 00:00:00 2001
From: Michael DeHaan <michael.dehaan@gmail.com>
Date: Wed, 14 Mar 2012 19:05:19 -0400
Subject: [PATCH] Split argsfile handling into subfunction, attempt to apply
 argsfile logic to setup

---
 lib/ansible/runner.py | 32 ++++++++++++++++++++------------
 library/setup         |  8 +++++++-
 2 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py
index 0701989967c..8680cb5bb26 100755
--- a/lib/ansible/runner.py
+++ b/lib/ansible/runner.py
@@ -35,6 +35,7 @@ import jinja2
 import time
 import traceback
 import tempfile
+import StringIO
 
 # FIXME: stop importing *, use as utils/errors
 from ansible.utils import *
@@ -227,7 +228,6 @@ class Runner(object):
     def _transfer_file(self, conn, source, dest):
         ''' transfers a remote file '''
 
-        self.remote_log(conn, 'COPY remote:%s local:%s' % (source, dest))
         conn.put_file(source, dest)
 
     # *****************************************************
@@ -244,6 +244,23 @@ class Runner(object):
 
     # *****************************************************
 
+    def _transfer_argsfile(self, conn, tmp, args_str):
+        ''' 
+        transfer arguments as a single file to be fed to the module.
+        this is to avoid various shell things being eaten by SSH
+        '''
+        args_fd, args_file = tempfile.mkstemp()
+        args_fo = os.fdopen(args_fd, 'w')
+        args_fo.write(args_str)
+        args_fo.flush()
+        args_fo.close()
+        args_remote = os.path.join(tmp, 'arguments')
+        self._transfer_file(conn, args_file, 'arguments')
+        os.unlink(args_file)
+        return args_remote
+
+    # *****************************************************
+
     def _execute_module(self, conn, tmp, remote_module_path, module_args):
         ''' 
         runs a module that has already been transferred, but first
@@ -268,17 +285,8 @@ class Runner(object):
         template = jinja2.Template(args)
         args = template.render(inject_vars)
 
-        # make a tempfile for the args and stuff the args into the file
-        argsfd,argsfile = tempfile.mkstemp()
-        argsfo = os.fdopen(argsfd, 'w')
-        argsfo.write(args)
-        argsfo.flush()
-        argsfo.close()
-        args_rem = tmp + 'argsfile'
-        self.remote_log(conn, "args: %s" % args)
-        self._transfer_file(conn,argsfile, args_rem)
-        os.unlink(argsfile)
-        cmd = "%s %s" % (remote_module_path, args_rem)
+        argsfile = self._transfer_argsfile(conn, tmp, args)
+        cmd = "%s %s" % (remote_module_path, 'arguments')
         result = self._exec_command(conn, cmd)
         return result
 
diff --git a/library/setup b/library/setup
index 30e16032381..c10547e553c 100755
--- a/library/setup
+++ b/library/setup
@@ -31,7 +31,13 @@ except ImportError:
 
 # load config & template variables
 
-input_data = sys.argv[1:]
+if len(sys.argv) == 1:
+    sys.exit(1)
+argfile = sys.argv[1]
+if not os.path.exists(argfile):
+    sys.exit(1)
+input_data = open(argfile, 'r').read()
+
 new_options = dict([ x.split('=') for x in input_data ])
 ansible_file = new_options.get('metadata', DEFAULT_ANSIBLE_SETUP)
 ansible_dir = os.path.dirname(ansible_file)