From 7cbe1bcf63dfeb5dd72d5ee8cc63ea9908e90777 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sun, 12 Dec 2021 09:15:33 +1300 Subject: [PATCH] hponcfg - revamped the module using ModuleHelper (#3840) * hponcfg - revamped the module using ModuleHelper * added changelog fragment * fixed imports * Update plugins/modules/remote_management/hpilo/hponcfg.py * fixed --- .../fragments/3840-hponcfg-mh-revamp.yaml | 2 + .../remote_management/hpilo/hponcfg.py | 42 +++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) create mode 100644 changelogs/fragments/3840-hponcfg-mh-revamp.yaml diff --git a/changelogs/fragments/3840-hponcfg-mh-revamp.yaml b/changelogs/fragments/3840-hponcfg-mh-revamp.yaml new file mode 100644 index 0000000000..ee4d063f68 --- /dev/null +++ b/changelogs/fragments/3840-hponcfg-mh-revamp.yaml @@ -0,0 +1,2 @@ +minor_changes: + - hponcfg - revamped module using ModuleHelper (https://github.com/ansible-collections/community.general/pull/3840). diff --git a/plugins/modules/remote_management/hpilo/hponcfg.py b/plugins/modules/remote_management/hpilo/hponcfg.py index 451e4b0613..b6aaeb5725 100644 --- a/plugins/modules/remote_management/hpilo/hponcfg.py +++ b/plugins/modules/remote_management/hpilo/hponcfg.py @@ -69,12 +69,13 @@ EXAMPLES = r''' executable: /opt/hp/tools/hponcfg ''' -from ansible.module_utils.basic import AnsibleModule +from ansible_collections.community.general.plugins.module_utils.module_helper import ( + CmdModuleHelper, ArgFormat +) -def main(): - - module = AnsibleModule( +class HPOnCfg(CmdModuleHelper): + module = dict( argument_spec=dict( src=dict(type='path', required=True, aliases=['path']), minfw=dict(type='str'), @@ -82,29 +83,24 @@ def main(): verbose=dict(default=False, type='bool'), ) ) + command_args_formats = dict( + src=dict(fmt=["-f", "{0}"]), + verbose=dict(fmt="-v", style=ArgFormat.BOOLEAN), + minfw=dict(fmt=["-m", "{0}"]), + ) + check_rc = True - # Consider every action a change (not idempotent yet!) - changed = True + def __init_module__(self): + self.command = self.vars.executable + # Consider every action a change (not idempotent yet!) + self.changed = True - src = module.params['src'] - minfw = module.params['minfw'] - executable = module.params['executable'] - verbose = module.params['verbose'] + def __run__(self): + self.run_command(params=['src', 'verbose', 'minfw']) - options = ' -f %s' % src - if verbose: - options += ' -v' - - if minfw: - options += ' -m %s' % minfw - - rc, stdout, stderr = module.run_command('%s %s' % (executable, options)) - - if rc != 0: - module.fail_json(rc=rc, msg="Failed to run hponcfg", stdout=stdout, stderr=stderr) - - module.exit_json(changed=changed, stdout=stdout, stderr=stderr) +def main(): + HPOnCfg.execute() if __name__ == '__main__':