| 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_hostctrl_exec |
| 26 |
|
| 27 |
short_description: Ansible Module to execute SAPHostControl |
| 28 |
|
| 29 |
version_added: "1.6.0" |
| 30 |
|
| 31 |
description: |
| 32 |
- Provides support for SAP Host Agent |
| 33 |
- More information of some functions can be found here |
| 34 |
U(https://help.sap.com/docs/host-agent/saphostcontrol-web-service-interface) |
| 35 |
- When hostname is 'localhost' 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 |
port: |
| 40 |
description: |
| 41 |
- The port number of the sapstartsrv (usually 1128 and 1129). |
| 42 |
- If provided, the module will use always use http connection instead of local socket. |
| 43 |
required: false |
| 44 |
type: int |
| 45 |
username: |
| 46 |
description: |
| 47 |
- The username to connect to the sapstartsrv. |
| 48 |
required: false |
| 49 |
type: str |
| 50 |
password: |
| 51 |
description: |
| 52 |
- The password to connect to the sapstartsrv. |
| 53 |
required: false |
| 54 |
type: str |
| 55 |
hostname: |
| 56 |
description: |
| 57 |
- The hostname to connect to the sapstartsrv. |
| 58 |
- Could be an IP address, FQDN or hostname. |
| 59 |
required: false |
| 60 |
default: localhost |
| 61 |
type: str |
| 62 |
function: |
| 63 |
description: |
| 64 |
- The function to execute. |
| 65 |
required: true |
| 66 |
choices: |
| 67 |
- ACOSPrepare |
| 68 |
- AttachDatabase |
| 69 |
- CallServiceOperation |
| 70 |
- CancelOperation |
| 71 |
- ConfigureOutsideDiscovery |
| 72 |
- ConfigureOutsideDiscoveryDestination |
| 73 |
- ConfigureOutsideDiscoveryPath |
| 74 |
- DeployConfiguration |
| 75 |
- DeployManagedObjectsFromSAR |
| 76 |
- DetachDatabase |
| 77 |
- DetectManagedObjects |
| 78 |
- ExecuteDatabaseOperation |
| 79 |
- ExecuteInstallationProcedure |
| 80 |
- ExecuteOperation |
| 81 |
- ExecuteOutsideDiscovery |
| 82 |
- ExecuteUpgradeProcedure |
| 83 |
- FinalizeDatabaseCopy |
| 84 |
- GetCIMObject |
| 85 |
- GetComputerSystem |
| 86 |
- GetDatabaseProperties |
| 87 |
- GetDatabaseStatus |
| 88 |
- GetDatabaseSystemStatus |
| 89 |
- GetIpAddressProperties |
| 90 |
- GetOperationResults |
| 91 |
- ListDatabases |
| 92 |
- ListDatabaseSystems |
| 93 |
- ListInstances |
| 94 |
- LiveDatabaseUpdate |
| 95 |
- PrepareDatabaseCopy |
| 96 |
- RegisterInstanceService |
| 97 |
- SetDatabaseProperty |
| 98 |
- StartDatabase |
| 99 |
- StartInstance |
| 100 |
- StopDatabase |
| 101 |
- StopInstance |
| 102 |
- UnregisterInstanceService |
| 103 |
type: str |
| 104 |
parameters: |
| 105 |
description: |
| 106 |
- A dictionary containing all the parameters to pass to the function. |
| 107 |
- This option is mandatory for most of the functions, only a few like ListInstances or ListDatabases can be run without option. |
| 108 |
- Be careful, no validation is done by this module regarding the suboptions. |
| 109 |
- An analysis of the WSDL file must be done to provide correct parameters. |
| 110 |
- See also the examples section for more appreciation. |
| 111 |
required: false |
| 112 |
type: dict |
| 113 |
force: |
| 114 |
description: |
| 115 |
- Forces the execution of the function C(Stop). |
| 116 |
required: false |
| 117 |
default: false |
| 118 |
type: bool |
| 119 |
author: |
| 120 |
- Rainer Leber (@RainerLeber) |
| 121 |
- Robert Kraemer (@rkpobe) |
| 122 |
- Yannick Douvry (@ydouvry) |
| 123 |
notes: |
| 124 |
- Does not support C(check_mode). |
| 125 |
''' |
| 126 |
|
| 127 |
|
| 128 |
EXAMPLES = r""" |
| 129 |
- name: ListDatabases with custom host and port |
| 130 |
community.sap_libs.sap_hostctrl_exec: |
| 131 |
hostname: 192.168.8.15 |
| 132 |
function: ListDatabases |
| 133 |
port: 1128 |
| 134 |
|
| 135 |
- name: ListInstances using local Unix socket (requires become) |
| 136 |
community.sap_libs.sap_hostctrl_exec: |
| 137 |
function: ListInstances |
| 138 |
become: true |
| 139 |
|
| 140 |
- name: ListInstances using local Unix socket as SAP admin user and selector parameters |
| 141 |
community.sap_libs.sap_hostctrl_exec: |
| 142 |
function: ListInstances |
| 143 |
parameters: |
| 144 |
aSelector: |
| 145 |
aInstanceStatus: S-INSTALLED # S-INSTALLED | S-RUNNING | S-STOPPED | S-LAST |
| 146 |
become: true |
| 147 |
become_user: "{{ sap_sid | lower }}adm" |
| 148 |
|
| 149 |
- name: StartInstance with authentication |
| 150 |
community.sap_libs.sap_hostctrl_exec: |
| 151 |
hostname: 192.168.8.15 |
| 152 |
username: tstadm |
| 153 |
password: test1234 |
| 154 |
function: StartInstance |
| 155 |
parameters: |
| 156 |
aInstance: |
| 157 |
mSid: 'TST' |
| 158 |
mSystemNumber: '01' |
| 159 |
aOptions: |
| 160 |
mTimeout: -1 # -1=synchronous, 0=async, >0=wait timeout in seconds |
| 161 |
mSoftTimeout: 0 |
| 162 |
mOptions: |
| 163 |
- O-INSTANCE |
| 164 |
|
| 165 |
- name: Synchronous StartDatabase using local Unix socket with arguments |
| 166 |
community.sap_libs.sap_hostctrl_exec: |
| 167 |
function: StartDatabase |
| 168 |
parameters: |
| 169 |
aArguments: |
| 170 |
item: "{{ dict_arguments | dict2items(key_name='mKey', value_name='mValue') }}" |
| 171 |
aOptions: |
| 172 |
mTimeout: -1 |
| 173 |
vars: |
| 174 |
dict_arguments: |
| 175 |
Database/Name: SYSTEMDB@XDH |
| 176 |
Database/Type: hdb # hdb|ora|mss|db6|ada|sap|syb|ase|db2|max |
| 177 |
# Database/InstanceName: HDB00 # the following parameters are optional |
| 178 |
# Database/Host: mydbhost.example.com |
| 179 |
# Database/Username: SYSTEM |
| 180 |
# Database/Password: StarWarsFTW123! |
| 181 |
become: true |
| 182 |
|
| 183 |
- name: Example of GetDatabaseStatus |
| 184 |
community.sap_libs.sap_hostctrl_exec: |
| 185 |
function: GetDatabaseStatus |
| 186 |
parameters: |
| 187 |
aArguments: |
| 188 |
item: "{{ dict_arguments | dict2items(key_name='mKey', value_name='mValue') }}" |
| 189 |
vars: |
| 190 |
dict_arguments: |
| 191 |
Database/Name: XDH |
| 192 |
Database/Type: hdb |
| 193 |
become: true |
| 194 |
|
| 195 |
# Example from https://help.sap.com/docs/host-agent/saphostcontrol-web-service-interface/executeoperation |
| 196 |
- name: Asynchronous ExecuteOperation |
| 197 |
community.sap_libs.sap_hostctrl_exec: |
| 198 |
function: ExecuteOperation |
| 199 |
parameters: |
| 200 |
aOperation: "sayhello" |
| 201 |
aArguments: |
| 202 |
item: |
| 203 |
mKey: "MY_NAME" |
| 204 |
mValue: "Sally" |
| 205 |
register: operation_say_hello |
| 206 |
become: true |
| 207 |
|
| 208 |
- name: Check results of previous ExecuteOperation |
| 209 |
community.sap_libs.sap_hostctrl_exec: |
| 210 |
function: GetOperationResults |
| 211 |
parameters: |
| 212 |
aOperationID: "{{ operation_say_hello.out[0].mOperationID }}" |
| 213 |
aOptions: |
| 214 |
mTimeout: -1 |
| 215 |
become: true |
| 216 |
|
| 217 |
# Output of GetOperationResults for the above ExecuteOperation : |
| 218 |
# |
| 219 |
# changed: true |
| 220 |
# error: "" |
| 221 |
# msg: Succesful execution of: GetOperationResults |
| 222 |
# out: |
| 223 |
# - mOperationID: "42010A3F050B1FD0B5A26EF66B9FA7B7" |
| 224 |
# mOperationResults: |
| 225 |
# item: |
| 226 |
# - mMessageKey: description |
| 227 |
# mMessageValue: Say hello |
| 228 |
# - mMessageKey: null |
| 229 |
# mMessageValue: "\"hello Sally\"" |
| 230 |
# - mMessageKey: exitcode |
| 231 |
# mMessageValue: 0 |
| 232 |
""" |
| 233 |
|
| 234 |
RETURN = r''' |
| 235 |
msg: |
| 236 |
description: Success-message with functionname. |
| 237 |
type: str |
| 238 |
returned: always |
| 239 |
sample: 'Succesful execution of: ListInstances' |
| 240 |
out: |
| 241 |
description: The full output of the required function. |
| 242 |
type: list |
| 243 |
elements: dict |
| 244 |
returned: always |
| 245 |
sample: [{ |
| 246 |
"item": [ |
| 247 |
{ |
| 248 |
"mHostname": "test-vm-001", |
| 249 |
"mSapVersionInfo": "793, patch 200, commit ec1833e294d84a70c04c6a1b01fd1a493f5c72fb", |
| 250 |
"mSid": "TST", |
| 251 |
"mSystemNumber": "01" |
| 252 |
}, |
| 253 |
{ |
| 254 |
"mHostname": "test-vm-001", |
| 255 |
"mSapVersionInfo": "793, patch 200, commit ec1833e294d84a70c04c6a1b01fd1a493f5c72fb", |
| 256 |
"mSid": "TST", |
| 257 |
"mSystemNumber": "00" |
| 258 |
}] |
| 259 |
}] |
| 260 |
''' |
| 261 |
|
| 262 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 263 |
|
| 264 |
from ..module_utils.sapstartsrv_client import ( |
| 265 |
HAS_SUDS_LIBRARY, |
| 266 |
SUDS_LIBRARY_IMPORT_ERROR, |
| 267 |
recursive_dict, |
| 268 |
call_sap_hostctrl as connection, |
| 269 |
is_read_only_function, |
| 270 |
requires_force, |
| 271 |
) |
| 272 |
|
| 273 |
|
| 274 |
def choices(): |
| 275 |
retlist = ["ACOSPrepare", "AttachDatabase", "CallServiceOperation", "CancelOperation", "ConfigureOutsideDiscovery", |
| 276 |
"ConfigureOutsideDiscoveryDestination", "ConfigureOutsideDiscoveryPath", "DeployConfiguration", "DeployManagedObjectsFromSAR", |
| 277 |
"DetachDatabase", "DetectManagedObjects", "ExecuteDatabaseOperation", "ExecuteInstallationProcedure", "ExecuteOperation", |
| 278 |
"ExecuteOutsideDiscovery", "ExecuteUpgradeProcedure", "FinalizeDatabaseCopy", "GetCIMObject", "GetComputerSystem", |
| 279 |
"GetDatabaseProperties", "GetDatabaseStatus", "GetDatabaseSystemStatus", "GetIpAddressProperties", "GetOperationResults", |
| 280 |
"ListDatabases", "ListDatabaseSystems", "ListInstances", "LiveDatabaseUpdate", "PrepareDatabaseCopy", "RegisterInstanceService", |
| 281 |
"SetDatabaseProperty", "StartDatabase", "StartInstance", "StopDatabase", "StopInstance", "UnregisterInstanceService"] |
| 282 |
return retlist |
| 283 |
|
| 284 |
|
| 285 |
def main(): |
| 286 |
module = AnsibleModule( |
| 287 |
argument_spec=dict( |
| 288 |
port=dict(type='int', required=False), |
| 289 |
username=dict(type='str', required=False), |
| 290 |
password=dict(type='str', no_log=True, required=False), |
| 291 |
hostname=dict(type='str', default="localhost"), |
| 292 |
function=dict(type='str', required=True, choices=choices()), |
| 293 |
parameters=dict(type='dict', required=False), |
| 294 |
force=dict(type='bool', default=False), |
| 295 |
), |
| 296 |
supports_check_mode=False, |
| 297 |
) |
| 298 |
result = dict(changed=False, msg='', out=[], error='') # Default out to list for consistent return type. |
| 299 |
params = module.params |
| 300 |
|
| 301 |
port = params['port'] |
| 302 |
username = params['username'] |
| 303 |
password = params['password'] |
| 304 |
hostname = params['hostname'] |
| 305 |
function = params['function'] |
| 306 |
parameters = params['parameters'] |
| 307 |
force = params['force'] |
| 308 |
|
| 309 |
if not HAS_SUDS_LIBRARY: |
| 310 |
module.fail_json( |
| 311 |
msg=missing_required_lib('suds'), |
| 312 |
exception=SUDS_LIBRARY_IMPORT_ERROR) |
| 313 |
|
| 314 |
# Validate arguments |
| 315 |
if requires_force(function) and force is False: |
| 316 |
module.fail_json(msg="Function '{0}' requires force: True".format(function)) |
| 317 |
|
| 318 |
# Determine if we should use local Unix socket connection |
| 319 |
# Use local socket connection if hostname is localhost and no username/password provided |
| 320 |
# True: Socket connection, False: SOAP connection |
| 321 |
is_socket = (hostname == "localhost" and |
| 322 |
username is None and |
| 323 |
password is None) |
| 324 |
|
| 325 |
if port is None: |
| 326 |
try: |
| 327 |
if is_socket: |
| 328 |
result['connection_type'] = 'socket' |
| 329 |
result['connection_url'] = "http://localhost/SAPHostControl/?wsdl" |
| 330 |
|
| 331 |
result_conn = connection(hostname, None, username, password, function, parameters, is_socket=True) |
| 332 |
else: |
| 333 |
result['connection_type'] = 'soap' |
| 334 |
|
| 335 |
# Try HTTPS and HTTP ports |
| 336 |
try: |
| 337 |
result['connection_url'] = 'http://{0}:1129/SAPHostControl/?wsdl'.format(hostname) |
| 338 |
result_conn = connection(hostname, "1129", username, password, function, parameters) |
| 339 |
except Exception: |
| 340 |
result['connection_url'] = 'http://{0}:1128/SAPHostControl/?wsdl'.format(hostname) |
| 341 |
result_conn = connection(hostname, "1128", username, password, function, parameters) |
| 342 |
except Exception as err: |
| 343 |
result['error'] = str(err) |
| 344 |
else: |
| 345 |
result['connection_type'] = 'soap' |
| 346 |
result['connection_url'] = 'http://{0}:{1}/SAPHostControl/?wsdl'.format(hostname, port) |
| 347 |
try: |
| 348 |
result_conn = connection(hostname, port, username, password, function, parameters, is_socket=False) |
| 349 |
except Exception as err: |
| 350 |
result['error'] = str(err) |
| 351 |
|
| 352 |
if result['error'] != '': |
| 353 |
result['msg'] = 'Function execution has failed. See error for more details.' |
| 354 |
module.fail_json(**result) |
| 355 |
|
| 356 |
conn_result = result_conn |
| 357 |
returned_data = recursive_dict(conn_result) if conn_result is not None else conn_result |
| 358 |
|
| 359 |
result['changed'] = not is_read_only_function(function) |
| 360 |
result['msg'] = "Successful execution of function: " + function |
| 361 |
result['out'] = [returned_data] |
| 362 |
|
| 363 |
module.exit_json(**result) |
| 364 |
|
| 365 |
|
| 366 |
if __name__ == '__main__': |
| 367 |
main() |