| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022-2026 The Project Contributors. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
# |
| 17 |
# For a detailed list of copyright holders and contribution history, |
| 18 |
# please refer to the CONTRIBUTORS.md file in the project root. |
| 19 |
|
| 20 |
from __future__ import (absolute_import, division, print_function) |
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
DOCUMENTATION = r''' |
| 24 |
--- |
| 25 |
module: sap_control_exec |
| 26 |
|
| 27 |
short_description: Ansible Module to execute SAPCONTROL |
| 28 |
|
| 29 |
version_added: "1.1.0" |
| 30 |
|
| 31 |
description: |
| 32 |
- Provides support for sapstartsrv formaly known as sapcontrol |
| 33 |
- A complete information of all functions and the parameters can be found here |
| 34 |
U(https://www.sap.com/documents/2016/09/0a40e60d-8b7c-0010-82c7-eda71af511fa.html) |
| 35 |
- When hostname is 'localhost', sysnr is set and no username/password are provided, the module will attempt |
| 36 |
to use local Unix socket authentication (which works with 'become' privilege escalation). |
| 37 |
|
| 38 |
options: |
| 39 |
sysnr: |
| 40 |
description: |
| 41 |
- The system number of the instance. |
| 42 |
required: false |
| 43 |
type: str |
| 44 |
port: |
| 45 |
description: |
| 46 |
- The port number of the sapstartsrv. |
| 47 |
- If provided, the module will use always use http connection instead of local socket. |
| 48 |
required: false |
| 49 |
type: int |
| 50 |
username: |
| 51 |
description: |
| 52 |
- The username to connect to the sapstartsrv. |
| 53 |
required: false |
| 54 |
type: str |
| 55 |
password: |
| 56 |
description: |
| 57 |
- The password to connect to the sapstartsrv. |
| 58 |
required: false |
| 59 |
type: str |
| 60 |
hostname: |
| 61 |
description: |
| 62 |
- The hostname to connect to the sapstartsrv. |
| 63 |
- Could be an IP address, FQDN or hostname. |
| 64 |
required: false |
| 65 |
default: localhost |
| 66 |
type: str |
| 67 |
function: |
| 68 |
description: |
| 69 |
- The function to execute. |
| 70 |
required: true |
| 71 |
choices: |
| 72 |
- Start |
| 73 |
- Stop |
| 74 |
- Shutdown |
| 75 |
- InstanceStart |
| 76 |
- InstanceStop |
| 77 |
- Bootstrap |
| 78 |
- ParameterValue |
| 79 |
- GetProcessList |
| 80 |
- GetProcessList2 |
| 81 |
- GetStartProfile |
| 82 |
- GetTraceFile |
| 83 |
- GetAlertTree |
| 84 |
- GetAlerts |
| 85 |
- RestartService |
| 86 |
- StopService |
| 87 |
- GetEnvironment |
| 88 |
- ListDeveloperTraces |
| 89 |
- ListLogFiles |
| 90 |
- ReadDeveloperTrace |
| 91 |
- ReadLogFile |
| 92 |
- AnalyseLogFile |
| 93 |
- ConfigureLogFileList |
| 94 |
- GetLogFileList |
| 95 |
- RestartInstance |
| 96 |
- SendSignal |
| 97 |
- GetVersionInfo |
| 98 |
- GetQueueStatistic |
| 99 |
- GetInstanceProperties |
| 100 |
- OSExecute |
| 101 |
- AnalyseLogFiles |
| 102 |
- GetAccessPointList |
| 103 |
- GetSystemInstanceList |
| 104 |
- StartSystem |
| 105 |
- StopSystem |
| 106 |
- RestartSystem |
| 107 |
- AccessCheck |
| 108 |
- GetProcessParameter |
| 109 |
- SetProcessParameter |
| 110 |
- SetProcessParameter2 |
| 111 |
- ShmDetach |
| 112 |
- CreateSnapshot |
| 113 |
- ReadSnapshot |
| 114 |
- ListSnapshots |
| 115 |
- DeleteSnapshots |
| 116 |
- RequestLogonFile |
| 117 |
- GetNetworkId |
| 118 |
- GetSecNetworkId |
| 119 |
- UpdateSystem |
| 120 |
- GetSystemUpdateList |
| 121 |
- UpdateSCSInstance |
| 122 |
- ABAPReadSyslog |
| 123 |
- ABAPReadRawSyslog |
| 124 |
- ABAPGetWPTable |
| 125 |
- ABAPAcknoledgeAlerts |
| 126 |
- CMGetThreadList |
| 127 |
- ICMGetConnectionList |
| 128 |
- ICMGetCacheEntries |
| 129 |
- ICMGetProxyConnectionList |
| 130 |
- WebDispGetServerList |
| 131 |
- WebDispGetGroupList |
| 132 |
- WebDispGetVirtHostList |
| 133 |
- WebDispGeUrlPrefixList |
| 134 |
- EnqGetLockTable |
| 135 |
- EnqRemoveLocks |
| 136 |
- EnqGetStatistic |
| 137 |
type: str |
| 138 |
parameter: |
| 139 |
description: |
| 140 |
- The parameter to pass to the function. |
| 141 |
- Specific functions require complex parameters to be defined. See example for InstanceStart. |
| 142 |
required: false |
| 143 |
type: raw |
| 144 |
force: |
| 145 |
description: |
| 146 |
- Forces the execution of the function C(Stop). |
| 147 |
required: false |
| 148 |
default: false |
| 149 |
type: bool |
| 150 |
author: |
| 151 |
- Rainer Leber (@RainerLeber) |
| 152 |
- Robert Kraemer (@rkpobe) |
| 153 |
notes: |
| 154 |
- Does not support C(check_mode). |
| 155 |
''' |
| 156 |
|
| 157 |
EXAMPLES = r""" |
| 158 |
- name: GetProcessList with sysnr |
| 159 |
community.sap_libs.sap_control_exec: |
| 160 |
hostname: 192.168.8.15 |
| 161 |
sysnr: "01" |
| 162 |
function: GetProcessList |
| 163 |
|
| 164 |
- name: GetProcessList with custom port |
| 165 |
community.sap_libs.sap_control_exec: |
| 166 |
hostname: 192.168.8.15 |
| 167 |
function: GetProcessList |
| 168 |
port: 50113 |
| 169 |
|
| 170 |
- name: ParameterValue with authentication |
| 171 |
community.sap_libs.sap_control_exec: |
| 172 |
hostname: 192.168.8.15 |
| 173 |
sysnr: "01" |
| 174 |
username: hdbadm |
| 175 |
password: test1234 |
| 176 |
function: ParameterValue |
| 177 |
parameter: ztta |
| 178 |
|
| 179 |
- name: GetVersionInfo using local Unix socket (requires become) |
| 180 |
community.sap_libs.sap_control_exec: |
| 181 |
sysnr: "00" |
| 182 |
function: GetVersionInfo |
| 183 |
become: true |
| 184 |
|
| 185 |
- name: GetProcessList using local Unix socket as SAP admin user |
| 186 |
community.sap_libs.sap_control_exec: |
| 187 |
sysnr: "00" |
| 188 |
function: GetProcessList |
| 189 |
become: true |
| 190 |
become_user: "{{ sap_sid | lower }}adm" |
| 191 |
|
| 192 |
- name: InstanceStart with complex parameter |
| 193 |
community.sap_libs.sap_control_exec: |
| 194 |
sysnr: "00" |
| 195 |
function: InstanceStart |
| 196 |
become: true |
| 197 |
parameter: |
| 198 |
host: "s4hana" |
| 199 |
nr: "00" |
| 200 |
""" |
| 201 |
|
| 202 |
RETURN = r''' |
| 203 |
msg: |
| 204 |
description: Success-message with functionname. |
| 205 |
type: str |
| 206 |
returned: always |
| 207 |
sample: 'Succesful execution of: GetProcessList' |
| 208 |
out: |
| 209 |
description: The full output of the required function. |
| 210 |
type: list |
| 211 |
elements: dict |
| 212 |
returned: always |
| 213 |
sample: [{ |
| 214 |
"item": [ |
| 215 |
{ |
| 216 |
"description": "MessageServer", |
| 217 |
"dispstatus": "SAPControl-GREEN", |
| 218 |
"elapsedtime": "412:30:50", |
| 219 |
"name": "msg_server", |
| 220 |
"pid": 70643, |
| 221 |
"starttime": "2022 03 13 15:22:42", |
| 222 |
"textstatus": "Running" |
| 223 |
}, |
| 224 |
{ |
| 225 |
"description": "EnqueueServer", |
| 226 |
"dispstatus": "SAPControl-GREEN", |
| 227 |
"elapsedtime": "412:30:50", |
| 228 |
"name": "enserver", |
| 229 |
"pid": 70644, |
| 230 |
"starttime": "2022 03 13 15:22:42", |
| 231 |
"textstatus": "Running" |
| 232 |
}, |
| 233 |
{ |
| 234 |
"description": "Gateway", |
| 235 |
"dispstatus": "SAPControl-GREEN", |
| 236 |
"elapsedtime": "412:30:50", |
| 237 |
"name": "gwrd", |
| 238 |
"pid": 70645, |
| 239 |
"starttime": "2022 03 13 15:22:42", |
| 240 |
"textstatus": "Running" |
| 241 |
} |
| 242 |
] |
| 243 |
}] |
| 244 |
''' |
| 245 |
|
| 246 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 247 |
|
| 248 |
from ..module_utils.sapstartsrv_client import ( |
| 249 |
HAS_SUDS_LIBRARY, |
| 250 |
SUDS_LIBRARY_IMPORT_ERROR, |
| 251 |
call_sap_control as connection, |
| 252 |
recursive_dict, |
| 253 |
is_read_only_function, |
| 254 |
requires_force, |
| 255 |
) |
| 256 |
|
| 257 |
|
| 258 |
def choices(): |
| 259 |
retlist = ["Start", "Stop", "Shutdown", "InstanceStart", "InstanceStop", "Bootstrap", "ParameterValue", "GetProcessList", |
| 260 |
"GetProcessList2", "GetStartProfile", "GetTraceFile", "GetAlertTree", "GetAlerts", "RestartService", |
| 261 |
"StopService", "GetEnvironment", "ListDeveloperTraces", "ListLogFiles", "ReadDeveloperTrace", "ReadLogFile", |
| 262 |
"AnalyseLogFile", "ConfigureLogFileList", "GetLogFileList", "RestartInstance", "SendSignal", "GetVersionInfo", |
| 263 |
"GetQueueStatistic", "GetInstanceProperties", "OSExecute", "AnalyseLogFiles", "GetAccessPointList", |
| 264 |
"GetSystemInstanceList", "StartSystem", "StopSystem", "RestartSystem", "AccessCheck", "GetProcessParameter", |
| 265 |
"SetProcessParameter", "SetProcessParameter2", "ShmDetach", "CreateSnapshot", "ReadSnapshot", "ListSnapshots", |
| 266 |
"DeleteSnapshots", "RequestLogonFile", "GetNetworkId", "GetSecNetworkId", "UpdateSystem", "GetSystemUpdateList", |
| 267 |
"UpdateSCSInstance", "ABAPReadSyslog", "ABAPReadRawSyslog", "ABAPGetWPTable", "ABAPAcknoledgeAlerts", |
| 268 |
"CMGetThreadList", "ICMGetConnectionList", "ICMGetCacheEntries", "ICMGetProxyConnectionList", |
| 269 |
"WebDispGetServerList", "WebDispGetGroupList", "WebDispGetVirtHostList", "WebDispGeUrlPrefixList", |
| 270 |
"EnqGetLockTable", "EnqRemoveLocks", "EnqGetStatistic"] |
| 271 |
return retlist |
| 272 |
|
| 273 |
|
| 274 |
def main(): |
| 275 |
module = AnsibleModule( |
| 276 |
argument_spec=dict( |
| 277 |
sysnr=dict(type='str', required=False), |
| 278 |
port=dict(type='int', required=False), |
| 279 |
username=dict(type='str', required=False), |
| 280 |
password=dict(type='str', no_log=True, required=False), |
| 281 |
hostname=dict(type='str', default="localhost"), |
| 282 |
function=dict(type='str', required=True, choices=choices()), |
| 283 |
parameter=dict(type='raw', required=False), # raw will allow dict or string. |
| 284 |
force=dict(type='bool', default=False), |
| 285 |
), |
| 286 |
# Remove strict requirements to allow local mode |
| 287 |
required_one_of=[('sysnr', 'port')], |
| 288 |
mutually_exclusive=[('sysnr', 'port')], |
| 289 |
supports_check_mode=False, |
| 290 |
) |
| 291 |
result = dict(changed=False, msg='', out=[], error='') # Default out to list for consistent return type. |
| 292 |
params = module.params |
| 293 |
|
| 294 |
sysnr = params['sysnr'] |
| 295 |
port = params['port'] |
| 296 |
username = params['username'] |
| 297 |
password = params['password'] |
| 298 |
hostname = params['hostname'] |
| 299 |
function = params['function'] |
| 300 |
parameter = params['parameter'] |
| 301 |
force = params['force'] |
| 302 |
|
| 303 |
if not HAS_SUDS_LIBRARY: |
| 304 |
module.fail_json( |
| 305 |
msg=missing_required_lib('suds'), |
| 306 |
exception=SUDS_LIBRARY_IMPORT_ERROR) |
| 307 |
|
| 308 |
# Validate arguments |
| 309 |
if sysnr is None and port is None: |
| 310 |
module.fail_json(msg="Either 'sysnr' or 'port' must be provided") |
| 311 |
|
| 312 |
if sysnr is not None and port is not None: |
| 313 |
module.fail_json(msg="'sysnr' and 'port' are mutually exclusive") |
| 314 |
|
| 315 |
if requires_force(function) and force is False: |
| 316 |
module.fail_json(msg="Function '{0}' requires force: True".format(function)) |
| 317 |
|
| 318 |
if function == "StartSystem": |
| 319 |
parameter = dict(waittimeout=0) |
| 320 |
elif function == "StopSystem" or function == "RestartSystem": |
| 321 |
parameter = dict(waittimeout=0, softtimeout=0) |
| 322 |
|
| 323 |
# Determine if we should use local Unix socket connection |
| 324 |
# Use local socket connection if hostname is localhost and no username/password provided |
| 325 |
# True: Socket connection, False: SOAP connection |
| 326 |
is_socket = (hostname == "localhost" and |
| 327 |
username is None and |
| 328 |
password is None and |
| 329 |
sysnr is not None) |
| 330 |
|
| 331 |
if port is None: |
| 332 |
try: |
| 333 |
if is_socket: |
| 334 |
result['connection_type'] = 'socket' |
| 335 |
result['connection_url'] = "http://localhost/sapcontrol?wsdl" |
| 336 |
|
| 337 |
result_conn = connection(hostname, None, username, password, function, parameter, sysnr=sysnr, is_socket=True) |
| 338 |
else: |
| 339 |
result['connection_type'] = 'soap' |
| 340 |
|
| 341 |
# Try HTTPS and HTTP ports |
| 342 |
try: |
| 343 |
result['connection_url'] = 'http://{0}:5{1}14/sapcontrol?wsdl'.format(hostname, str(sysnr).zfill(2)) |
| 344 |
result_conn = connection(hostname, "5{0}14".format((sysnr).zfill(2)), username, password, function, parameter, sysnr) |
| 345 |
except Exception: |
| 346 |
result['connection_url'] = 'http://{0}:5{1}13/sapcontrol?wsdl'.format(hostname, str(sysnr).zfill(2)) |
| 347 |
result_conn = connection(hostname, "5{0}13".format((sysnr).zfill(2)), username, password, function, parameter, sysnr) |
| 348 |
except Exception as err: |
| 349 |
if "already started" in str(err).lower(): |
| 350 |
already_started_msg = "Function {0} returned that Instance is already started.".format(function) |
| 351 |
result_conn = ({"status": "already_started", "msg": already_started_msg}) |
| 352 |
else: |
| 353 |
result['error'] = str(err) |
| 354 |
else: |
| 355 |
result['connection_type'] = 'soap' |
| 356 |
result['connection_url'] = 'http://{0}:{1}/sapcontrol?wsdl'.format(hostname, port) |
| 357 |
try: |
| 358 |
result_conn = connection(hostname, port, username, password, function, parameter, sysnr, is_socket=False) |
| 359 |
except Exception as err: |
| 360 |
if "already started" in str(err).lower(): |
| 361 |
already_started_msg = "Function {0} returned that Instance is already started.".format(function) |
| 362 |
result_conn = ({"status": "already_started", "msg": already_started_msg}) |
| 363 |
else: |
| 364 |
result['error'] = str(err) |
| 365 |
|
| 366 |
if result['error'] != '': |
| 367 |
result['msg'] = 'Function execution has failed. See error for more details.' |
| 368 |
module.fail_json(**result) |
| 369 |
|
| 370 |
conn_result = result_conn |
| 371 |
|
| 372 |
# Ensure that we run recursive_dict only on dict and leave it for idempotent functions. |
| 373 |
if isinstance(conn_result, dict) and conn_result.get('status') == 'already_started': |
| 374 |
result['changed'] = False |
| 375 |
result['msg'] = conn_result.get('msg') |
| 376 |
result['out'] = [None] # Ensure we return same content as Start and Stop functions for consistency |
| 377 |
else: |
| 378 |
result['changed'] = not is_read_only_function(function) |
| 379 |
result['msg'] = "Successful execution of function: " + function |
| 380 |
returned_data = recursive_dict(conn_result) if conn_result is not None else conn_result |
| 381 |
result['out'] = [returned_data] |
| 382 |
|
| 383 |
module.exit_json(**result) |
| 384 |
|
| 385 |
|
| 386 |
if __name__ == '__main__': |
| 387 |
main() |