| 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_user |
| 26 |
short_description: This module will manage a user entities in a SAP S4/HANA environment |
| 27 |
version_added: "1.0.0" |
| 28 |
description: |
| 29 |
- The M(community.sap_libs.sap_user) module depends on C(pyrfc) Python library (version 2.4.0 and upwards). |
| 30 |
Depending on distribution you are using, you may need to install additional packages to |
| 31 |
have these available. |
| 32 |
- This module will use the following user BAPIs to manage user entities. |
| 33 |
- C(BAPI_USER_GET_DETAIL) |
| 34 |
- C(BAPI_USER_DELETE) |
| 35 |
- C(BAPI_USER_CREATE1) |
| 36 |
- C(BAPI_USER_CHANGE) |
| 37 |
- C(BAPI_USER_ACTGROUPS_ASSIGN) |
| 38 |
- C(BAPI_USER_PROFILES_ASSIGN) |
| 39 |
- C(BAPI_USER_UNLOCK) |
| 40 |
- C(BAPI_USER_LOCK) |
| 41 |
options: |
| 42 |
state: |
| 43 |
description: |
| 44 |
- The decision what to do with the user. |
| 45 |
default: 'present' |
| 46 |
choices: |
| 47 |
- 'present' |
| 48 |
- 'absent' |
| 49 |
- 'lock' |
| 50 |
- 'unlock' |
| 51 |
required: false |
| 52 |
type: str |
| 53 |
force: |
| 54 |
description: |
| 55 |
- Must be C('True') if the password or type should be overwritten. |
| 56 |
default: False |
| 57 |
required: false |
| 58 |
type: bool |
| 59 |
conn_username: |
| 60 |
description: The required username for the SAP system. |
| 61 |
required: true |
| 62 |
type: str |
| 63 |
conn_password: |
| 64 |
description: The required password for the SAP system. |
| 65 |
required: true |
| 66 |
type: str |
| 67 |
host: |
| 68 |
description: The required host for the SAP system. Can be either an FQDN or IP Address. |
| 69 |
required: true |
| 70 |
type: str |
| 71 |
sysnr: |
| 72 |
description: |
| 73 |
- The system number of the SAP system. |
| 74 |
- You must quote the value to ensure retaining the leading zeros. |
| 75 |
default: '00' |
| 76 |
type: str |
| 77 |
client: |
| 78 |
description: |
| 79 |
- The client number to connect to. |
| 80 |
- You must quote the value to ensure retaining the leading zeros. |
| 81 |
default: '000' |
| 82 |
type: str |
| 83 |
username: |
| 84 |
description: |
| 85 |
- The username. |
| 86 |
type: str |
| 87 |
required: true |
| 88 |
firstname: |
| 89 |
description: |
| 90 |
- The Firstname of the user in the SAP system. |
| 91 |
type: str |
| 92 |
required: false |
| 93 |
lastname: |
| 94 |
description: |
| 95 |
- The lastname of the user in the SAP system. |
| 96 |
type: str |
| 97 |
required: false |
| 98 |
email: |
| 99 |
description: |
| 100 |
- The email address of the user in the SAP system. |
| 101 |
type: str |
| 102 |
required: false |
| 103 |
password: |
| 104 |
description: |
| 105 |
- The password for the user in the SAP system. |
| 106 |
type: str |
| 107 |
required: false |
| 108 |
useralias: |
| 109 |
description: |
| 110 |
- The alias for the user in the SAP system. |
| 111 |
type: str |
| 112 |
required: false |
| 113 |
user_type: |
| 114 |
description: |
| 115 |
- The type for the user in the SAP system. |
| 116 |
- C('A') Dialog user, C('B') System User, C('C') Communication User, |
| 117 |
C('S') Service User, C('L') Reference User. |
| 118 |
- Must be in uppercase. |
| 119 |
type: str |
| 120 |
required: false |
| 121 |
default: 'A' |
| 122 |
choices: ['A', 'B', 'C', 'S', 'L'] |
| 123 |
company: |
| 124 |
description: |
| 125 |
- The specific company the user belongs to. |
| 126 |
- The company name must be available in the SAP system. |
| 127 |
type: str |
| 128 |
required: false |
| 129 |
profiles: |
| 130 |
description: |
| 131 |
- Assign profiles to the user. |
| 132 |
- Should be in uppercase, for example C('SAP_NEW') or C('SAP_ALL'). |
| 133 |
type: list |
| 134 |
elements: str |
| 135 |
default: [''] |
| 136 |
required: false |
| 137 |
roles: |
| 138 |
description: |
| 139 |
- Assign roles to the user. |
| 140 |
type: list |
| 141 |
elements: str |
| 142 |
default: [''] |
| 143 |
required: false |
| 144 |
|
| 145 |
requirements: |
| 146 |
- pyrfc >= 2.4.0 |
| 147 |
author: |
| 148 |
- Rainer Leber (@rainerleber) |
| 149 |
notes: |
| 150 |
- Does not support C(check_mode). |
| 151 |
''' |
| 152 |
|
| 153 |
EXAMPLES = r''' |
| 154 |
- name: Create SAP User |
| 155 |
community.sap_libs.sap_user: |
| 156 |
conn_username: 'DDIC' |
| 157 |
conn_password: 'Test123' |
| 158 |
host: 192.168.1.150 |
| 159 |
sysnr: '01' |
| 160 |
client: '000' |
| 161 |
state: present |
| 162 |
username: ADMIN |
| 163 |
firstname: first_admin |
| 164 |
lastname: last_admin |
| 165 |
email: [email protected] |
| 166 |
password: Test123456 |
| 167 |
useralias: ADMIN |
| 168 |
company: DEFAULT_COMPANY |
| 169 |
roles: |
| 170 |
- "SAP_ALL" |
| 171 |
|
| 172 |
- name: Force change SAP User |
| 173 |
community.sap_libs.sap_user: |
| 174 |
conn_username: 'DDIC' |
| 175 |
conn_password: 'Test123' |
| 176 |
host: 192.168.1.150 |
| 177 |
sysnr: '01' |
| 178 |
client: '000' |
| 179 |
state: present |
| 180 |
force: true |
| 181 |
username: ADMIN |
| 182 |
firstname: first_admin |
| 183 |
lastname: last_admin |
| 184 |
email: [email protected] |
| 185 |
password: Test123456 |
| 186 |
useralias: ADMIN |
| 187 |
company: DEFAULT_COMPANY |
| 188 |
roles: |
| 189 |
- "SAP_ALL" |
| 190 |
|
| 191 |
- name: Delete SAP User |
| 192 |
community.sap_libs.sap_user: |
| 193 |
conn_username: 'DDIC' |
| 194 |
conn_password: 'Test123' |
| 195 |
host: 192.168.1.150 |
| 196 |
sysnr: '01' |
| 197 |
client: '000' |
| 198 |
state: absent |
| 199 |
force: true |
| 200 |
username: ADMIN |
| 201 |
|
| 202 |
- name: Unlock SAP User |
| 203 |
community.sap_libs.sap_user: |
| 204 |
conn_username: 'DDIC' |
| 205 |
conn_password: 'Test123' |
| 206 |
host: 192.168.1.150 |
| 207 |
sysnr: '01' |
| 208 |
client: '000' |
| 209 |
state: unlock |
| 210 |
force: true |
| 211 |
username: ADMIN |
| 212 |
''' |
| 213 |
|
| 214 |
RETURN = r''' |
| 215 |
msg: |
| 216 |
description: A small execution description about the user action. |
| 217 |
type: str |
| 218 |
returned: always |
| 219 |
sample: 'User ADMIN created' |
| 220 |
out: |
| 221 |
description: A detailed description about the user action. |
| 222 |
type: list |
| 223 |
elements: dict |
| 224 |
returned: on success |
| 225 |
sample: [...,{ |
| 226 |
"RETURN": [ |
| 227 |
{ |
| 228 |
"FIELD": "BNAME", |
| 229 |
"ID": "01", |
| 230 |
"LOG_MSG_NO": "000000", |
| 231 |
"LOG_NO": "", |
| 232 |
"MESSAGE": "User ADMIN created", |
| 233 |
"MESSAGE_V1": "ADMIN", |
| 234 |
"MESSAGE_V2": "", |
| 235 |
"MESSAGE_V3": "", |
| 236 |
"MESSAGE_V4": "", |
| 237 |
"NUMBER": "102", |
| 238 |
"PARAMETER": "", |
| 239 |
"ROW": 0, |
| 240 |
"SYSTEM": "", |
| 241 |
"TYPE": "S" |
| 242 |
} |
| 243 |
], |
| 244 |
"SAPUSER_UUID_HIST": []}] |
| 245 |
''' |
| 246 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 247 |
import traceback |
| 248 |
import datetime |
| 249 |
try: |
| 250 |
from pyrfc import Connection |
| 251 |
except ImportError: |
| 252 |
HAS_PYRFC_LIBRARY = False |
| 253 |
PYRFC_LIBRARY_IMPORT_ERROR = traceback.format_exc() |
| 254 |
else: |
| 255 |
PYRFC_LIBRARY_IMPORT_ERROR = None |
| 256 |
HAS_PYRFC_LIBRARY = True |
| 257 |
|
| 258 |
|
| 259 |
def add_to_dict(target_dict, target_key, value): |
| 260 |
# Adds the given value to a dict as the key |
| 261 |
# check if the given key is in the given dict yet |
| 262 |
if target_key in target_dict: |
| 263 |
return False |
| 264 |
target_dict[target_key] = value |
| 265 |
return True |
| 266 |
|
| 267 |
|
| 268 |
def call_rfc_method(connection, method_name, kwargs): |
| 269 |
# PyRFC call function |
| 270 |
return connection.call(method_name, **kwargs) |
| 271 |
|
| 272 |
|
| 273 |
def build_rfc_user_params(username, firstname, lastname, email, raw_password, |
| 274 |
useralias, user_type, raw_company, user_change, force): |
| 275 |
"""Creates RFC parameters for Creating users""" |
| 276 |
# define dicts in batch |
| 277 |
params = dict() |
| 278 |
address = dict() |
| 279 |
password = dict() |
| 280 |
alias = dict() |
| 281 |
logondata = dict() |
| 282 |
company = dict() |
| 283 |
# for change parameters |
| 284 |
addressx = dict() |
| 285 |
passwordx = dict() |
| 286 |
logondatax = dict() |
| 287 |
companyx = dict() |
| 288 |
# define username |
| 289 |
add_to_dict(params, 'USERNAME', username) |
| 290 |
# define Address |
| 291 |
add_to_dict(address, 'FIRSTNAME', firstname) |
| 292 |
add_to_dict(address, 'LASTNAME', lastname) |
| 293 |
add_to_dict(address, 'E_MAIL', email) |
| 294 |
# define Password |
| 295 |
add_to_dict(password, 'BAPIPWD', raw_password) |
| 296 |
# define Alias |
| 297 |
add_to_dict(alias, 'USERALIAS', useralias) |
| 298 |
# define LogonData |
| 299 |
add_to_dict(logondata, 'GLTGV', datetime.date.today()) |
| 300 |
add_to_dict(logondata, 'GLTGB', '20991231') |
| 301 |
add_to_dict(logondata, 'USTYP', user_type) |
| 302 |
# define company |
| 303 |
add_to_dict(company, 'COMPANY', raw_company) |
| 304 |
params['LOGONDATA'] = logondata |
| 305 |
params['ADDRESS'] = address |
| 306 |
params['COMPANY'] = company |
| 307 |
params['ALIAS'] = alias |
| 308 |
params['PASSWORD'] = password |
| 309 |
# add change if user exists |
| 310 |
if user_change and force: |
| 311 |
add_to_dict(addressx, 'FIRSTNAME', 'X') |
| 312 |
add_to_dict(addressx, 'LASTNAME', 'X') |
| 313 |
add_to_dict(addressx, 'E_MAIL', 'X') |
| 314 |
# define Password |
| 315 |
add_to_dict(passwordx, 'BAPIPWD', 'X') |
| 316 |
# define LogonData |
| 317 |
add_to_dict(logondatax, 'USTYP', 'X') |
| 318 |
# define company |
| 319 |
add_to_dict(companyx, 'COMPANY', 'X') |
| 320 |
params['LOGONDATAX'] = logondatax |
| 321 |
params['ADDRESSX'] = addressx |
| 322 |
params['COMPANYX'] = companyx |
| 323 |
params['PASSWORDX'] = passwordx |
| 324 |
return params |
| 325 |
|
| 326 |
|
| 327 |
def user_role_assignment_build_rfc_params(roles, username): |
| 328 |
rfc_table = [] |
| 329 |
|
| 330 |
for role_name in roles: |
| 331 |
table_row = {'AGR_NAME': role_name} |
| 332 |
|
| 333 |
add_to_dict(table_row, 'FROM_DAT', datetime.date.today()) |
| 334 |
add_to_dict(table_row, 'TO_DAT', '20991231') |
| 335 |
|
| 336 |
rfc_table.append(table_row) |
| 337 |
|
| 338 |
return { |
| 339 |
'USERNAME': username, |
| 340 |
'ACTIVITYGROUPS': rfc_table |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
def user_profile_assignment_build_rfc_params(profiles, username): |
| 345 |
rfc_table = [] |
| 346 |
|
| 347 |
for profile_name in profiles: |
| 348 |
table_row = {'BAPIPROF': profile_name} |
| 349 |
rfc_table.append(table_row) |
| 350 |
|
| 351 |
return { |
| 352 |
'USERNAME': username, |
| 353 |
'PROFILES': rfc_table |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
def check_user(user_detail): |
| 358 |
if len(user_detail['RETURN']) > 0: |
| 359 |
for sub in user_detail['RETURN']: |
| 360 |
if sub['NUMBER'] == '124': |
| 361 |
return False |
| 362 |
return True |
| 363 |
|
| 364 |
|
| 365 |
def return_analysis(raw): |
| 366 |
change = False |
| 367 |
failed = False |
| 368 |
for state in raw['RETURN']: |
| 369 |
if state['TYPE'] == "E": |
| 370 |
if state['NUMBER'] == '224' or state['NUMBER'] == '124': |
| 371 |
change = False |
| 372 |
else: |
| 373 |
failed = True |
| 374 |
if state['TYPE'] == "S": |
| 375 |
if state['NUMBER'] != '029': |
| 376 |
change = True |
| 377 |
if state['TYPE'] == "W": |
| 378 |
if state['NUMBER'] == '049' or state['NUMBER'] == '047': |
| 379 |
change = True |
| 380 |
if state['NUMBER'] == '255': |
| 381 |
change = True |
| 382 |
return [{"change": change}, {"failed": failed}] |
| 383 |
|
| 384 |
|
| 385 |
def run_module(): |
| 386 |
module = AnsibleModule( |
| 387 |
argument_spec=dict( |
| 388 |
# logical values |
| 389 |
state=dict(default='present', choices=[ |
| 390 |
'absent', 'present', 'lock', 'unlock']), |
| 391 |
force=dict(type='bool', default=False), |
| 392 |
# values for connection |
| 393 |
conn_username=dict(type='str', required=True), |
| 394 |
conn_password=dict(type='str', required=True, no_log=True), |
| 395 |
host=dict(type='str', required=True), |
| 396 |
sysnr=dict(type='str', default="00"), |
| 397 |
client=dict(type='str', default="000"), |
| 398 |
# values for the new or existing user |
| 399 |
username=dict(type='str', required=True), |
| 400 |
firstname=dict(type='str', required=False), |
| 401 |
lastname=dict(type='str', required=False), |
| 402 |
email=dict(type='str', required=False), |
| 403 |
password=dict(type='str', required=False, no_log=True), |
| 404 |
useralias=dict(type='str', required=False), |
| 405 |
user_type=dict(default="A", |
| 406 |
choices=['A', 'B', 'C', 'S', 'L']), |
| 407 |
company=dict(type='str', required=False), |
| 408 |
# values for profile must a list |
| 409 |
# Example ["SAP_NEW", "SAP_ALL"] |
| 410 |
profiles=dict(type='list', elements='str', default=[""]), |
| 411 |
# values for roles must a list |
| 412 |
roles=dict(type='list', elements='str', default=[""]), |
| 413 |
), |
| 414 |
supports_check_mode=False, |
| 415 |
required_if=[('state', 'present', ['useralias', 'company'])] |
| 416 |
) |
| 417 |
result = dict(changed=False, msg='', out='') |
| 418 |
count = 0 |
| 419 |
raw = "" |
| 420 |
|
| 421 |
params = module.params |
| 422 |
|
| 423 |
state = params['state'] |
| 424 |
conn_username = (params['conn_username']).upper() |
| 425 |
conn_password = params['conn_password'] |
| 426 |
host = params['host'] |
| 427 |
sysnr = params['sysnr'] |
| 428 |
client = params['client'] |
| 429 |
|
| 430 |
username = (params['username']).upper() |
| 431 |
firstname = params['firstname'] |
| 432 |
lastname = params['lastname'] |
| 433 |
email = params['email'] |
| 434 |
password = params['password'] |
| 435 |
force = params['force'] |
| 436 |
if not params['useralias'] is None: |
| 437 |
useralias = (params['useralias']).upper() |
| 438 |
user_type = (params['user_type']).upper() |
| 439 |
company = params['company'] |
| 440 |
|
| 441 |
profiles = params['profiles'] |
| 442 |
roles = params['roles'] |
| 443 |
|
| 444 |
if not HAS_PYRFC_LIBRARY: |
| 445 |
module.fail_json( |
| 446 |
msg=missing_required_lib('pyrfc'), |
| 447 |
exception=PYRFC_LIBRARY_IMPORT_ERROR) |
| 448 |
|
| 449 |
# basic RFC connection with pyrfc |
| 450 |
try: |
| 451 |
conn = Connection(user=conn_username, passwd=conn_password, ashost=host, sysnr=sysnr, client=client) |
| 452 |
except Exception as err: |
| 453 |
result['error'] = str(err) |
| 454 |
result['msg'] = 'Something went wrong connecting to the SAP system.' |
| 455 |
module.fail_json(**result) |
| 456 |
|
| 457 |
# user details |
| 458 |
user_detail = call_rfc_method(conn, 'BAPI_USER_GET_DETAIL', {'USERNAME': username}) |
| 459 |
user_exists = check_user(user_detail) |
| 460 |
|
| 461 |
if state == "absent": |
| 462 |
if user_exists: |
| 463 |
raw = call_rfc_method(conn, 'BAPI_USER_DELETE', {'USERNAME': username}) |
| 464 |
|
| 465 |
if state == "present": |
| 466 |
user_params = build_rfc_user_params(username, firstname, lastname, email, password, useralias, user_type, company, user_exists, force) |
| 467 |
if not user_exists: |
| 468 |
raw = call_rfc_method(conn, 'BAPI_USER_CREATE1', user_params) |
| 469 |
|
| 470 |
if user_exists: |
| 471 |
# check for address changes when user exists |
| 472 |
user_no_changes = all((user_detail.get('ADDRESS')).get(k) == v for k, v in (user_params.get('ADDRESS')).items()) |
| 473 |
if not user_no_changes or force: |
| 474 |
raw = call_rfc_method(conn, 'BAPI_USER_CHANGE', user_params) |
| 475 |
|
| 476 |
call_rfc_method(conn, 'BAPI_USER_ACTGROUPS_ASSIGN', user_role_assignment_build_rfc_params(roles, username)) |
| 477 |
|
| 478 |
call_rfc_method(conn, 'BAPI_USER_PROFILES_ASSIGN', user_profile_assignment_build_rfc_params(profiles, username)) |
| 479 |
|
| 480 |
if state == "unlock": |
| 481 |
if user_exists: |
| 482 |
raw = call_rfc_method(conn, 'BAPI_USER_UNLOCK', {'USERNAME': username}) |
| 483 |
|
| 484 |
if state == "lock": |
| 485 |
if user_exists: |
| 486 |
raw = call_rfc_method(conn, 'BAPI_USER_LOCK', {'USERNAME': username}) |
| 487 |
|
| 488 |
# analyse return value |
| 489 |
if raw != '': |
| 490 |
analysed = return_analysis(raw) |
| 491 |
|
| 492 |
result['out'] = raw |
| 493 |
|
| 494 |
result['changed'] = analysed[0]['change'] |
| 495 |
for msgs in raw['RETURN']: |
| 496 |
if count > 0: |
| 497 |
result['msg'] = result['msg'] + '\n' |
| 498 |
result['msg'] = result['msg'] + msgs['MESSAGE'] |
| 499 |
count = count + 1 |
| 500 |
|
| 501 |
if analysed[1]['failed']: |
| 502 |
module.fail_json(**result) |
| 503 |
else: |
| 504 |
result['msg'] = "No changes where made." |
| 505 |
|
| 506 |
module.exit_json(**result) |
| 507 |
|
| 508 |
|
| 509 |
def main(): |
| 510 |
run_module() |
| 511 |
|
| 512 |
|
| 513 |
if __name__ == '__main__': |
| 514 |
main() |