Browse code

Avoid creating temporary directory for some action plugins

This ensures we don't litter remote systems with temporary directories
that don't get cleaned up, as well as speeds things up from not having
to touch every node.

Daniel Hokka Zakrisson authored on 2012/11/29 00:22:35
Showing 6 changed files
... ...
@@ -328,13 +328,6 @@ class Runner(object):
328 328
     def _executor_internal_inner(self, host, module_name, module_args, inject, port, is_chained=False):
329 329
         ''' decides how to invoke a module '''
330 330
 
331
-        # special non-user/non-fact variables:
332
-        # 'groups' variable is a list of host name in each group
333
-        # 'hostvars' variable contains variables for each host name
334
-        #  ... and is set elsewhere
335
-        # 'inventory_hostname' is also set elsewhere
336
-        inject['groups'] = self.inventory.groups_list()
337
-
338 331
         # allow module args to work as a dictionary
339 332
         # though it is usually a string
340 333
         new_args = ""
... ...
@@ -343,6 +336,18 @@ class Runner(object):
343 343
                 new_args = new_args + "%s='%s' " % (k,v)
344 344
             module_args = new_args
345 345
 
346
+        module_name = utils.template(self.basedir, module_name, inject)
347
+        module_args = utils.template(self.basedir, module_args, inject, expand_lists=True)
348
+
349
+        if module_name in utils.plugins.action_loader:
350
+            if self.background != 0:
351
+                raise errors.AnsibleError("async mode is not supported with the %s module" % module_name)
352
+            handler = utils.plugins.action_loader.get(module_name, self)
353
+        elif self.background == 0:
354
+            handler = utils.plugins.action_loader.get('normal', self)
355
+        else:
356
+            handler = utils.plugins.action_loader.get('async', self)
357
+
346 358
         conditional = utils.template(self.basedir, self.conditional, inject)
347 359
         if not utils.check_conditional(conditional):
348 360
             result = utils.jsonify(dict(skipped=True))
... ...
@@ -396,24 +401,12 @@ class Runner(object):
396 396
             result = dict(failed=True, msg="FAILED: %s" % str(e))
397 397
             return ReturnData(host=host, comm_ok=False, result=result)
398 398
 
399
-        module_name = utils.template(self.basedir, module_name, inject)
400
-        module_args = utils.template(self.basedir, module_args, inject, expand_lists=True)
401
-
402 399
         tmp = ''
403
-        if self.module_name != 'raw':
400
+        # all modules get a tempdir, action plugins get one unless they have NEEDS_TMPPATH set to False
401
+        if getattr(handler, 'NEEDS_TMPPATH', True):
404 402
             tmp = self._make_tmp_path(conn)
405
-        result = None
406 403
 
407
-        if module_name in utils.plugins.action_loader:
408
-            if self.background != 0:
409
-                raise errors.AnsibleError("async mode is not supported with the %s module" % module_name)
410
-            handler = utils.plugins.action_loader.get(module_name, self)
411
-            result = handler.run(conn, tmp, module_name, module_args, inject)
412
-        else:
413
-            if self.background == 0:
414
-                result = utils.plugins.action_loader.get('normal', self).run(conn, tmp, module_name, module_args, inject)
415
-            else:
416
-                result = utils.plugins.action_loader.get('async', self).run(conn, tmp, module_name, module_args, inject)
404
+        result = handler.run(conn, tmp, module_name, module_args, inject)
417 405
 
418 406
         conn.close()
419 407
 
... ...
@@ -29,6 +29,7 @@ class ActionModule(object):
29 29
 
30 30
     ### We need to be able to modify the inventory
31 31
     BYPASS_HOST_LOOP = True
32
+    NEEDS_TMPPATH = False
32 33
 
33 34
     def __init__(self, runner):
34 35
         self.runner = runner
... ...
@@ -23,6 +23,8 @@ from ansible.runner.return_data import ReturnData
23 23
 class ActionModule(object):
24 24
     ''' Print statements during execution '''
25 25
 
26
+    NEEDS_TMPPATH = False
27
+
26 28
     def __init__(self, runner):
27 29
         self.runner = runner
28 30
 
... ...
@@ -23,6 +23,8 @@ from ansible.runner.return_data import ReturnData
23 23
 class ActionModule(object):
24 24
     ''' Fail with custom message '''
25 25
 
26
+    NEEDS_TMPPATH = False
27
+
26 28
     def __init__(self, runner):
27 29
         self.runner = runner
28 30
 
... ...
@@ -27,6 +27,7 @@ class ActionModule(object):
27 27
 
28 28
     ### We need to be able to modify the inventory
29 29
     BYPASS_HOST_LOOP = True
30
+    NEEDS_TMPPATH = False
30 31
 
31 32
     def __init__(self, runner):
32 33
         self.runner = runner
... ...
@@ -28,6 +28,7 @@ from ansible import module_common
28 28
 from ansible.runner.return_data import ReturnData
29 29
 
30 30
 class ActionModule(object):
31
+    NEEDS_TMPPATH = False
31 32
 
32 33
     def __init__(self, runner):
33 34
         self.runner = runner