VMware Cloud Community
sutter
Enthusiast
Enthusiast

Script issue with changing hostname

All,

Having an odd problem and I'm not good enough with JavaScript to figure out what I'm doing wrong.  Any help would be appreciated.

I've add Kali Linux to my vRA build but sadly VMware does not have a customization file setting that will work with the newest Kali Linux build.  Not a big deal I just need to be able to change hostnames, I figured an easy way to do this would be to make a vRO workflow to change the hostname to whatever the user named their VM.  I've written it all up and added all the proper attributes in vRO and it is giving me a coding error.  I'm assuming what it is saying is miss labeled so hopefully someone can see what I've done wrong.  Here is the code:

System.log("Workflow started from workflow stub " + externalWFStub + " on vCAC host " + vCACHost.displayName);

System.log("Got vCAC virtual machine " + vCACVm.virtualMachineName);

if (vCenterVm != null) {

  System.log("Got vCenter VM " + vCenterVm.name);

}

//Displaying vCAC VM properties

if (vCACVmProperties != null) {

  var log = "";

  log += "vCAC VM properties :\n";

  var array = new Array();

  for each (var key in vCACVmProperties.keys) {

  array.push(key + " : " + vCACVmProperties.get(key));

  }

  array.sort();

  for each (var line in array) {

  log += "\t" + line + "\n";

  }

  System.log(log);

}

// Obtain build information from Blueprint

Build=vCACVmProperties.get('VirtualMachine.BuildType');

// Obtain new hostname from VM

newHostName=vCACVmProperties.get('vCenterVM.name')

// Set hostname on server through ssh

if (Build=='Kali'){

  username='root';

  password='password1';

  hostNameOrIP=vCACVmProperties.get('VirtualMachine.Network0.Address');

  cmd='sed -i -e "s/CURRENTHOSTNAME/' + newHostName '/g" /etc/hosts';

}

Simple enough, it pulls the machines information and runs an ssh command if it sees a build profile attribute that labels it as Kali.  The problem is the last lines.  The error I'm getting is "missing ; before statement (script#34) which is the line "cmd=hostname ' + newHostName ';

username, password, hostNameOrIP and cmd are all variables in the workflow and if I delete the cmd line it takes the error away.  I have no idea what it is actually looking for.  Any idea or more information needed?

0 Kudos
1 Reply
qc4vmware
Virtuoso
Virtuoso

First when you are declaring a new variable be sure to use var unless you intention is to make it globally accessible.  I'm guessing this line:

Build=vCACVmProperties.get('VirtualMachine.BuildType');


should be:


var Build=vCACVmProperties.get('VirtualMachine.BuildType');


I'm not positive how all the scoping shakes out in vRO but if you haven't read up on variable scoping in javascript I'd take a look here JavaScript Scope.


and I think this is the line you want for setting cmd:


var cmd='sed -i -e \"s/CURRENTHOSTNAME/' + newHostName + '/g\" /etc/hosts';

You need to escape the " and also forgot a plust sign between the strings and the variable.

0 Kudos