Advertisement
alsastre

Full Action Module Example

Jan 19th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from ansible.plugins.action import ActionBase
  4. from ansible.errors import AnsibleFileNotFound
  5.  
  6.  
  7. class ActionModule(ActionBase):
  8.     '''Must have this name for ansible to detect it
  9.       and the name of the file must be the same than the module
  10.       we want to refer'''
  11.  
  12.     def run(self, tmp=None, task_vars=None):
  13.         module_args = self._task.args  # Obtaing module args
  14.         # List of keys in my args that are files
  15.         args_files = ["pfx_file", "pswd_file", "cer_file"]
  16.         for file in args_files:
  17.             try:
  18.                 module_args[file] = self._get_absolute_path(module_args[file])
  19.             except AnsibleFileNotFound as e:
  20.                 return dict(
  21.                     failed=True,
  22.                     msg="Couldn't find %s: %s - %s" % (file, module_args[file],
  23.                                                        str(e))
  24.                 )
  25.  
  26.         # Execute module with name pfx_deconstruct with its args modified
  27.         return self._execute_module('pfx_deconstruct', module_args=module_args,
  28.                                     task_vars=task_vars)
  29.  
  30.     def _get_absolute_path(self, path):
  31.         if path.startswith('/'):
  32.             return path
  33.  
  34.         if self._task._role is not None:
  35.             path = self._loader.path_dwim_relative(self._task._role._role_path,
  36.                                                    'files', path)
  37.         else:
  38.             path = self._loader.path_dwim_relative(self._loader.get_basedir(),
  39.                                                    'files', path)
  40.  
  41.         return path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement