Pages

Thursday, May 10, 2018

Create and Delete Users in Weblogic using WLST Script

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


1

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


2

I hope this blog will help to someone who want to script the WLS users creation and deletion.

Happy Blogging :)

No comments:

Post a Comment