| 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_pyrfc |
| 26 |
|
| 27 |
short_description: Ansible Module for use of SAP PyRFC to execute SAP RFCs (Remote Function Calls) to SAP remote-enabled function modules |
| 28 |
|
| 29 |
version_added: "1.2.0" |
| 30 |
|
| 31 |
description: |
| 32 |
- This module will executes rfc calls on a sap system. |
| 33 |
- It is a generic approach to call rfc functions on a SAP System. |
| 34 |
- This module should be used where no module or role is provided. |
| 35 |
|
| 36 |
options: |
| 37 |
function: |
| 38 |
description: The SAP RFC function to call. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
parameters: |
| 42 |
description: The parameters which are needed by the function. |
| 43 |
required: true |
| 44 |
type: dict |
| 45 |
connection: |
| 46 |
description: The required connection details. |
| 47 |
required: true |
| 48 |
type: dict |
| 49 |
suboptions: |
| 50 |
ashost: |
| 51 |
description: The required host for the SAP system. Can be either an FQDN or IP Address. |
| 52 |
type: str |
| 53 |
required: true |
| 54 |
sysid: |
| 55 |
description: The systemid of the SAP system. |
| 56 |
type: str |
| 57 |
required: false |
| 58 |
sysnr: |
| 59 |
description: |
| 60 |
- The system number of the SAP system. |
| 61 |
- You must quote the value to ensure retaining the leading zeros. |
| 62 |
type: str |
| 63 |
required: true |
| 64 |
client: |
| 65 |
description: |
| 66 |
- The client number to connect to. |
| 67 |
- You must quote the value to ensure retaining the leading zeros. |
| 68 |
type: str |
| 69 |
required: true |
| 70 |
user: |
| 71 |
description: The required username for the SAP system. |
| 72 |
type: str |
| 73 |
required: true |
| 74 |
passwd: |
| 75 |
description: The required password for the SAP system. |
| 76 |
type: str |
| 77 |
required: true |
| 78 |
lang: |
| 79 |
description: The used language to execute. |
| 80 |
type: str |
| 81 |
required: false |
| 82 |
|
| 83 |
requirements: |
| 84 |
- pyrfc >= 2.4.0 |
| 85 |
|
| 86 |
author: |
| 87 |
- Sean Freeman (@seanfreeman) |
| 88 |
- Rainer Leber (@rainerleber) |
| 89 |
''' |
| 90 |
|
| 91 |
EXAMPLES = ''' |
| 92 |
- name: test the pyrfc module |
| 93 |
community.sap_libs.sap_pyrfc: |
| 94 |
function: STFC_CONNECTION |
| 95 |
parameters: |
| 96 |
REQUTEXT: "Hello SAP!" |
| 97 |
connection: |
| 98 |
ashost: s4hana.poc.cloud |
| 99 |
sysid: TDT |
| 100 |
sysnr: "01" |
| 101 |
client: "400" |
| 102 |
user: DDIC |
| 103 |
passwd: Password1 |
| 104 |
lang: EN |
| 105 |
''' |
| 106 |
|
| 107 |
RETURN = r''' |
| 108 |
result: |
| 109 |
description: The execution description. |
| 110 |
type: dict |
| 111 |
returned: always |
| 112 |
sample: {"ECHOTEXT": "Hello SAP!", |
| 113 |
"RESPTEXT": "SAP R/3 Rel. 756 Sysid: TST Date: 20220710 Time: 140717 Logon_Data: 000/DDIC/E"} |
| 114 |
''' |
| 115 |
|
| 116 |
import traceback |
| 117 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 118 |
from ..module_utils.pyrfc_handler import get_connection |
| 119 |
|
| 120 |
try: |
| 121 |
from pyrfc import ABAPApplicationError, ABAPRuntimeError, CommunicationError, LogonError |
| 122 |
except ImportError: |
| 123 |
HAS_PYRFC_LIBRARY = False |
| 124 |
PYRFC_LIBRARY_IMPORT_ERROR = traceback.format_exc() |
| 125 |
else: |
| 126 |
PYRFC_LIBRARY_IMPORT_ERROR = None |
| 127 |
HAS_PYRFC_LIBRARY = True |
| 128 |
|
| 129 |
|
| 130 |
def main(): |
| 131 |
msg = None |
| 132 |
params_spec = dict( |
| 133 |
ashost=dict(type='str', required=True), |
| 134 |
sysid=dict(type='str', required=False), |
| 135 |
sysnr=dict(type='str', required=True), |
| 136 |
client=dict(type='str', required=True), |
| 137 |
user=dict(type='str', required=True), |
| 138 |
passwd=dict(type='str', required=True, no_log=True), |
| 139 |
lang=dict(type='str', required=False), |
| 140 |
) |
| 141 |
|
| 142 |
argument_spec = dict(function=dict(required=True, type='str'), |
| 143 |
parameters=dict(required=True, type='dict'), |
| 144 |
connection=dict( |
| 145 |
required=True, type='dict', options=params_spec), |
| 146 |
) |
| 147 |
|
| 148 |
module = AnsibleModule( |
| 149 |
argument_spec=argument_spec, |
| 150 |
supports_check_mode=True, |
| 151 |
) |
| 152 |
|
| 153 |
function = module.params.get('function') |
| 154 |
func_params = module.params.get('parameters') |
| 155 |
conn_params = module.params.get('connection') |
| 156 |
|
| 157 |
if not HAS_PYRFC_LIBRARY: |
| 158 |
module.fail_json( |
| 159 |
msg=missing_required_lib('pyrfc'), |
| 160 |
exception=PYRFC_LIBRARY_IMPORT_ERROR) |
| 161 |
|
| 162 |
# Check mode |
| 163 |
if module.check_mode: |
| 164 |
msg = "function: %s; params: %s; login: %s" % ( |
| 165 |
function, func_params, conn_params) |
| 166 |
module.exit_json(msg=msg, changed=True) |
| 167 |
|
| 168 |
try: |
| 169 |
conn = get_connection(module, conn_params) |
| 170 |
result = conn.call(function, **func_params) |
| 171 |
error_msg = None |
| 172 |
except CommunicationError as err: |
| 173 |
msg = "Could not connect to server" |
| 174 |
error_msg = err.message |
| 175 |
except LogonError as err: |
| 176 |
msg = "Could not log in" |
| 177 |
error_msg = err.message |
| 178 |
except (ABAPApplicationError, ABAPRuntimeError) as err: |
| 179 |
msg = "ABAP error occurred" |
| 180 |
error_msg = err.message |
| 181 |
except Exception as err: |
| 182 |
msg = "Something went wrong." |
| 183 |
error_msg = err |
| 184 |
else: |
| 185 |
module.exit_json(changed=True, result=result) |
| 186 |
|
| 187 |
if msg: |
| 188 |
module.fail_json(msg=msg, exception=error_msg) |
| 189 |
|
| 190 |
|
| 191 |
if __name__ == '__main__': |
| 192 |
main() |