| 1 |
|
| 2 |
|
| 3 |
) |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
__metaclass__ = type |
| 20 |
|
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
--- |
| 24 |
module: host_power |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Power State of Hosts |
| 27 |
description: |
| 28 |
- "Manage power state of a host" |
| 29 |
- "This beta version can start and stop an existing foreman host and question the current power state." |
| 30 |
author: |
| 31 |
- "Bernhard Hopfenmueller (@Fobhep) ATIX AG" |
| 32 |
- "Baptiste Agasse (@bagasse)" |
| 33 |
options: |
| 34 |
name: |
| 35 |
description: Name (FQDN) of the host |
| 36 |
required: true |
| 37 |
aliases: |
| 38 |
- hostname |
| 39 |
type: str |
| 40 |
state: |
| 41 |
description: Desired power state |
| 42 |
default: state |
| 43 |
choices: |
| 44 |
- 'on' |
| 45 |
- 'start' |
| 46 |
- 'off' |
| 47 |
- 'stop' |
| 48 |
- 'soft' |
| 49 |
- 'reboot' |
| 50 |
- 'cycle' |
| 51 |
- 'reset' |
| 52 |
- 'state' |
| 53 |
- 'status' |
| 54 |
type: str |
| 55 |
extends_documentation_fragment: |
| 56 |
- theforeman.foreman.foreman |
| 57 |
''' |
| 58 |
|
| 59 |
EXAMPLES = ''' |
| 60 |
- name: "Switch a host on" |
| 61 |
theforeman.foreman.host_power: |
| 62 |
username: "admin" |
| 63 |
password: "changeme" |
| 64 |
server_url: "https://foreman.example.com" |
| 65 |
hostname: "test-host.domain.test" |
| 66 |
state: 'on' |
| 67 |
|
| 68 |
- name: "Switch a host off" |
| 69 |
theforeman.foreman.host_power: |
| 70 |
username: "admin" |
| 71 |
password: "changeme" |
| 72 |
server_url: "https://foreman.example.com" |
| 73 |
hostname: "test-host.domain.test" |
| 74 |
state: 'off' |
| 75 |
|
| 76 |
- name: "Query host power state" |
| 77 |
theforeman.foreman.host_power: |
| 78 |
username: "admin" |
| 79 |
password: "changeme" |
| 80 |
server_url: "https://foreman.example.com" |
| 81 |
hostname: "test-host.domain.test" |
| 82 |
state: state |
| 83 |
register: result |
| 84 |
- debug: |
| 85 |
msg: "Host power state is {{ result.power_state }}" |
| 86 |
''' |
| 87 |
|
| 88 |
RETURN = ''' |
| 89 |
power_state: |
| 90 |
description: current power state of host |
| 91 |
returned: always |
| 92 |
type: str |
| 93 |
sample: "off" |
| 94 |
''' |
| 95 |
|
| 96 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule |
| 97 |
|
| 98 |
|
| 99 |
def main(): |
| 100 |
module = ForemanEntityAnsibleModule( |
| 101 |
foreman_spec=dict( |
| 102 |
name=dict(aliases=['hostname'], required=True), |
| 103 |
), |
| 104 |
argument_spec=dict( |
| 105 |
state=dict(default='state', choices=['on', 'start', 'off', 'stop', 'soft', 'reboot', 'cycle', 'reset', 'state', 'status']), |
| 106 |
) |
| 107 |
) |
| 108 |
|
| 109 |
module_params = module.foreman_params |
| 110 |
|
| 111 |
with module.api_connection(): |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
if 'power_status' not in module.foremanapi.resource('hosts').actions: |
| 116 |
params = {'id': module_params['name'], 'power_action': 'status'} |
| 117 |
power_state = module.resource_action('hosts', 'power', params=params, ignore_check_mode=True) |
| 118 |
power_state['state'] = 'on' if power_state['power'] == 'running' else 'off' |
| 119 |
else: |
| 120 |
|
| 121 |
params = {'id': module_params['name']} |
| 122 |
power_state = module.resource_action('hosts', 'power_status', params=params, ignore_check_mode=True) |
| 123 |
|
| 124 |
if module.state in ['state', 'status']: |
| 125 |
module.exit_json(power_state=power_state['state']) |
| 126 |
elif ((module.state in ['on', 'start'] and power_state['state'] == 'on') |
| 127 |
or (module.state in ['off', 'stop'] and power_state['state'] == 'off')): |
| 128 |
module.exit_json() |
| 129 |
else: |
| 130 |
params['power_action'] = module.state |
| 131 |
module.resource_action('hosts', 'power', params=params) |
| 132 |
|
| 133 |
|
| 134 |
if __name__ == '__main__': |
| 135 |
main() |