| 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_snote |
| 26 |
|
| 27 |
short_description: This module will upload and (de)implements C(SNOTES) in a SAP S4HANA environment. |
| 28 |
|
| 29 |
version_added: "1.0.0" |
| 30 |
|
| 31 |
description: |
| 32 |
- The C(sap_snote) module depends on C(pyrfc) Python library (version 2.4.0 and upwards). |
| 33 |
Depending on distribution you are using, you may need to install additional packages to |
| 34 |
have these available. |
| 35 |
- This module will use the Function Group C(SCWB_API). |
| 36 |
- The C(TMS) must be configured at first. |
| 37 |
- Integrating SNOTES cannot be done via C(DDIC)- or C(SAP*)-User. |
| 38 |
options: |
| 39 |
state: |
| 40 |
description: |
| 41 |
- The decision what to do with the SNOTE. |
| 42 |
- Could be C('present'), C('absent') |
| 43 |
default: 'present' |
| 44 |
choices: |
| 45 |
- 'present' |
| 46 |
- 'absent' |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
conn_username: |
| 50 |
description: The required username for the SAP system. |
| 51 |
required: true |
| 52 |
type: str |
| 53 |
conn_password: |
| 54 |
description: The required password for the SAP system. |
| 55 |
required: true |
| 56 |
type: str |
| 57 |
host: |
| 58 |
description: The required host for the SAP system. Can be either an FQDN or IP Address. |
| 59 |
required: true |
| 60 |
type: str |
| 61 |
sysnr: |
| 62 |
description: |
| 63 |
- The system number of the SAP system. |
| 64 |
- You must quote the value to ensure retaining the leading zeros. |
| 65 |
required: false |
| 66 |
default: '01' |
| 67 |
type: str |
| 68 |
client: |
| 69 |
description: |
| 70 |
- The client number to connect to. |
| 71 |
- You must quote the value to ensure retaining the leading zeros. |
| 72 |
required: false |
| 73 |
default: '000' |
| 74 |
type: str |
| 75 |
snote_path: |
| 76 |
description: |
| 77 |
- The path to the extracted SNOTE txt file. |
| 78 |
- The File could be extracted from SAR package. |
| 79 |
- If C(snote_path) is not provided, the C(snote) parameter must be defined. |
| 80 |
- The SNOTE txt file must be at a place where the SAP System is authorized for. For example C(/usr/sap/trans/files). |
| 81 |
required: false |
| 82 |
type: str |
| 83 |
snote: |
| 84 |
description: |
| 85 |
- With the C(snote) paramter only implementation and deimplementation will work. |
| 86 |
- Upload SNOTES to the System is only available if C(snote_path) is provided. |
| 87 |
required: false |
| 88 |
type: str |
| 89 |
|
| 90 |
requirements: |
| 91 |
- pyrfc >= 2.4.0 |
| 92 |
|
| 93 |
author: |
| 94 |
- Rainer Leber (@rainerleber) |
| 95 |
''' |
| 96 |
|
| 97 |
EXAMPLES = r''' |
| 98 |
- name: test snote module |
| 99 |
hosts: localhost |
| 100 |
tasks: |
| 101 |
- name: implement SNOTE |
| 102 |
community.sap_libs.sap_snote: |
| 103 |
conn_username: 'DDIC' |
| 104 |
conn_password: 'Passwd1234' |
| 105 |
host: 192.168.1.100 |
| 106 |
sysnr: '01' |
| 107 |
client: '000' |
| 108 |
state: present |
| 109 |
snote_path: /usr/sap/trans/tmp/0002949148.txt |
| 110 |
|
| 111 |
- name: test snote module without path |
| 112 |
hosts: localhost |
| 113 |
tasks: |
| 114 |
- name: deimplement SNOTE |
| 115 |
community.sap_libs.sap_snote: |
| 116 |
conn_username: 'DDIC' |
| 117 |
conn_password: 'Passwd1234' |
| 118 |
host: 192.168.1.100 |
| 119 |
sysnr: '01' |
| 120 |
client: '000' |
| 121 |
state: absent |
| 122 |
snote: 0002949148 |
| 123 |
|
| 124 |
''' |
| 125 |
|
| 126 |
RETURN = r''' |
| 127 |
msg: |
| 128 |
description: A small execution description. |
| 129 |
type: str |
| 130 |
returned: always |
| 131 |
sample: 'SNOTE 000298026 implemented.' |
| 132 |
out: |
| 133 |
description: A complete description of the SNOTE implementation. If this is available. |
| 134 |
type: list |
| 135 |
elements: dict |
| 136 |
returned: always |
| 137 |
sample: '{ |
| 138 |
"RETURN": [{"ES_MSG": { "MSGNO": "000", "MSGTY": "", "MSGTXT": "", "MSGV1": "" }, |
| 139 |
"ET_MSG": [], |
| 140 |
"EV_RC": 0, |
| 141 |
"ET_MISSING_NOTES": [], |
| 142 |
"IT_FILENAME": [{"FILENAME": "/usr/sap/trans/tmp/0002980265.txt"}], |
| 143 |
"IT_NOTES": [{"NUMM": "0002980265", "VERSNO": "0000"}] |
| 144 |
}]}' |
| 145 |
''' |
| 146 |
|
| 147 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 148 |
from os import path as os_path |
| 149 |
import traceback |
| 150 |
try: |
| 151 |
from pyrfc import Connection |
| 152 |
except ImportError: |
| 153 |
HAS_PYRFC_LIBRARY = False |
| 154 |
ANOTHER_LIBRARY_IMPORT_ERROR = traceback.format_exc() |
| 155 |
else: |
| 156 |
ANOTHER_LIBRARY_IMPORT_ERROR = None |
| 157 |
HAS_PYRFC_LIBRARY = True |
| 158 |
|
| 159 |
|
| 160 |
def call_rfc_method(connection, method_name, kwargs): |
| 161 |
# PyRFC call function |
| 162 |
return connection.call(method_name, **kwargs) |
| 163 |
|
| 164 |
|
| 165 |
def check_implementation(conn, snote): |
| 166 |
check_implemented = call_rfc_method(conn, 'SCWB_API_GET_NOTES_IMPLEMENTED', {}) |
| 167 |
for snote_list in check_implemented['ET_NOTES_IMPL']: |
| 168 |
if snote in snote_list['NUMM']: |
| 169 |
return True |
| 170 |
return False |
| 171 |
|
| 172 |
|
| 173 |
def run_module(): |
| 174 |
module = AnsibleModule( |
| 175 |
argument_spec=dict( |
| 176 |
state=dict(default='present', choices=['absent', 'present']), |
| 177 |
conn_username=dict(type='str', required=True), |
| 178 |
conn_password=dict(type='str', required=True, no_log=True), |
| 179 |
host=dict(type='str', required=True), |
| 180 |
sysnr=dict(type='str', default="01"), |
| 181 |
client=dict(type='str', default="000"), |
| 182 |
snote_path=dict(type='str', required=False), |
| 183 |
snote=dict(type='str', required=False), |
| 184 |
), |
| 185 |
required_one_of=[('snote_path', 'snote')], |
| 186 |
supports_check_mode=False, |
| 187 |
) |
| 188 |
result = dict(changed=False, msg='', out={}, error='') |
| 189 |
raw = "" |
| 190 |
post_check = False |
| 191 |
|
| 192 |
params = module.params |
| 193 |
|
| 194 |
state = params['state'] |
| 195 |
conn_username = (params['conn_username']).upper() |
| 196 |
conn_password = params['conn_password'] |
| 197 |
host = params['host'] |
| 198 |
sysnr = (params['sysnr']).zfill(2) |
| 199 |
client = params['client'] |
| 200 |
|
| 201 |
path = params['snote_path'] |
| 202 |
snote = params['snote'] |
| 203 |
|
| 204 |
if not HAS_PYRFC_LIBRARY: |
| 205 |
module.fail_json( |
| 206 |
msg=missing_required_lib('pyrfc'), |
| 207 |
exception=ANOTHER_LIBRARY_IMPORT_ERROR) |
| 208 |
|
| 209 |
if conn_username == "DDIC" or conn_username == "SAP*": |
| 210 |
result['msg'] = 'User C(DDIC) or C(SAP*) not allowed for this operation.' |
| 211 |
module.fail_json(**result) |
| 212 |
|
| 213 |
# basic RFC connection with pyrfc |
| 214 |
try: |
| 215 |
conn = Connection(user=conn_username, passwd=conn_password, ashost=host, sysnr=sysnr, client=client) |
| 216 |
except Exception as err: |
| 217 |
result['error'] = str(err) |
| 218 |
result['msg'] = 'Something went wrong connecting to the SAP system.' |
| 219 |
module.fail_json(**result) |
| 220 |
|
| 221 |
# pre evaluation of parameters |
| 222 |
if path is not None: |
| 223 |
if path.endswith('.txt'): |
| 224 |
# splits snote number from path and txt extension |
| 225 |
snote = os_path.basename(os_path.normpath(path)).split('.')[0] |
| 226 |
else: |
| 227 |
result['msg'] = 'The path must include the extracted snote file and ends with txt.' |
| 228 |
module.fail_json(**result) |
| 229 |
|
| 230 |
pre_check = check_implementation(conn, snote) |
| 231 |
|
| 232 |
if state == "absent" and pre_check: |
| 233 |
raw = call_rfc_method(conn, 'SCWB_API_NOTES_DEIMPLEMENT', {'IT_NOTES': [snote]}) |
| 234 |
|
| 235 |
if state == "present" and not pre_check: |
| 236 |
if path: |
| 237 |
raw_upload = call_rfc_method(conn, 'SCWB_API_UPLOAD_NOTES', {'IT_FILENAME': [path], 'IT_NOTES': [snote]}) |
| 238 |
if raw_upload['EV_RC'] != 0: |
| 239 |
result['out'] = raw_upload |
| 240 |
result['msg'] = raw_upload['ES_MSG']['MSGTXT'] |
| 241 |
module.fail_json(**result) |
| 242 |
|
| 243 |
raw = call_rfc_method(conn, 'SCWB_API_NOTES_IMPLEMENT', {'IT_NOTES': [snote]}) |
| 244 |
queued = call_rfc_method(conn, 'SCWB_API_CINST_QUEUE_GET', {}) |
| 245 |
|
| 246 |
if queued['ET_MANUAL_ACTIVITIES']: |
| 247 |
raw = call_rfc_method(conn, 'SCWB_API_CONFIRM_MAN_ACTIVITY', {}) |
| 248 |
|
| 249 |
if raw: |
| 250 |
if raw['EV_RC'] == 0: |
| 251 |
post_check = check_implementation(conn, snote) |
| 252 |
if post_check and state == "present": |
| 253 |
result['changed'] = True |
| 254 |
result['msg'] = 'SNOTE "{0}" implemented.'.format(snote) |
| 255 |
if not post_check and state == "absent": |
| 256 |
result['changed'] = True |
| 257 |
result['msg'] = 'SNOTE "{0}" deimplemented.'.format(snote) |
| 258 |
else: |
| 259 |
result['msg'] = "Something went wrong." |
| 260 |
module.fail_json(**result) |
| 261 |
result['out'] = raw |
| 262 |
else: |
| 263 |
result['msg'] = "Nothing to do." |
| 264 |
|
| 265 |
module.exit_json(**result) |
| 266 |
|
| 267 |
|
| 268 |
def main(): |
| 269 |
run_module() |
| 270 |
|
| 271 |
|
| 272 |
if __name__ == '__main__': |
| 273 |
main() |