Thank you for your feedback.
Form temporarily unavailable. Please try again or contact docfeedback@servicenow.com to submit your comments.
Versions
  • Madrid
  • London
  • Kingston
  • Jakarta
  • Istanbul
  • Helsinki
  • Geneva
  • Store
Close

Cloud scripts and cloud script templates

Log in to subscribe to topics and get notified when content changes.

Cloud scripts and cloud script templates

In the Cloud Management application, script execution is divided into cloud scripts and cloud script templates.

Cloud scripts

Cloud scripts are simple java scripts that use ServiceNow's platform features. An example of a sample cloud script:
function evaluateTemplate() {
	// Template Resolver is a helper function which we will use to fetch a template and replace its parameters,
	// var templateResolver=new TemplateResolver(); this comes inbuild
	// templateAttributes below will passed from the user.
	var listDir=templateResolver.getTemplate('Install',templateAttributes);
	return listDir;
}

Each cloud script contains certain parameters. Parameters are the attributes that you want to access. A parameter contains a name, a default value, and an override value. The default value can be a string literal, a resource block parameter or a complex script expression used in resource blocks.

A cloud script, by default, has access to the templateResolver object. The templateResolver object has, by default, a getTemplate method, whose first parameter is the cloud script template name. templateAttributes are the cloud script parameters created as part of a cloud script. If needed, customizations can be done in templateAttributes. As an example, see the following code snippet if the value of DevName parameter needs to be changed from John to Emily.

function evaluateTemplate() {
	// Template Resolver is a helper function which we will use to fetch a template and replace its parameters,
	// var templateResolver=new TemplateResolver(); this comes inbuild
	// templateAttributes below will passed from the user.
	templateAttributes['DevName'] = 'Emily';
	var customNodeName;
	var gr = new GlideRecord('some_custom_table_to_query');
	gr.addQuery('some_parameter','some_value');
	gr.query();
	if(gr.next())
	 	customNodeName=gr.getValue('some_custom_node_name');
		
	templateAttributes['NodeName'] = customNodeName;
	var listDir=templateResolver.getTemplate('Install',templateAttributes);
	return listDir;
}
evaluateTemplate();

Cloud script templates

Cloud script templates are actual executables which are passed to target a virtual machine for execution. Cloud templates can be of any type (Shell/PowerShell/CloudInit) depending on the execution context. You need to create a cloud template first and then associate it with a cloud script.

Cloud scripts as CloudInit

Cloud providers provide a way to execute scripts at boottime for virtual machines. In the Cloud Management application, you can speicfy a cloudinit script at various levels:
  • Image level: At the time of creating a cloud script, you can specify an image against which this script should be executed. Once an OS profile which contains this specific image gets selected, the default cloud script against this image is executed. In the example below, an Apache cloud script is associated with a particular image.
    Apache cloud script associated with an image.
  • OS profile level: You can run a cloudinit against a specific cloud provider and against a specific AMI/image. You can also be generic and associate a cloudinit to an OS profile, irrespective of the underlying image. In the example below, multiple images are associated with the OS profile. You can map any cloud script to the OS profile in the Cloud Script OS Profile Mappings tab. The mapping of a cloud script with an OS profile takes precedence over the image level cloud script association.
    cloud script default at OS profile
    In this example, the Apache cloud script is mapped to the Centos OS profile. If needed, you can override the script parameters in the OS Profile Mapping Overrides section.
  • Resource blocks: While mapping a cloud script to an OS profile, you can choose if you want to execute the cloud script for a specific resource block used in a blueprint and against a particular resource alias. See Create an OS profile.

Cloud scripts as Postinit

You can use cloud scripts as Postinit scripts in a resource block. Once you create a resource block with a virtual machine to be provisioned, you can add an ExecuteScript operation. The ExecuteScript operation takes a script parameter (a pool of cloud scripts) and you can select any script to execute. Use the ScriptParameters attribute in the ExecuteScript operation to override any script parameter. You can provide a JSON map of script parameter and its override value. All expressions that are supported by Cloud Management work in the ScriptParamters attribute.

Create cloud script templates and scripts

Create cloud script templates and associate a cloud script to the template to use during the provisioning of virtual machines.

Before you begin

Role required: cloud_admin

Procedure

  1. Navigate to Cloud Management > Cloud Admin Portal > Design > Cloud Scripts > Cloud Script Templates.
  2. Click New.
  3. Enter the name and content of the script.
  4. Click Submit.
  5. Navigate to Cloud Management > Cloud Admin Portal > Design > Cloud Scripts.
  6. Click New.
  7. Enter a name and create a script that uses the template name in the following line of code:
    templateResolver.getTemplate(cloudscripttemplateName);

    Where cloudscripttemplateName is the name of the template.

  8. Click Submit.

What to do next

To run a script at the time a virtual resource is provisioned, map the script to an OS profile. See Map scripts to an OS profile. To run a post-provisioning script, create a blueprint and specify the script in the Execute Script operation. See Example: A post-provisioning cloud script.

Map scripts to an OS profile

You can map a script to an OS profile if you want to execute scripted actions during the provisioning of virtual machines. The script runs on virtual machines that are created based on the image template in the OS profile.

Before you begin

  • Role required: cloud_admin
  • A cloud script that you want to run.

Procedure

  1. Navigate to Cloud Management > Cloud Admin Portal > Resource Profiles.
  2. From the Profiles list, select OS Profile.
  3. Open the OS profile that you want to map to a cloud script.
  4. On the Cloud Script OS Mappings related list, click New.
  5. Fill out the form fields (see table).
    Field Description
    Blueprint

    [Optional]

    Select a blueprint to execute the cloud script against. If a blueprint is selected, the cloud script gets executed when the blueprint is provisioned.
    Cloud script Select the cloud script that you want to map to this OS profile.
    Active Select this check box if you want the cloud script to be activated once the virtual machine is provisioned.
    OS profile The OS profile record that this script mapping is associated with.
    Resource Alias

    [Optional]

    Select a resource from the blueprint that you selected. If a resource alias for the blueprint is specified, then the cloud script gets executed when the blueprint with the specified resource alias is provisioned.
    Note: Profile mappings that specify more details are run first. For example, a mapping that specifies a blueprint, OS profile, and resource alias takes precedence over one that just specifies an OS profile.
  6. Right-click the form header and select Save.
  7. Click Update.

Example: A post-provisioning cloud script

This example shows you how to create a cloud script that is run after a user provisions a virtual machine.

Before you begin

  • Role required: cloud_admin
  • An OS resource profile that is mapped to an image template from which you provision a resource. You must add the necessary basic authentication credentials to the image.
  • A Compute resource profile that is mapped to a Hardware type.

About this task

In this example, you will create a cloud init script, customize a resource block, and provision a VM from the User Portal.

Procedure

  1. Create a cloud script template and a cloud script that refers to the template.
  2. Open the virtual server resource block, and then do the following:
    1. Set the Draft/Published switch to Draft.

      Virtual Server

    2. Click the Operations tab, and then select Virtual Server Interface from the Interface field.

      Virtual Server Interface

    3. In the Operation field, verify that these operations already exist:
      • Block Until Ready
      • Execute Script: This operation tells the blueprint to run a script.

      Block Until Ready

      Execute Script operation

    4. Click the Input Parameters tab to see the inputs for each operation.
    5. Click the Steps tab.
    6. Add steps as needed.
    7. For the Script parameter on each step, clear the Script check box and change the expression in the Mapping column to the following text: $(CloudScript.scripts.scriptname), where scriptname is the name of the script, not the script template, that you created earlier.

      Script parameter

  3. Publish the virtual server resource block, and use it in a blueprint that in turn is published to a catalog item.
  4. Launch a new stack to provision a VM, and specify the OS profile and Compute profile you had created as a prerequisite to this example task.

Result

The Stack Status screen runs through the CreateNode, BlockUntilReady, and ExecuteShellScript operations. The last operation is what kicked off the script you created above.
Feedback