In one of my recent Event engagement, I was expected to provision 50+ users in Weblogic. Creating users in Weblogic is not cumbersome process, it’s fairly easy. However, when you need to do in bulk it can take hours.
So, I just came up with simple WLST script which take an configuration file as input which contains all the server credentials required to make connectivity and list of users, description and group name etc and create and delete users in Weblogic.
Note: I have tested this script in Weblogic 12.2.1.2 but it should work in all Weblogic version as long as the beans which I have used inside python not been change in particular Weblogic Version.
Creating Users
createUserMultiGrp.properties
This is configuration file which keep server credentials and all users name along with description, group and password.
USER="weblogic" PASSWORD="XXXX1" ADMIN_URL="t3://XXX.XX.25.226:7001 userName10={ "name":"api-manager-user-10", "userPwd":"OracleiPa$$Us3r10", "description":"This user been created for PTF Event", "groupName1":"Administrators", "groupName2":"APIPlatformSysadmins", "groupName3":"APICSAdministrators" } userName11={ "name":"api-manager-user-11", "userPwd":"OracleiPa$$Us3r11", "description":"This user been created for PTF Event", "groupName1":"Administrators", "groupName2":"APIPlatformSysadmins", "groupName3":"APICSAdministrators" } userName12={ "name":"api-manager-user-12", "userPwd":"OracleiPa$$Us3r12", "description":"This user been created for PTF Event", "groupName1":"Administrators", "groupName2":"APIPlatformSysadmins", "groupName3":"APICSAdministrators" } userNameArray=(userName10,userName11,userName12)
createUserMultiGrp.py-
This is the python script which use above configuration file.
import sys #read properties file if len(sys.argv) != 2: print "Invalid Arguments :: Please provide input file" exit() try: print "Load properties file" properties=sys.argv[1] file=open(properties,'r') print "Read properties file" exec file print "Execute properties file" file.close except: exit() print 'userName Array Values are : ',userNameArray print 'Admin server user name : ',USER print 'ADMIN Server URL : ',ADMIN_URL connect(USER,PASSWORD,ADMIN_URL) edit() serverConfig() successCount=0 for userName in userNameArray: usrName=userName['name'] # print 'The Name of the user will be created : ',usrName pwd=userName['userPwd'] #print 'The password for this user : ',pwd desc=userName['description'] # print 'Description for this user : ',desc grpName1=userName['groupName1'] # print 'First Group Name for this user : ',grpName1 grpName2=userName['groupName2'] # print 'Second Group Name for this user : ',grpName2 grpName3=userName['groupName3'] # print 'Third Group Name for this user : ',grpName3 cd('/SecurityConfiguration/APICS01_domain/Realms/myrealm/AuthenticationProviders/DefaultAuthenticator') cmo.createUser(usrName,pwd,desc) print usrName,'- been created' cmo.addMemberToGroup(grpName1,usrName) cmo.addMemberToGroup(grpName2,usrName) cmo.addMemberToGroup(grpName3,usrName) print grpName1, grpName2, grpName3,'- been assigend to ',usrName successCount=successCount+1 print str(successCount)+" users successfully created"
Run this python Script
/u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh /home/opc/createUserMultiGrp.py /home/opc/createUserMultiGrp.properties
Delete Users in Weblogic
This WLST script work in same way as above and will user same configuration file which been used to create users as input and will delete all users listed in that configuration file.
deleteUser.py
import sys #read properties file if len(sys.argv) != 2: print "Invalid Arguments :: Please provide input file" exit() try: print "Load properties file" properties=sys.argv[1] file=open(properties,'r') print "Read properties file" exec file print "Execute properties file" file.close except: exit() print 'userName Array Values are : ',userNameArray print 'Admin server user name : ',USER print 'ADMIN Server URL : ',ADMIN_URL connect(USER,PASSWORD,ADMIN_URL) edit() serverConfig() successCount=0 for userName in userNameArray: usrName=userName['name'] # print 'The Name of the user will be created : ',usrName cd('/SecurityConfiguration/APICS01_domain/Realms/myrealm/AuthenticationProviders/DefaultAuthenticator') cmo.removeUser(usrName) print usrName,'- has been Deleted' successCount=successCount+1 print str(successCount)+" users successfully deleted"
Run this python Script
/u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh /home/opc/deleteUser.py /home/opc/createUserMultiGrp.properties
I hope this blog will help to someone who want to script the WLS users creation and deletion.
Happy Blogging 🙂
Hi Manish, Can we log off the users as well based on their logon time on server. If so please share the script for the same.that would be helpful
LikeLike
Hi Manish, Is it possible to log off user from weblogic from script based on their logon time on server. if so pls share your thoughts
LikeLike
Seeing errors with the scripts provided. please let us know the correct/updated scripts.
LikeLike
Hi Sundeep – what version of WebLogic are you running? This blog was writing in 2018 with WLS 12.2.1.2. I’m happy for you send me a direct message on LinkedIn (if possible) with any exceptions that you are getting – https://www.linkedin.com/in/lowe-jason.
LikeLike