From 76f3351b02dbdb2795c10d165de4aa0ba7fc1597 Mon Sep 17 00:00:00 2001
From: Daniel Hokka Zakrisson <daniel@hozac.com>
Date: Thu, 7 Mar 2013 11:05:17 +0100
Subject: [PATCH] Move complex_args templating to be with module_args

Keeps every action plugin from having to do the same thing.
---
 lib/ansible/runner/__init__.py                | 10 +++++-----
 lib/ansible/runner/action_plugins/copy.py     |  2 --
 lib/ansible/runner/action_plugins/normal.py   |  1 -
 lib/ansible/runner/action_plugins/template.py |  2 --
 4 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py
index c9a4ae28ae4..a4f74c25d78 100644
--- a/lib/ansible/runner/__init__.py
+++ b/lib/ansible/runner/__init__.py
@@ -351,7 +351,7 @@ class Runner(object):
         # logic to decide how to run things depends on whether with_items is used
 
         if items is None:
-            return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port)
+            return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=self.complex_args)
         elif len(items) > 0:
             # executing using with_items, so make multiple calls
             # TODO: refactor
@@ -362,7 +362,7 @@ class Runner(object):
             results = []
             for x in items:
                 inject['item'] = x
-                result = self._executor_internal_inner(host, self.module_name, self.module_args, inject, port)
+                result = self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=self.complex_args)
                 results.append(result.result)
                 if result.comm_ok == False:
                     all_comm_ok = False
@@ -387,7 +387,7 @@ class Runner(object):
 
     # *****************************************************
 
-    def _executor_internal_inner(self, host, module_name, module_args, inject, port, is_chained=False):
+    def _executor_internal_inner(self, host, module_name, module_args, inject, port, is_chained=False, complex_args=None):
         ''' decides how to invoke a module '''
 
 
@@ -401,7 +401,7 @@ class Runner(object):
 
         module_name = utils.template(self.basedir, module_name, inject)
         module_args = utils.template(self.basedir, module_args, inject)
-        
+        complex_args = utils.template(self.basedir, complex_args, inject)
 
         if module_name in utils.plugins.action_loader:
             if self.background != 0:
@@ -479,7 +479,7 @@ class Runner(object):
         if getattr(handler, 'NEEDS_TMPPATH', True):
             tmp = self._make_tmp_path(conn)
         
-        result = handler.run(conn, tmp, module_name, module_args, inject, self.complex_args)
+        result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
 
         conn.close()
 
diff --git a/lib/ansible/runner/action_plugins/copy.py b/lib/ansible/runner/action_plugins/copy.py
index f905a24745d..60e29ff885b 100644
--- a/lib/ansible/runner/action_plugins/copy.py
+++ b/lib/ansible/runner/action_plugins/copy.py
@@ -36,10 +36,8 @@ class ActionModule(object):
         if complex_args:
             options.update(complex_args)
         options.update(utils.parse_kv(module_args))
-        options = utils.template(self.runner.basedir, options, inject)
         source  = options.get('src', None)
         dest    = options.get('dest', None)
-        module_args = self.runner._complex_args_hack(options, '')
 
         if (source is None and not 'first_available_file' in inject) or dest is None:
             result=dict(failed=True, msg="src and dest are required")
diff --git a/lib/ansible/runner/action_plugins/normal.py b/lib/ansible/runner/action_plugins/normal.py
index 51d7bb0ffe0..a54090625a7 100644
--- a/lib/ansible/runner/action_plugins/normal.py
+++ b/lib/ansible/runner/action_plugins/normal.py
@@ -36,7 +36,6 @@ class ActionModule(object):
     def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
         ''' transfer & execute a module that is not 'copy' or 'template' '''
 
-        complex_args = utils.template(self.runner.basedir, complex_args, inject)
         module_args = self.runner._complex_args_hack(complex_args, module_args)
 
         if self.runner.check:
diff --git a/lib/ansible/runner/action_plugins/template.py b/lib/ansible/runner/action_plugins/template.py
index 65881be3b93..7c137839c31 100644
--- a/lib/ansible/runner/action_plugins/template.py
+++ b/lib/ansible/runner/action_plugins/template.py
@@ -41,8 +41,6 @@ class ActionModule(object):
         if complex_args:
             options.update(complex_args)
         options.update(utils.parse_kv(module_args))
-        options = utils.template(self.runner.basedir, options, inject)
-        module_args = self.runner._complex_args_hack(options, '')
 
         source   = options.get('src', None)
         dest     = options.get('dest', None)