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__':