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

Cloud initialization scripts

Cloud initialization scripts

Cloud initialization scripts give you the ability to run scripts when a virtual machine is provisioned.

Cloud initialization script components

Cloud initialization scripts have the following components:
Template
The actual script that is run.
Script
A script is a container of templates. You can use multiple templates in a script. The script name can be called from a blueprint operation or from an Image template.
Parameters
Parameters are the attributes that you want to access. An attribute contains a name, a default value, and an override value.

Where cloud init scripts are used

Cloud initialization scripts are used in:
Resource blocks and blueprints
The Execute Script operations on a virtual machine in a blueprint provides a field called Script. You map the script name to this field. The script runs after the VM is provisioned. See Example: A post-provisioning cloud script for an example.
OS profiles
You can map a script to an OS profile.

Scripts on resource blocks override scripts on image templates. Therefore, if you have a script on both components, only the script specified on the resource block is run when the VM is provisioned. The script on the Image is ignored.

Create cloud initialization script templates and a script

Create cloud initialization templates and a script to use during VM provisioning.

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. Create as many script templates as needed.
  6. Navigate to Cloud Management > Cloud Admin Portal > Design > Cloud Scripts.
  7. Click New.
  8. Enter a descriptive name and create script that uses the template name in the following line of code:
    templateResolver.getTemplate(scriptName);

    Where scriptName is the name of the template.

  9. 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 for an example.

Map scripts to an OS profile

You can map a script to an OS profile if you want to execute scripted actions during VM provisioning. The script runs on VMs 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 > OS Profiles.
  2. On the Cloud Script OS Mappings related list, click New.
  3. Fill out the form fields (see table).
    Field Description
    Blueprint

    [Optional]

    Select a blueprint to limit the script to a specific resource block used in the blueprint.
    Cloud script Select that cloud script.
    Active Keep this check box selected to use the script.
    OS profile Keep the OS profile record that this script mapping is associated with.
    Resource Alias

    [Optional]

    Select the resource block that is part of the blueprint. The script runs when a cloud resource is provisioned based on this resource block.
    Note: Profile mappings that specify more details are run first. For example, a mapping that specifies a blueprint, OS profile, and resource alias take precedence over a one that just specifies an OS profile.
  4. Right-click the form header and select Save.
    The attribute scriptName appears in the OS Profile Mapping Overrides section.
  5. In the OS Profile Mapping Overrides section, enter an optional attribute name and value that you want to use when the resource is provisioned.

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

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.