Create a new database user using vRealize Automation XaaS
Recently I described how to deliver Database-as-a-Service using XaaS (Anything-as-a-Service) in vRealize Automation. In the meantime I added some add-ons to this.
It’s nice to have the ability to create a new database using self-service from vRealize Automation but when the database is running, users may want to edit it’s properties. User may want to add a user to a database to log in with or give users additional rights on the database. So I created a few XaaS services in vRealize Automation to do just that.
Running the script inside the guest OS with vRealize Orchestrator.
Next we need a script which creates a database on an existing SQL Server instance. I used the PowerShell script below:
# Database as a Service add-on - MS SQL Server
# Create a new database user with db role
# Erik Scholten (VMGuru)
# January 2017 - Version 1.0
# [COMPUTERNAME] is SQL Server name
# [DBUSER] is new dbuser account name
# [DBPASSWORD] is password for new dbuser account
# [DATABASE] is the database on which the account receives the rights
# [DBROLE] is the role the account gets on the database
# Import SQL Server module
Import-Module SQLPS -DisableNameChecking
# Replace this with your instance name
$instanceName = $env:[COMPUTERNAME]
$dbUserName = '[DBUSER]'
$dbPassword = '[DBPASSWORD]'
$Databases = '[DATABASE]'
$roleName = '[DBROLE]'
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
# Login already exists?
Write-Host("Create $dbUserName failed because username already exists.")
# Define user type and create the user
$Login = New-Object Microsoft.SqlServer.Management.Smo.Login($server, $dbUserName)
$Login.LoginType = 'SqlLogin'
$login.PasswordExpirationEnabled = $false
Write-Host("Login $dbUserName created successfully.")
foreach($databaseToMap in $Databases)
$database = $server.Databases[$databaseToMap]
Write-Host("Dropping user $dbUserName on $database.")
$dbUser = New-Object `
-TypeName Microsoft.SqlServer.Management.Smo.User `
-ArgumentList $database, $dbUserName
$dbUser.Login = $loginName
Write-Host("User $dbUser created successfully.")
#assign database role for a new user
$dbrole = $Database.Roles[$roleName]
Write-Host("User $dbUser successfully added to $roleName role.")
Just like the ‘Database-as-a-Service’ we’re going to use the VM Guest Script Manager to run this script inside the database server. You can download the Guest Script Manager package from the VMware Development Center. Once downloaded, import the package into your Orchestrator instance.
Now we need to add the script above to the vRealize Orchestrator by running the ‘Guest Script Manager/Script Management/Add script configuration‘ workflow. Name your imported script ‘Create a database user (code)‘ (I added ‘code’ to the name to distinguish between the code and the actual workflow). Select ‘PowerShell’ as the script type and copy+paste this code in the script content window.
How to call the script?
So we’ve imported the script, now we need to create a workflow to run this script and input the variables which are mentioned in the script comments. This workflow is the actual workflow we’re going to call as XaaS service from vRealize Automation.
So create a new workflow called ‘Create a database user‘.
- Drag and drop the workflow element ‘Run script in VM guest’ on your workflow canvas.
- Click on Setup button at the top right to set the workflow parameters.
- Set ‘scriptConfiguration’ as mapping type ‘Value’, click on input value and type in the filter the name of the script (‘Create a database user (code)’). Select it.
- Set ‘scriptVariables‘ mapping type to ‘Value‘ but do not set any input value.
- Click ‘Promote‘.
The result should look like this.
Create the XaaS workflow and blueprint
Now we have to retrieve input to fill the script variables mentioned in the script comments. The following input variables need to be provided:
- [COMPUTERNAME] which is the SQL Server name.
- [DBUSER] is the new dbUser account name.
- [DBPASSWORD] is password for new dbuser account.
- [DATABASE] is the database on which the account action is performed.
- [DBROLE] is the role the account gets on the database.
The For each of these variables we need to add an Action element to the workflow which maps a workflow input. This results in an input prompt when running this workflow.
To do this:
- Map DatabaseName:
- Drag and drop an Action Element on the canvas and place it before the ‘Run script in VM guest’.
- In the Search Choose the ‘addScriptVariableToArrayOfScriptVariables‘ action.
- Click on Setup button at the top right to set the workflow parameters.
- Rename “stringToReplace” with ‘ComputerNameVar‘.
- Set the mapping type to ‘Value‘.
- Select ‘Input value’ and set it to ‘[COMPUTERNAME]’.
- Rename “replacingString” with ‘ComputerName‘.
- Leave the mapping type on ‘Input‘.
- Set the Output Parameter Name to ‘scriptVariables‘.
- Click ‘Promote’.
Do the same for [DBUSER], [DBPASSWORD], [DATABASE] and [DBROLE].
Each mapping should look like this (Example: [DATABASE]):
Your completed workflow should look like this:
Now we need to create an XaaS service in vRealize Automation which calls the workflow that we’ve just created.
Go to ‘vRealize Automation/Design/XaaS Blueprints‘ and select ‘New Blueprint‘. Select the Orchestrator workflow you’ve just created.
Name your XaaS blueprint e.g. ‘Create a database user‘ and version it (1.0.0). Give the inputs nice user friendly input values so users who request this will know what they need to enter in which field. I also created some nice dropdown lists so users do not have to guess the values but select them from a predefined list. For now these are all static values but you can populate the field values dynamically for a more agile approach.
The last step is publishing the XaaS Blueprint, set the correct entitlements and enjoy a new Anything-as-a-Service. You can now request a new database administrator account without bothering the database administrators or ever opening SQL Server Management Studio.
Other articles in the series vRealize Automation:
- Infoblox & vRealize Automation, Setup IPAM endpoint
- Infoblox & vRealize Automation, Install Infoblox IPAM plugin
- Setup provisioning of vCloud Air workloads
- Setup a vCloud Air Endpoint in vRealize Automation
- Setup Postgres database high availability for vRealize Automation
- Setup vRealize Automation Cost profiles & currency
- Speed up vRealize Automation life cycle updates
- Configure vRealize Automation not to delete virtual machines
- Track infrastructure changes with vRealize Automation Data Collection
- MS SQL high availability support for vRealize Automation
- Setup the vRealize Automation prerequisites for IaaS
- vRealize Automation: How to …
- Downgrade the vRealize Automation license
- vRealize Automation 7 – New Architecture & Installation
- vRealize Automation 7 – Improved Authentication
- vRealize Automation 7 – Converged Blueprints
- vRealize Automation 7 – LifeCycle Extensibility
- How to transfer vRealize Automation payload to Orchestrator
- Automation is not difficult with vRealize Automation 7
- vRA custom host name using Orchestrator and Event Broker
- How to deliver Database-as-a-Service with vRealize Automation
- Prepare Windows for vRA software deployment
- Missing endpoints after upgrading to vRealize Automation 7.3
- How to run Containers as a Service – Part 1
- How to run Containers as a Service – Part 2
- Infoblox & vRealize Automation, IP Address Management (IPAM) made easy
- How to run Containers as a Service – Secure Docker communications
- Create a new database user using vRealize Automation XaaS
- Infoblox & vRealize Automation, Infoblox NIOS setup
- Infoblox & vRealize Automation, vRA setup
- vRealize Automation failed requests monitoring
- vRealize Automation 7.2 released
- How to specify a deployment target when using vRealize Automation
- vRealize Suite just became a lot easier with Lifecycle Manager
- How to deliver Database-as-a-Service with vRealize… by Erik Scholten
- Downgrade the vRealize Automation license by Erik Scholten
- MS SQL high availability support for vRealize Automation by Erik Scholten
- Setup the vRealize Automation prerequisites for IaaS by Erik Scholten
- Setup Postgres database high availability for vRealize… by Erik Scholten
The founder and driving force behind VMGuru. With over 20 years experience in IT, he now works as a Cloud Management Specialist at VMware Benelux. He worked as technical consultant, pre-sales and solutions architect for several systems integrators.
He’s a long time VMware VCP, VCP Desktop, VCA, VSP and VTSP, vExpert Cloud (2017) and 9 year vExpert (2009 – 2017).