1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/remote_management/ipmi/ipmi_power.py
patchback[bot] 6e51690a95
Support IPMI encryption key parameter in ipmi_boot (#3702) (#3770)
* Support IPMI encryption key parameter in ipmi_boot

* Support py2 on hex parsing, error handling

Change parsing hex string to support python2 and add error handling to it based on feedback.

* Don't explicitly set required to false

* Add version_added to key arg

* Add changelog fragment

* Add IPMI encryption key arg to ipmi_power

* Fix the formatting of changelog fragment

(cherry picked from commit 4013d0c9ca)

Co-authored-by: bluikko <14869000+bluikko@users.noreply.github.com>
2021-11-22 13:49:54 +01:00

153 lines
4 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: ipmi_power
short_description: Power management for machine
description:
- Use this module for power management
options:
name:
description:
- Hostname or ip address of the BMC.
required: true
type: str
port:
description:
- Remote RMCP port.
default: 623
type: int
user:
description:
- Username to use to connect to the BMC.
required: true
type: str
password:
description:
- Password to connect to the BMC.
required: true
type: str
key:
description:
- Encryption key to connect to the BMC in hex format.
required: false
type: str
version_added: 4.1.0
state:
description:
- Whether to ensure that the machine in desired state.
- "The choices for state are:
- on -- Request system turn on
- off -- Request system turn off without waiting for OS to shutdown
- shutdown -- Have system request OS proper shutdown
- reset -- Request system reset without waiting for OS
- boot -- If system is off, then 'on', else 'reset'"
choices: ['on', 'off', shutdown, reset, boot]
required: true
type: str
timeout:
description:
- Maximum number of seconds before interrupt request.
default: 300
type: int
requirements:
- "python >= 2.6"
- pyghmi
author: "Bulat Gaifullin (@bgaifullin) <gaifullinbf@gmail.com>"
'''
RETURN = '''
powerstate:
description: The current power state of the machine.
returned: success
type: str
sample: on
'''
EXAMPLES = '''
- name: Ensure machine is powered on
community.general.ipmi_power:
name: test.testdomain.com
user: admin
password: password
state: on
'''
import traceback
import binascii
PYGHMI_IMP_ERR = None
try:
from pyghmi.ipmi import command
except ImportError:
PYGHMI_IMP_ERR = traceback.format_exc()
command = None
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True),
port=dict(default=623, type='int'),
state=dict(required=True, choices=['on', 'off', 'shutdown', 'reset', 'boot']),
user=dict(required=True, no_log=True),
password=dict(required=True, no_log=True),
key=dict(type='str', no_log=True),
timeout=dict(default=300, type='int'),
),
supports_check_mode=True,
)
if command is None:
module.fail_json(msg=missing_required_lib('pyghmi'), exception=PYGHMI_IMP_ERR)
name = module.params['name']
port = module.params['port']
user = module.params['user']
password = module.params['password']
state = module.params['state']
timeout = module.params['timeout']
try:
if module.params['key']:
key = binascii.unhexlify(module.params['key'])
else:
key = None
except Exception as e:
module.fail_json(msg="Unable to convert 'key' from hex string.")
# --- run command ---
try:
ipmi_cmd = command.Command(
bmc=name, userid=user, password=password, port=port, kg=key
)
module.debug('ipmi instantiated - name: "%s"' % name)
current = ipmi_cmd.get_power()
if current['powerstate'] != state:
response = {'powerstate': state} if module.check_mode else ipmi_cmd.set_power(state, wait=timeout)
changed = True
else:
response = current
changed = False
if 'error' in response:
module.fail_json(msg=response['error'])
module.exit_json(changed=changed, **response)
except Exception as e:
module.fail_json(msg=str(e))
if __name__ == '__main__':
main()