/lib/ansible/plugins/__init__.py

https://github.com/debfx/ansible · Python · 79 lines · 35 code · 16 blank · 28 comment · 6 complexity · ab1df7c970bf968db1dc3d10388e63d6 MD5 · raw file

  1. # (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.com>
  2. # (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> and others
  3. # (c) 2017, Toshio Kuratomi <tkuratomi@ansible.com>
  4. #
  5. # This file is part of Ansible
  6. #
  7. # Ansible is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Ansible is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
  19. # Make coding more python3-ish
  20. from __future__ import (absolute_import, division, print_function)
  21. __metaclass__ = type
  22. from abc import ABCMeta
  23. from ansible import constants as C
  24. from ansible.module_utils.six import with_metaclass, string_types
  25. from ansible.utils.display import Display
  26. display = Display()
  27. # Global so that all instances of a PluginLoader will share the caches
  28. MODULE_CACHE = {}
  29. PATH_CACHE = {}
  30. PLUGIN_PATH_CACHE = {}
  31. def get_plugin_class(obj):
  32. if isinstance(obj, string_types):
  33. return obj.lower().replace('module', '')
  34. else:
  35. return obj.__class__.__name__.lower().replace('module', '')
  36. class AnsiblePlugin(with_metaclass(ABCMeta, object)):
  37. # allow extra passthrough parameters
  38. allow_extras = False
  39. def __init__(self):
  40. self._options = {}
  41. def get_option(self, option, hostvars=None):
  42. if option not in self._options:
  43. option_value = C.config.get_config_value(option, plugin_type=get_plugin_class(self), plugin_name=self._load_name, variables=hostvars)
  44. self.set_option(option, option_value)
  45. return self._options.get(option)
  46. def set_option(self, option, value):
  47. self._options[option] = value
  48. def set_options(self, task_keys=None, var_options=None, direct=None):
  49. '''
  50. Sets the _options attribute with the configuration/keyword information for this plugin
  51. :arg task_keys: Dict with playbook keywords that affect this option
  52. :arg var_options: Dict with either 'connection variables'
  53. :arg direct: Dict with 'direct assignment'
  54. '''
  55. self._options = C.config.get_plugin_options(get_plugin_class(self), self._load_name, keys=task_keys, variables=var_options, direct=direct)
  56. # allow extras/wildcards from vars that are not directly consumed in configuration
  57. # this is needed to support things like winrm that can have extended protocol options we don't direclty handle
  58. if self.allow_extras and var_options and '_extras' in var_options:
  59. self.set_option('_extras', var_options['_extras'])
  60. def _check_required(self):
  61. # FIXME: standarize required check based on config
  62. pass