Weblogic Datasource Creation WLST script & Ansible Playbook

In this post, we are going to see how to create a weblogic data source using WLST script and an Ansible Playbook.

For this example, we are going to create a simple generic data source.

Weblogic Datasource WLST ansible

 

What is DataSource? Quick intro

A Datasource or Connection Pool is simply a buffer where the application server creates DB connections and stores for the application to use.

Applications deployed in weblogic make use of these data sources/connection pools by performing the JNDI Lookup

Once the application is done with its need. the connection should be reverted to the pool based on the connection pool configuration.

DB Connection can be managed either at the application level or at the application server (container) level.

  1. Application Managed:  Developers hard code the DB username and password data into the application directly or read it from the application properties file. Here the DB connections are managed by the Application itself. So there is no reliability over the DB Connections
  2. Container Managed:  Administrators create DataSource in application servers like weblogic/WebSphere and these data sources would be available at certain JNDI name for the applications to procure.  In this way, there is connection reliability and security as the DB info is stored at weblogic encrypted.

Data sources are the major element in the container-managed approach. Based on the need, there can be high availability and durability added to this data source. Like Multi datasource.

But for this post, we are going to use only a basic Generic data source.

 

Weblogic Datasource creation WLST script

Here is the script that creates a simple data source named DS2 with the jndi name jndi/ds2  and target the data source to the ms1 managed server.

Before using this WLST script. you might need to change the variables like user, pass and dsname and dbhost etc.

admin_user = "weblogic"
admin_pass = "weblogic1"
admin_server_url = "t3://localhost:8001"

dsname = "DS2"
dbhost = "ora.internal"
dbport = "1521"
dbsid = "mydb"

dbusername = "mydbuser"
dbpassword = "mydbP@ss"

targetname = "ms1"
targettype = "Server"

jndiname = "jndi/ds2"


print "------------------------------------------------------------"
print " Weblogic Datasource creation script"
print "------------------------------------------------------------"
print "Connecting to Admin Server"
connect(admin_user,admin_pass,admin_server_url)
edit()
startEdit()


print "Creating the Datasource"
cd('/')
cmo.createJDBCSystemResource(dsname)

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname)
cmo.setName(dsname)

print "Setting JNDIName"
cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDataSourceParams/'+dsname)
set('JNDINames',jarray.array([String(jndiname)], String))

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname)
cmo.setDatasourceType('GENERIC')

print "Configuring the JDBC URL"
cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDriverParams/'+dsname)
cmo.setUrl('jdbc:oracle:thin:@//'+dbhost+':'+dbport+'/'+dbsid)
cmo.setDriverName('oracle.jdbc.OracleDriver')

print "Encrypting the Password and Setting up DS credentials"
enpwd = encrypt(dbpassword)
set('PasswordEncrypted',enpwd)

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCConnectionPoolParams/'+dsname)
cmo.setTestTableName('SQL ISVALID\r\n\r\n\r\n\r\n')

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDriverParams/'+dsname+'/Properties/'+dsname)
cmo.createProperty('user')

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDriverParams/'+dsname+'/Properties/'+dsname+'/Properties/user')
cmo.setValue(dbusername)

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDataSourceParams/'+dsname)
cmo.setGlobalTransactionsProtocol('OnePhaseCommit')

cd('/JDBCSystemResources/'+dsname)
set('Targets',jarray.array([ObjectName('com.bea:Name='+targetname+',Type='+targettype)], ObjectName))
print "JDBC DataSource creation completed"
print "------------------------------------------------------------"

save()

 

How to execute this WLST script

change directory to your domain directory and bin location. run . ./setDomainEnv.sh for setting your Environment

run the java weblogic.WLST command followed by the WLST file name and path

cd domain/bin
. ./setDomainEnv.sh
java weblogic.WLST /path/to/create_jdbc.py

 

Ansible playbook version of this WLST

ansible-playbook version of this WLST can be found here

you can simply git checkout this repository and place it under the roles directory of your ansible setup and use this role.

Here is the main.yml file with tasks to copy the WLST script template and execute it with WLST command

---
# tasks file for setup-jms
- name: Copy the file with template feature
  tags: templatecopy 
  become: yes
  become_user: "{{ userid }}"
  template:
     src: "{{ item.src }}"
     dest: "{{ oracle_home }}/{{ item.dest }}"
     mode: 0755
  with_items:
    - { src: 'createjdbc.j2', dest: 'createjdbc.py' }

- name: Create JDBC resources
  tags: jmscreate
  become: yes
  become_user: "{{ userid }}"
  shell:  "source {{oracle_home}}/domains/{{domain_name}}/bin/setDomainEnv.sh && java weblogic.WLST {{ oracle_home }}/createjdbc.py"
  register: jmscreate
  args:
    chdir: "{{ oracle_home }}"
  # when: "{{ inventory_hostname == groups['launched'][0] }}"
  environment:
          USER_MEM_ARGS: "-Djava.security.egd=file:/tmp/big.random.file" 
          CONFIG_JVM_ARGS: "-Djava.security.egd=file:/tmp/big.random.file"
  

 

Here is the template version of the WLST script

admin_user = "{{admin_user}}"
admin_pass = "{{admin_pass}}"
admin_server_url = "{{admin_server_url}}"

dsname = "{{dsname}}"
dbhost = "{{dbhost}}"
dbport = "{{dbport}}"
dbsid = "{{dbsid}}"

dbusername = "{{dbusername}}"
dbpassword = "{{dbpassword}}"

targetname = "{{targetname}}"
targettype = "{{targettype}}"

jndiname = "{{jndiname}}"


print "------------------------------------------------------------"
print " Weblogic Datasource creation script"
print "------------------------------------------------------------"
print "Connecting to Admin Server"
connect(admin_user,admin_pass,admin_server_url)
edit()
startEdit()


print "Creating the Datasource"
cd('/')
cmo.createJDBCSystemResource(dsname)

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname)
cmo.setName(dsname)

print "Setting JNDIName"
cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDataSourceParams/'+dsname)
set('JNDINames',jarray.array([String(jndiname)], String))

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname)
cmo.setDatasourceType('GENERIC')

print "Configuring the JDBC URL"
cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDriverParams/'+dsname)
cmo.setUrl('jdbc:oracle:thin:@//'+dbhost+':'+dbport+'/'+dbsid)
cmo.setDriverName('oracle.jdbc.OracleDriver')

print "Encrypting the Password and Setting up DS credentials"
enpwd = encrypt(dbpassword)
set('PasswordEncrypted',enpwd)

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCConnectionPoolParams/'+dsname)
cmo.setTestTableName('SQL ISVALID\r\n\r\n\r\n\r\n')

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDriverParams/'+dsname+'/Properties/'+dsname)
cmo.createProperty('user')

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDriverParams/'+dsname+'/Properties/'+dsname+'/Properties/user')
cmo.setValue(dbusername)

cd('/JDBCSystemResources/'+dsname+'/JDBCResource/'+dsname+'/JDBCDataSourceParams/'+dsname)
cmo.setGlobalTransactionsProtocol('OnePhaseCommit')

cd('/JDBCSystemResources/'+dsname)
set('Targets',jarray.array([ObjectName('com.bea:Name='+targetname+',Type='+targettype)], ObjectName))
print "JDBC DataSource creation completed"
print "------------------------------------------------------------"

save()
activate()

These are variables required to be put in the /vars/main.yml file

# Paths
oracle_home: "/opt/oracle"
wlshome: "/opt/oracle/wlserver"
domainroot: "/opt/oracle/domains"
domain_name: "mwidomain"

# User ID of weblogic process 
userid: weblogic

admin_user : weblogic
admin_pass : weblogic1
admin_server_url : t3://localhost:8001

dsname : DS2
dbhost : ora.local
dbport : 1521
dbsid : mydb

# db credentials
dbusername : myuser
dbpassword : mypassw0rd


# Datasource target instance. Can be the Name of the Managed Server or Cluster
targetname : ms1

# Server or Cluster
targettype : Server
jndiname: jndi/ds2

 

I have not covered a few things in detail assuming that you are comfortable with Ansible roles and weblogic already. If you have any questions looking for support. Let me know in comments.

Cheers

Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading