mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
ModuleHelper module utils: delegates unknown attributes to AnsibleModule (#4600)
* ModuleHelper module util: delegates unknown attributes to AnsibleModule * added changelog fragment * delegate only a few selected attrs * Update plugins/module_utils/mh/base.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update changelogs/fragments/4600-mh-delegate.yaml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
f17d2669fa
commit
bca7f09b71
3 changed files with 17 additions and 2 deletions
2
changelogs/fragments/4600-mh-delegate.yaml
Normal file
2
changelogs/fragments/4600-mh-delegate.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- ModuleHelper module utils - ``ModuleHelperBase` now delegates the attributes ``check_mode``, ``get_bin_path``, ``warn``, and ``deprecate`` to the underlying ``AnsibleModule`` instance (https://github.com/ansible-collections/community.general/pull/4600).
|
|
@ -14,6 +14,9 @@ from ansible_collections.community.general.plugins.module_utils.mh.deco import m
|
||||||
class ModuleHelperBase(object):
|
class ModuleHelperBase(object):
|
||||||
module = None
|
module = None
|
||||||
ModuleHelperException = _MHE
|
ModuleHelperException = _MHE
|
||||||
|
_delegated_to_module = (
|
||||||
|
'check_mode', 'get_bin_path', 'warn', 'deprecate',
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self, module=None):
|
def __init__(self, module=None):
|
||||||
self._changed = False
|
self._changed = False
|
||||||
|
@ -24,6 +27,15 @@ class ModuleHelperBase(object):
|
||||||
if not isinstance(self.module, AnsibleModule):
|
if not isinstance(self.module, AnsibleModule):
|
||||||
self.module = AnsibleModule(**self.module)
|
self.module = AnsibleModule(**self.module)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def diff_mode(self):
|
||||||
|
return self.module._diff
|
||||||
|
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
if attr in self._delegated_to_module:
|
||||||
|
return getattr(self.module, attr)
|
||||||
|
raise AttributeError("ModuleHelperBase has no attribute '%s'" % (attr, ))
|
||||||
|
|
||||||
def __init_module__(self):
|
def __init_module__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,8 @@ class ModuleHelper(DeprecateAttrsMixin, VarsMixin, DependencyMixin, ModuleHelper
|
||||||
version="6.0.0",
|
version="6.0.0",
|
||||||
collection_name="community.general",
|
collection_name="community.general",
|
||||||
target=ModuleHelper,
|
target=ModuleHelper,
|
||||||
module=self.module)
|
module=self.module,
|
||||||
|
)
|
||||||
|
|
||||||
def update_output(self, **kwargs):
|
def update_output(self, **kwargs):
|
||||||
self.update_vars(meta={"output": True}, **kwargs)
|
self.update_vars(meta={"output": True}, **kwargs)
|
||||||
|
@ -65,7 +66,7 @@ class ModuleHelper(DeprecateAttrsMixin, VarsMixin, DependencyMixin, ModuleHelper
|
||||||
facts = self.vars.facts()
|
facts = self.vars.facts()
|
||||||
if facts is not None:
|
if facts is not None:
|
||||||
result['ansible_facts'] = {self.facts_name: facts}
|
result['ansible_facts'] = {self.facts_name: facts}
|
||||||
if self.module._diff:
|
if self.diff_mode:
|
||||||
diff = result.get('diff', {})
|
diff = result.get('diff', {})
|
||||||
vars_diff = self.vars.diff() or {}
|
vars_diff = self.vars.diff() or {}
|
||||||
result['diff'] = dict_merge(dict(diff), vars_diff)
|
result['diff'] = dict_merge(dict(diff), vars_diff)
|
||||||
|
|
Loading…
Reference in a new issue