From bca7f09b715ef6de87def4a9dcce2440aa91eb00 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Mon, 9 May 2022 17:10:49 +1200 Subject: [PATCH] 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 * Update changelogs/fragments/4600-mh-delegate.yaml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein --- changelogs/fragments/4600-mh-delegate.yaml | 2 ++ plugins/module_utils/mh/base.py | 12 ++++++++++++ plugins/module_utils/mh/module_helper.py | 5 +++-- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/4600-mh-delegate.yaml diff --git a/changelogs/fragments/4600-mh-delegate.yaml b/changelogs/fragments/4600-mh-delegate.yaml new file mode 100644 index 0000000000..dadaefb0d7 --- /dev/null +++ b/changelogs/fragments/4600-mh-delegate.yaml @@ -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). diff --git a/plugins/module_utils/mh/base.py b/plugins/module_utils/mh/base.py index 90c228b306..3d311de865 100644 --- a/plugins/module_utils/mh/base.py +++ b/plugins/module_utils/mh/base.py @@ -14,6 +14,9 @@ from ansible_collections.community.general.plugins.module_utils.mh.deco import m class ModuleHelperBase(object): module = None ModuleHelperException = _MHE + _delegated_to_module = ( + 'check_mode', 'get_bin_path', 'warn', 'deprecate', + ) def __init__(self, module=None): self._changed = False @@ -24,6 +27,15 @@ class ModuleHelperBase(object): if not isinstance(self.module, AnsibleModule): 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): pass diff --git a/plugins/module_utils/mh/module_helper.py b/plugins/module_utils/mh/module_helper.py index 65842fd74e..71731411e0 100644 --- a/plugins/module_utils/mh/module_helper.py +++ b/plugins/module_utils/mh/module_helper.py @@ -44,7 +44,8 @@ class ModuleHelper(DeprecateAttrsMixin, VarsMixin, DependencyMixin, ModuleHelper version="6.0.0", collection_name="community.general", target=ModuleHelper, - module=self.module) + module=self.module, + ) def update_output(self, **kwargs): self.update_vars(meta={"output": True}, **kwargs) @@ -65,7 +66,7 @@ class ModuleHelper(DeprecateAttrsMixin, VarsMixin, DependencyMixin, ModuleHelper facts = self.vars.facts() if facts is not None: result['ansible_facts'] = {self.facts_name: facts} - if self.module._diff: + if self.diff_mode: diff = result.get('diff', {}) vars_diff = self.vars.diff() or {} result['diff'] = dict_merge(dict(diff), vars_diff)