Guest User

action_plugins/normal.py

a guest
Feb 20th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. '''
  2. A wrapper around ansible modules that mangles the arguments
  3. '''
  4. import os
  5.  
  6. from ansible import utils
  7. from ansible.callbacks import vv
  8.  
  9. def _find_module(name, suffix='.yml'):
  10.     '''find a module with a different suffix'''
  11.     dirs = utils.plugins.module_finder.print_paths().split(os.pathsep)
  12.     for d in dirs:
  13.         path = os.path.join(d, "%s%s" % (name, suffix))
  14.         if os.path.exists(path):
  15.             return path
  16.  
  17. class ActionModule(object):
  18.  
  19.     def __init__(self, runner):
  20.         self.runner = runner
  21.  
  22.     def run(self, conn, tmp, module_name, module_args, inject):
  23.         '''use a module wrapper'''
  24.  
  25.         path = _find_module(module_name)
  26.         if path:
  27.             vv('using module wrapper: %s' % path, host=conn.host)
  28.            
  29.             # we may want to use args in the wrapper
  30.             args = utils.parse_kv(module_args)
  31.             inject.update(args)
  32.  
  33.             data = utils.parse_yaml_from_file(path)
  34.             data = utils.template_ds(self.runner.basedir, data, inject)
  35.  
  36.             # apply defaults
  37.             if 'defaults' in data:
  38.                 args = dict(data['defaults'].items() + args.items())
  39.  
  40.             module_args = " ".join("%s='%s'" % i for i in args.items())
  41.  
  42.             # possibly change the name
  43.             module_name = data.get('module', module_name)
  44.  
  45.         ### upstream normal plugin follows ###
  46.  
  47.         # shell and command are the same module
  48.         if module_name == 'shell':
  49.             module_name = 'command'
  50.             module_args += " #USE_SHELL"
  51.  
  52.         vv("REMOTE_MODULE %s %s" % (module_name, module_args), host=conn.host)
  53.         return self.runner._execute_module(conn, tmp, module_name, module_args, inject=inject)
Advertisement
Add Comment
Please, Sign In to add comment