2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
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
|
2023-02-20 17:28:13 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Hostname or ip address of the BMC.
|
|
|
|
required: true
|
2021-02-10 07:19:32 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
port:
|
|
|
|
description:
|
|
|
|
- Remote RMCP port.
|
|
|
|
default: 623
|
2021-02-10 07:19:32 +01:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- Username to use to connect to the BMC.
|
|
|
|
required: true
|
2021-02-10 07:19:32 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- Password to connect to the BMC.
|
|
|
|
required: true
|
2021-02-10 07:19:32 +01:00
|
|
|
type: str
|
2021-11-22 07:18:13 +01:00
|
|
|
key:
|
|
|
|
description:
|
|
|
|
- Encryption key to connect to the BMC in hex format.
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
version_added: 4.1.0
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether to ensure that the machine in desired state.
|
2020-11-27 21:16:47 +01:00
|
|
|
- "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'"
|
2022-01-08 16:03:51 +01:00
|
|
|
- Either this option or I(machine) is required.
|
2020-11-27 21:16:47 +01:00
|
|
|
choices: ['on', 'off', shutdown, reset, boot]
|
2021-02-10 07:19:32 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- Maximum number of seconds before interrupt request.
|
|
|
|
default: 300
|
2021-02-10 07:19:32 +01:00
|
|
|
type: int
|
2022-01-08 16:03:51 +01:00
|
|
|
machine:
|
|
|
|
description:
|
|
|
|
- Provide a list of the remote target address for the bridge IPMI request,
|
|
|
|
and the power status.
|
|
|
|
- Either this option or I(state) is required.
|
|
|
|
required: false
|
|
|
|
type: list
|
|
|
|
elements: dict
|
|
|
|
version_added: 4.3.0
|
|
|
|
suboptions:
|
|
|
|
targetAddress:
|
|
|
|
description:
|
|
|
|
- Remote target address for the bridge IPMI request.
|
|
|
|
type: int
|
|
|
|
required: true
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether to ensure that the machine specified by I(targetAddress) in desired state.
|
|
|
|
- If this option is not set, the power state is set by I(state).
|
|
|
|
- If both this option and I(state) are set, this option takes precedence over I(state).
|
|
|
|
choices: ['on', 'off', shutdown, reset, boot]
|
|
|
|
type: str
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- pyghmi
|
|
|
|
author: "Bulat Gaifullin (@bgaifullin) <gaifullinbf@gmail.com>"
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
powerstate:
|
|
|
|
description: The current power state of the machine.
|
2022-01-08 16:03:51 +01:00
|
|
|
returned: success and I(machine) is not provided
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
sample: on
|
2022-01-08 16:03:51 +01:00
|
|
|
status:
|
|
|
|
description: The current power state of the machine when the machine option is set.
|
|
|
|
returned: success and I(machine) is provided
|
|
|
|
type: list
|
|
|
|
elements: dict
|
|
|
|
version_added: 4.3.0
|
|
|
|
contains:
|
|
|
|
powerstate:
|
|
|
|
description: The current power state of the machine specified by I(targetAddress).
|
|
|
|
type: str
|
|
|
|
targetAddress:
|
|
|
|
description: The remote target address.
|
|
|
|
type: int
|
|
|
|
sample: [
|
|
|
|
{
|
|
|
|
"powerstate": "on",
|
|
|
|
"targetAddress": 48,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"powerstate": "on",
|
|
|
|
"targetAddress": 50,
|
|
|
|
},
|
|
|
|
]
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Ensure machine is powered on
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.ipmi_power:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: test.testdomain.com
|
|
|
|
user: admin
|
|
|
|
password: password
|
|
|
|
state: on
|
2022-01-08 16:03:51 +01:00
|
|
|
|
|
|
|
- name: Ensure machines of which remote target address is 48 and 50 are powered off
|
|
|
|
community.general.ipmi_power:
|
|
|
|
name: test.testdomain.com
|
|
|
|
user: admin
|
|
|
|
password: password
|
|
|
|
state: off
|
|
|
|
machine:
|
|
|
|
- targetAddress: 48
|
|
|
|
- targetAddress: 50
|
|
|
|
|
|
|
|
- name: Ensure machine of which remote target address is 48 is powered on, and 50 is powered off
|
|
|
|
community.general.ipmi_power:
|
|
|
|
name: test.testdomain.com
|
|
|
|
user: admin
|
|
|
|
password: password
|
|
|
|
machine:
|
|
|
|
- targetAddress: 48
|
|
|
|
state: on
|
|
|
|
- targetAddress: 50
|
|
|
|
state: off
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import traceback
|
2021-11-22 07:18:13 +01:00
|
|
|
import binascii
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
PYGHMI_IMP_ERR = None
|
2022-01-08 16:03:51 +01:00
|
|
|
INVALID_TARGET_ADDRESS = 0x100
|
2020-03-09 10:11:07 +01:00
|
|
|
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'),
|
2022-01-08 16:03:51 +01:00
|
|
|
state=dict(choices=['on', 'off', 'shutdown', 'reset', 'boot']),
|
2020-03-09 10:11:07 +01:00
|
|
|
user=dict(required=True, no_log=True),
|
|
|
|
password=dict(required=True, no_log=True),
|
2021-11-22 07:18:13 +01:00
|
|
|
key=dict(type='str', no_log=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout=dict(default=300, type='int'),
|
2022-01-08 16:03:51 +01:00
|
|
|
machine=dict(
|
|
|
|
type='list', elements='dict',
|
|
|
|
options=dict(
|
|
|
|
targetAddress=dict(required=True, type='int'),
|
|
|
|
state=dict(type='str', choices=['on', 'off', 'shutdown', 'reset', 'boot']),
|
|
|
|
),
|
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
2022-01-08 16:03:51 +01:00
|
|
|
required_one_of=(
|
|
|
|
['state', 'machine'],
|
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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']
|
2022-01-08 16:03:51 +01:00
|
|
|
machine = module.params['machine']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-22 07:18:13 +01:00
|
|
|
try:
|
|
|
|
if module.params['key']:
|
|
|
|
key = binascii.unhexlify(module.params['key'])
|
|
|
|
else:
|
|
|
|
key = None
|
2022-01-08 16:03:51 +01:00
|
|
|
except Exception:
|
2021-11-22 07:18:13 +01:00
|
|
|
module.fail_json(msg="Unable to convert 'key' from hex string.")
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
# --- run command ---
|
|
|
|
try:
|
|
|
|
ipmi_cmd = command.Command(
|
2021-11-22 07:18:13 +01:00
|
|
|
bmc=name, userid=user, password=password, port=port, kg=key
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
module.debug('ipmi instantiated - name: "%s"' % name)
|
|
|
|
|
2022-01-08 16:03:51 +01:00
|
|
|
changed = False
|
|
|
|
if machine is None:
|
|
|
|
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
|
|
|
|
|
|
|
|
if 'error' in response:
|
|
|
|
module.fail_json(msg=response['error'])
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, **response)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2022-01-08 16:03:51 +01:00
|
|
|
response = []
|
|
|
|
for entry in machine:
|
|
|
|
taddr = entry['targetAddress']
|
|
|
|
if taddr >= INVALID_TARGET_ADDRESS:
|
|
|
|
module.fail_json(msg="targetAddress should be set between 0 to 255.")
|
|
|
|
|
|
|
|
try:
|
|
|
|
# bridge_request is supported on pyghmi 1.5.30 and later
|
|
|
|
current = ipmi_cmd.get_power(bridge_request={"addr": taddr})
|
|
|
|
except TypeError:
|
|
|
|
module.fail_json(
|
|
|
|
msg="targetAddress isn't supported on the installed pyghmi.")
|
|
|
|
|
|
|
|
if entry['state']:
|
|
|
|
tstate = entry['state']
|
|
|
|
elif state:
|
|
|
|
tstate = state
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Either state or suboption of machine state should be set.")
|
|
|
|
|
|
|
|
if current['powerstate'] != tstate:
|
|
|
|
changed = True
|
|
|
|
if not module.check_mode:
|
|
|
|
new = ipmi_cmd.set_power(tstate, wait=timeout, bridge_request={"addr": taddr})
|
|
|
|
if 'error' in new:
|
|
|
|
module.fail_json(msg=new['error'])
|
|
|
|
|
|
|
|
response.append(
|
|
|
|
{'targetAddress:': taddr, 'powerstate': new['powerstate']})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-01-08 16:03:51 +01:00
|
|
|
if current['powerstate'] == tstate or module.check_mode:
|
|
|
|
response.append({'targetAddress:': taddr, 'powerstate': tstate})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-01-08 16:03:51 +01:00
|
|
|
module.exit_json(changed=changed, status=response)
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|