Getting Started: vRealize Orchestrator Script Environments [CB10098]

  1. Introduction
  2. Prerequisite
  3. Procedure
  4. Calling modules & variables
    1. For Node.js
    2. For Python
    3. For PowerShell
  5. Sample Node.js Script

Introduction

Do you use a lot of Polyglot scripts in vRO? Are you tired of creating bundles every time you work on a Python, Node.js or PowerShell script which uses modules and libraries which are not provided out-of-the-box by vRealize Orchestrator? Probably vRO guys at VMware heard your prayers this time.

vRO 8.8 onwards, you can now add modules and libraries directly as a dependency in your vRO actions and scriptable tasks. How cool is that!

As we know, in earlier versions, you could only add dependencies by adding them as a ZIP package which is not only a tiring additional steps, but also, editing and understanding those scripts becomes a real nightmare. But not any more.

In this post, we will see a detailed procedure on how to setup an script environment in your vRO (>8.8). I am going with Node.js but almost similar process can be followed for other languages as well. We will use an advanced date & time library called MomentJS available at https://momentjs.com/ but you can use any other module or library of your choice for that matter.


Note Similarly to other vRealize Orchestrator objects such as workflows and actions, environments can be exported to other vRealize Orchestrator deployments as part of a package, which means they are also a part of version control.


Prerequisite

  • vRealize Orchestrator 8.8 or greater

Procedure

  • Log in to the vRealize Orchestrator Client.
  • Navigate to Assets > Environments, and click New Environment.
  • Under the General tab, enter a name for your environment.
  • (Optional) Enter a description, version number, tags, and group permissions for the action.
  • Under the Definition tab, Click on Add button under Dependencies.

You can also change the Memory Limit to 128, 512, 1024 etc. depending on the number and size of packages that you will be using. In my personal experience, using PowerShell modules will require more than default.

  • Provide the package name and version that you want to install. For Node.js, passing latest will get you the most recent package.

Tip The package name is same as what you would use with package managers like npm while installing that package.


  • Once you click Create button, you should see Environment successfully created.
  • Under the Download Logs tab, check if the libraries are already installed.

Here, I have installed two modules, moment and moment-timezone as you can see from the logs.

  • In the Environment variables, you can provide a variable that you want to use as a part of this environment.
  • Create an action or a workflow with scriptable item, Select Runtime Environment as the one that you have created. I have selected moment.
  • Play with your script. Don’t forget to call the modules and environment variables.

Calling modules & variables

For Node.js

const myModule = require('moment');
const envVar = process.env.VAR_NAME;

For Python

import myModule
os.environ.get('VAR_NAME')

For PowerShell

Import-Module myModule
$env:VAR_NAME

Sample Node.js Script

exports.handler = (context, inputs, callback) => {
//——————-Don't edit above it————————//
const moment = require('moment'); // **IMPORTANT**
const tz = require('moment-timezone'); // **IMPORTANT**
const indianTimeZone = process.env.TIMEZONE_IN; // import Env variable in Node.js
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
console.log(moment().format('dddd'));
console.log(moment().format("MMM Do YY"));
console.log(moment().format('YYYY [escaped] YYYY'));
console.log(moment().format());
var jul = moment("2022-07-20T12:00:00Z");
var dec = moment("2022-12-20T12:00:00Z");
console.log(jul.tz('America/Los_Angeles').format('ha z')); // 5am PDT
console.log(dec.tz(indianTimeZone).format('ha z')); // 4am PST
//——————-Don't edit below it————————//
callback(undefined, {
status: "done"
});
}

That’s it on this post.

List of PowerShell Modules in vRO 8.x

Here is the list of all the PowerShell Modules that comes preinstalled in vRO 8.x. In later versions of vRO, we have 2 PowerShell environments 6.x and 7.x. There is a slight difference in the modules as you can notice below in both the versions.

Advertisements
For Powershell 7.xFor Powershell 6.x
PowerNSXPowerNSX
PowervRAPowervRA
VMware.CloudServicesVMware.DeployAutomation
VMware.DeployAutomationVMware.ImageBuilder
VMware.ImageBuilderVMware.PowerCLI
VMware.PowerCLIVMware.Vim
VMware.VimVMware.VimAutomation.Cis.Core
VMware.VimAutomation.Cis.CoreVMware.VimAutomation.Cloud
VMware.VimAutomation.CloudVMware.VimAutomation.Common
VMware.VimAutomation.CommonVMware.VimAutomation.Core
VMware.VimAutomation.CoreVMware.VimAutomation.Hcx
VMware.VimAutomation.HcxVMware.VimAutomation.HorizonView
VMware.VimAutomation.HorizonViewVMware.VimAutomation.License
VMware.VimAutomation.LicenseVMware.VimAutomation.Nsxt
VMware.VimAutomation.NsxtVMware.VimAutomation.Sdk
VMware.VimAutomation.SdkVMware.VimAutomation.Security
VMware.VimAutomation.SecurityVMware.VimAutomation.Srm
VMware.VimAutomation.SrmVMware.VimAutomation.Storage
VMware.VimAutomation.StorageVMware.VimAutomation.StorageUtility
VMware.VimAutomation.StorageUtilityVMware.VimAutomation.Vds
VMware.VimAutomation.VdsVMware.VimAutomation.Vmc
VMware.VimAutomation.VmcVMware.VimAutomation.vROps
VMware.VimAutomation.vROpsVMware.VumAutomation
VMware.VimAutomation.WorkloadManagementMicrosoft.PowerShell.Host
VMware.VumAutomationMicrosoft.PowerShell.Management
Microsoft.PowerShell.HostMicrosoft.PowerShell.Security
Microsoft.PowerShell.ManagementMicrosoft.PowerShell.Utility
Microsoft.PowerShell.SecurityPackageManagement
Microsoft.PowerShell.UtilityPowerShellGet
PackageManagementPSDesiredStateConfiguration
PowerShellGetPSReadLine
PSReadLinePSReadLine

As you might have noticed, in 7.x, we have two additional modules i.e. VMware.CloudServices & VMware.VimAutomation.WorkloadManagement, but misses out PSDesiredStateConfiguration that is only available in 6.x.

Check out other list of modules here:

for Python: https://cloudblogger.co.in/2022/03/08/list-of-all-available-python-modules-in-vro/

for NodeJs: https://cloudblogger.co.in/2022/03/15/list-of-available-node-js-modules-in-vro/

Advertisements

4 Pillars of VMware Automation

vRealize Orchestrator as a tool is good and really good but it has its own limits just like any other automation tool out there. It’s way of implementation, scope of reach and underlying capabilities all impose restrictions when we think of a complete Automation solution. That doesn’t mean we can replace it, it has it’s own unique place in VMware ecosystem. It is, in fact, the sentinel of VMware Automation. That means, we just have to find the right set of tools to make it more complete.

And I think when we talk about VMware, it’s always tip-toed, which means they have already figured out what would work best for them. In this article, I will discuss about all those tools that will help any automation engineer out there to make a complete automation solution for a VMware environment for all kinds of needs, recommended by VMware itself.

I have recently read somewhere that VMware automation includes use of three tools – Ansible, Terraform and vRO. I would like to add PowerCLI to this boy’s band. I think VMware implicitly means it.

So, according to me, the 4 pillars of VMware Automation are:

  • vRealize Orchestrator
  • PowerCLI
  • Ansible
  • Terraform

If you want to be an ideal Automation Engineer who can automate almost anything when it comes to VMware, must know these 4 skills. Let’s know a little about them.

PowerCLI

PowerCLI is a powerful command-line tool that lets you automate all aspects of vSphere management, including network, storage, VM, guest OS and more. PowerCLI is distributed as PowerShell modules, and includes over 500 PowerShell cmdlets for managing and automating vSphere and Cloud Director and many other VMware tools, along with documentation and samples.

Below is a PowerCLI script to create new VM from a template in vCenter Server.

# PowerCLI script to create Virtual Machines
# ===========================================
# It can create new VM or deploy VM from template if template name is specified.
#
#————————————————
# Start of script parameters section
#
# vCenter Server configuration
$vcenter = “vc01.home.uw.cz“
$vcenteruser = “readwrite“
$vcenterpw = “readwrite“
#
# Specify number of VMs you want to create
$vm_count = “5“
#
# Specify vCenter Server datastore
$Datastore = “TEST“
#
# Specify vCenter Server Virtual Machine & Templates folder
$Folder = “TEST”
#
# Specify the vSphere Cluster
$Cluster = “TEST“
#
# Specify the VM name to the left of the – sign
$VM_prefix = “XTEST-“
#
# Specify the VM provisioning type (sync/async) - true = async (parallel), false = sync (sequentional)
$VM_create_async = $false
#
# Specify if VM should be created from template
$VM_from_template="TEST_template"
#
# Specify if VM should be Power On 
$VM_power_on = $false
#
# Parameters below are used only for new VM. 
# VM created from template has these parameters included in the template.
#
# Specify number of VM CPUs
$numcpu = “1“
#
# Specify number of VM MB RAM
$MBram = “512“
#
# Specify VM disk size (in MB)
$MBguestdisk = “1000“
#
# Specify VM disk type, available options are Thin, Thick, EagerZeroedThick
$Typeguestdisk =”Thick“
#
# Specify VM guest OS
$guestOS = “winNetStandardGuest“
#
# End of script parameter section
#—————————————— #
clear-host
$o = Add-PSSnapin VMware.VimAutomation.Core
$o = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
#
# Connect to vCenter Server
write-host “Connecting to vCenter Server $vcenter” -foreground green
$vc = connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw
#
#$O_cluster=Get-Cluster $Cluster
1..$vm_count | foreach {
  $VM_postfix=”{0:D2}” -f $_
  $VM_name= $VM_prefix + $VM_postfix
  #$O_ESXi=Get-Cluster $Cluster_name | Get-VMHost -state connected | Get-Random
  if ($VM_from_template -eq "") {
    write-host “Creation of VM $VM_name initiated”  -foreground green
    New-VM -RunAsync:$VM_create_async -Name $VM_Name -ResourcePool $Cluster -numcpu $numcpu -MemoryMB $MBram -DiskMB $MBguestdisk -DiskStorageFormat $Typeguestdisk -Datastore $Datastore -GuestId $guestOS -Location $Folder
  } else {
    write-host “Deployment of VM $VM_name from template $VM_from_template initiated”  -foreground green
    New-VM -RunAsync:$VM_create_async -Name $VM_Name -Template $VM_from_template -ResourcePool $Cluster -Datastore $Datastore -Location $Folder
  }
  if ($VM_power_on) {
    write-host “Power On of the  VM $VM_name initiated" -foreground green
    Start-VM -VM $VM_name -confirm:$false -RunAsync
  }
}

Terraform

Terraform is an open-source infrastructure as code software tool created by HashiCorp. Users define and provide data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language, or optionally JSON. Terraform can work with vSphere, Cloud Director (vcd), NSX-T, vRA, Wavefront, Carvel and many other tools.

Below is an example of a Terraform .tf file which will deploy a windows machine in a vSphere environment with available features. Read more about this file here.

module "example-server-windowsvm-advanced" {
  source            = "Terraform-VMWare-Modules/vm/vsphere"
  version           = "X.X.X"
  dc                = "Datacenter"
  vmrp              = "cluster/Resources" #Works with ESXi/Resources
  vmfolder          = "Cattle"
  datastore_cluster = "Datastore Cluster" #You can use datastore variable instead
  vmtemp            = "TemplateName"
  instances         = 2
  vmname            = "AdvancedVM"
  vmnameformat      = "%03d" #To use three decimal with leading zero vmnames will be AdvancedVM001,AdvancedVM002
  domain            = "somedomain.com"
  network = {
    "Name of the Port Group in vSphere" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""]; You can also use a CIDR annotation;
    "Second Network Card"               = ["", ""]
  }
  ipv4submask  = ["24", "8"]
  network_type = ["vmxnet3", "vmxnet3"]
  tags = {
    "terraform-test-category" = "terraform-test-tag"
  }
  data_disk = {
    disk1 = {
      size_gb                   = 30,
      thin_provisioned          = false,
      data_disk_scsi_controller = 0,
    },
    disk2 = {
      size_gb                   = 70,
      thin_provisioned          = true,
      data_disk_scsi_controller = 1,
      datastore_id              = "datastore-90679"
    }
  }
  scsi_bus_sharing = "physicalSharing" // The modes are physicalSharing, virtualSharing, and noSharing
  scsi_type        = "lsilogic"        // Other acceptable value "pvscsi"
  scsi_controller  = 0                 // This will assign OS disk to controller 0
  dns_server_list  = ["192.168.0.2", "192.168.0.1"]
  enable_disk_uuid = true
  vmgateway        = "192.168.0.1"
  auto_logon       = true
  run_once         = ["command01", "command02"] // You can also run Powershell commands
  orgname          = "Terraform-Module"
  workgroup        = "Module-Test"
  is_windows_image = true
  firmware         = "efi"
  local_adminpass  = "Password@Strong"
}
output "vmnames" {
  value = module.example-server-windowsvm-advanced.VM
}
output "vmnameswip" {
  value = module.example-server-windowsvm-advanced.ip
}

Ansible

Ansible is an agent-less IT automation engine that works over secure shell (SSH). Once installed, there are no databases to configure and there are no daemons to start or keep running.

Ansible performs automation and orchestration via Playbooks. Playbooks are a YAML definition of automation tasks that describe how a particular piece of work needs to be done. A playbook consists of a series of ‘plays’ that define automation across a set of hosts, known as ‘inventory’. Each ‘play’ consists of multiple ‘tasks’ that can target one or many hosts in the inventory. Each task is a call to an Ansible module.

Below is example of an Ansible playbook (YAML) which will create a new vSphere VM from a template using a module. Read more about this file here.

---
  # vim: set ft=ansible et ts=2 sw=2:
  #
  # Create a new VM from a template using the new vmware_guest module
  - name: VM from template
    hosts: vmcreate
    gather_facts: false
    connection: local
    vars:
      vcenter_hostname: vcsa.box
      esxhost: esx1.box
      datastore: datastore1
      network: "VMnetwork"
      vmtemplate: CentOS7-Base
      vmcluster: LAN
      notes: Created by Ansible
      dumpfacts: False
    tasks:
      - name: Check for required variables
        fail: msg="Must pass name and group to -e"
        when: name is not defined or group is not defined
      - name: Check for vSphere access parameters
        fail: msg="Must set vcenter_user and vcenter_pass in a Vault"
        when: (vcenter_user is not defined) or (vcenter_pass is not defined)
        
      - name: Create VM from template
        vmware_guest:
          validate_certs: False
          hostname: "{{ vcenter_hostname }}"
          username: "{{ vcenter_user }}"
          password: "{{ vcenter_pass }}"
          esxi_hostname: "{{ esxhost }}"
          datacenter: BOX
          name: "{{ name }}"
          template: "{{ vmtemplate }}"
          disk:
            - size_gb: 10
              type: thin
              datastore: "{{ datastore }}"
          nic:
            - type: vmxnet3
              network: "{{ network }}"
          hardware:
            memory_mb: "{{ vm_memory | default(1024) }}"
          wait_for_ip_address: True
          state: present
        register: newvm
        
      - name: IP address info
        debug:
          msg: "{{ newvm.instance.ipv4 }} {{ name }}"


vRealize Orchestrator

vRealize Orchestrator is an automation platform that comes in the form of a virtual appliance. You can automate various infrastructure tasks using workflows that can be combined in a number of ways. While it is perfectly integrated with VMware vSphere and offers extensive content for it, you can automate almost any IT process through partner provided workflows for tasks outside of the VMware world such as Active Directory, SQL Server, REST and lot more. It also offers hybrid cloud capabilities with newer plugins from GCP and AWS.

Below is an example of vRO workflow to remove old snapshots for a VM in vSphere.

vRO workflow: Remove virtual machine snapshots older than X days/hours. –  BlanketVM
A Typical Workflow in vRO 8.x

Other Tools

I know there are many other tools like SaltStack, Chef, Puppet, Packer etc. which can help us to attain even more precise levels of automation. It’s great if you use them. Also, vRA is not part of 4 pillars because of obvious reasons. Reason being a Cloud Management Portal before being an standalone automation tool. It can ultimately make uses of all these tools that we have just discussed to give 1 complete solution to the customer As-a-service.

References

How to publish your PowerShell script to PSGallery online

You ever created a PS script and wanted it to share. What best place would it be other than the PowerShell Gallery itself!

Let’s go through this article to know how I listed my own PS script on PSGallery.

  • Click Create and provide Key Name = script_name and glob pattern = *
  • Now, I have my script ready with me. However, before uploading the script, I must add some metadata to my script like Author, Description, Version etc.
  • For that, I have used New-ScriptFileInfo method. I put the values as per my need and run them inside Powershell console.
$Parms = @{
  Path = "C:\Users\mayank.goyal1\Downloads\scriptInfo.ps1"
  Version = "2.1.0"
  Author = "Mayank Goyal"
  Description = "This script converts vRO Package directly into JSDoc website by connecting to vRO. Requires nodejs and jsdoc module installed. Create new files *.html & *.js directly from vRO with JSDoc annotation. It consumes a package name which should exist in vRO with all the Actions you want to document and creates .js files with JSDoc Annotations."
  ProjectUri = "https://github.com/imtrinity94/vRODoc"
}
New-ScriptFileInfo @Parms
Get-Content -Path C:\Users\mayank.goyal1\Downloads\scriptInfo.ps1
  • This creates a new file C:\Users\mayank.goyal1\Downloads\scriptInfo.ps1. Opening this file show me this content.

<#PSScriptInfo
.VERSION 2.1.0
.GUID 7202df73-3b8c-4616-adeb-b6d9b97a63c7
.AUTHOR Mayank Goyal
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI https://github.com/imtrinity94/vRODoc
.ICONURI
.EXTERNALMODULEDEPENDENCIES 
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<# 
.DESCRIPTION 
 This script converts vRO Package directly into JSDoc website by connecting to vRO. Requires nodejs and jsdoc module installed. Create new files *.html & *.js directly from vRO with JSDoc annotation. It consumes a package name which should exist in vRO with all the Actions you want to document and creates .js files with JSDoc Annotations. 
#> 
  • Copy this file’s content and put it on top of your original script.
  • Now, it’s time to publish our file. Use Publish-Script command along with file path and API key that I have handy already.
Publish-Script -Path "C:\Users\mayank.goyal1\Downloads\vRODoc.ps1" -NuGetApiKey oy2hxalemh46n4ghty*****************************64 -Repository PSGallery
  • This should have worked, but it didn’t. Got this error no matter from where I run it.

Failed to publish script ‘vRODoc’: ‘The underlying connection was closed: An unexpected error occurred on a send.

  • The first problem might be that my system runs TLS11 instead of TLS12, to fix this run the following command:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

The second problem might be that my PowerShellGet Module is not up to date. To fix this I run the following command:

Install-Module PowerShellGet -Force -AllowClobber -Scope CurrentUser
  • Restart PS terminal and run this command again. This time it worked.

BINGO! It’s published for anyone to use.