Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/host_power.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2019 Bernhard Hopfenmüller (ATIX AG)
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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 # power_status endpoint was only added in foreman 1.22.0 per https://projects.theforeman.org/issues/25436
113 # Delete this piece when versions below 1.22 are off common use
114 # begin delete
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 # end delete (on delete un-indent the below two lines)
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()