Saketh
ServiceNow Employee
ServiceNow Employee

Step - 1: Cloud Script Template

*In App Nav, navigate to Cloud Config Management > Cloud Script Templates

*Click New and enter a name. For example, ApacheTemplate

*In content section, enter the script. For example,

#!/bin/sh

sudo apt-get -y update

sudo apt-get -y install apache2

-> The above script will install apache web server

*Click submit

This completes step - 1.

Step -2: Cloud Script

*In App Nav, navigate to Cloud Config Management > Cloud Script

*Click New and enter a name. For example, ApacheScript.

*In content section, enter:

function evaluateTemplate() {

return templateResolver.getTemplate('<Cloud Script Template name>', templateAttributes);

}

evaluateTemplate();

Eg:

function evaluateTemplate() {

return templateResolver.getTemplate('ApacheTemplate', templateAttributes);

}

evaluateTemplate();

* Click Submit

Notes for Step - 2: There is a script include called TemplateResolver. An instance of this object is instantiated and you have access to this in your script. The method getTemplate simply substitutes given attributes into the template and returns the content.

Step - 3: Using the Cloud Script in a blueprint.

*Create a simple blueprint for provisioning a VM on Azure or AWS Datacenter.

*In Operations -> Steps, Click the + button to add a step.

*For Resource, enter Virtual Server.

*For Operation, pick Block Until Ready.

*Click Submit.

*Click the + again.

*For Resource, enter Virtual Server again.

*For Operation, pick ExecuteScript.

*Click Submit.

*There are some attributes to be updated. In Virtual Server.Provision, change DiscoveryAndAttachIP to true. The reason for this is we need a public IP address. If the MID server is not in the same subnet as the deployed VM and we will need to access the server to run the script.

*Click Save

*In Virtual Server.ExecuteScript, we will set some values here rather than have the user enter them. Uncheck Show in form for both Script and ScriptType.

*For ScriptType, enter Shell.

*For Script, enter ${CloudScript.scripts.<Cloud Script Name>}. For example, ${CloudScript.scripts.ApacheScript}. The expression evaluates to running the script designated by the part after CloudScript.scripts — in this case, ApacheScript.

*Click Save.

Ensure that the steps are in right order:

1. Blueprint Container Resource.Provision

2. Virtual Server.Provision

3. Virtual Server.Block Until Ready

4. Virtual Server.ExecuteScript

* Check-in the blueprint.

*Publish to Catlog

*Active and Update the catalog item.

This completes the end to end cycle to run scripts on provision.

Try deploying the catalog item and verify if the script ran successfully.

Comments
Adrian_
Kilo Contributor

Hi, Saketh!

 

I'm also trying this but encountered this error:

 

find_real_file.png

 

Any clue on this translator script error is coming from? This happens on ExecuteScript. 

Version history
Last update:
‎02-06-2018 09:28 AM
Updated by: