Pages

Tuesday, January 26, 2010

Creating Users and Groups in Weblogic with WLST

A small blogpost how you can create users and groups with WLST scripting in Weblogic. This can be handy when you have a lot of application environments for Dev, Test ...) In WLST there is no default WLST function for creating users and groups, but in the serverConfig() we can lookup the right MBean and call the createUser, createGroup and addMemberToGroup operation.
Here is an example of the user creation phyton script.

serverConfig()

print 'lookup DefaultAuthenticator'

password = 'weblogic1'

atnr=cmo.getSecurityConfiguration().getDefaultRealm().lookupAuthenticationProvider('DefaultAuthenticator')

print 'create group App-MHS-Users'
group = 'App-MHS-Users'
atnr.createGroup(group,group)

users = ['user1','user2']
for user in users:
print 'create user: ',user
atnr.createUser(user,password,user)
atnr.addMemberToGroup(group,user)


print 'create group App-MHS-Admin'
group = 'App-MHS-Admin'
atnr.createGroup(group,group)

users = ['admin1','admin2']
for user in users:
print 'create user: ',user
atnr.createUser(user,password,user)
atnr.addMemberToGroup(group,user)


print 'create group App-MHS-SB'
group = 'App-MHS-SB'
atnr.createGroup(group,group)

users = ['sbuser1','sbuser2']
for user in users:
print 'create user: ',user
atnr.createUser(user,password,user)
atnr.addMemberToGroup(group,user)


To run this script we need a Weblogic environment ( can be remote ) and start wlst.cmd or wlst.sh

Go to the C:\Oracle\jdevstudio111120\wlserver_10.3\common\bin folder and start wlst.cmd

connect('weblogic','weblogic1','t3://server.domainname:7001')
execfile('/oracle/wls_scripts/createDefaultUserAndGroup.py')
disconnect()
exit()

that's all.

8 comments:

  1. This works great. Thanks a lot.

    ReplyDelete
  2. Cómo puedo capturar el error cuando el grupo ya existe?
    Intenté usar un try exception, pero no me funcionó.

    ReplyDelete
  3. Thanks. This helped me create users in bulk.

    ReplyDelete
  4. thanks m8, works like a charm!

    ReplyDelete
  5. Hi Edwin,

    I have the user creation script created. I have saved it in my C drive. How do I execute that. Do I have to place it in any path inside the domain. I am not sure how to execute the same.
    I saw that in ur post u have given the below path:
    execfile('/oracle/wls_scripts/createDefaultUserAndGroup.py') -- is this the local directory path? please suggest.

    this is my userCreation.py
    serverConfig() print ‘lookup DefaultAuthenticator’ atnr=cmo.getSecurityConfiguration().getDefaultRealm().lookupAuthenticationProvider(‘DefaultAuthenticator’) print ‘create group ServiceUserGroup’ group = ‘ServiceUserGroup’ atnr.createGroup(group,group) print ‘create user: ‘,’chaithra’ atnr.createUser(‘chaithra’,’cRU2rEQa’,’chaithra’) atnr.addMemberToGroup(‘ServiceUserGroup’,’chaithra’)
    print ‘User created'

    Thanks in advance.

    ReplyDelete
    Replies
    1. Hi,

      you need to start wlst.sh , connect() to your adminserver and start execfile('aaa.py')

      Delete
  6. Is there any way to create user and group offline, I mean without running server.

    ReplyDelete