Writing Scripts for Use with Linode StackScripts

Updated by Linode Contributed by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

What are StackScripts?

StackScripts provide Linode users with the ability to automate the deployment of custom systems on top of Linode’s default Linux distribution images. For example, every time you deploy a new Linode you might execute the same tasks, like updating your system’s software, installing your favorite Linux tools, and adding a limited user account. These tasks can be automated using a StackScript that will perform these actions for you as part of your Linode’s first boot process.

All StackScripts are stored in the Linode Cloud Manager and can be accessed whenever you deploy a Linode. A StackScript authored by you is an Account StackScript. While a Community StackScript is a StackScript created by a Linode community member that has made their StackScript publicly available in the Linode Cloud Manager.

In this Guide

Writing a script for use in a StackScript will generally be the same as writing a script that will be executed from the command line or another program. This guide includes information about the StackScript system, including the following:

The StackScript System

StackScript Requirements

  • The primary requirement for your scripts is that the interpreter needed to execute your script should exist in the Linode base image you are deploying. While Bash is an obvious choice for a script, you may choose any scripting language.

    Note
    Linode images are created using “vanilla” versions of its given distribution. Consult our Choosing a Linux Distribution guide to see list of all distributions Linode provides and to access each distribution’s corresponding websites. You can find more information on the interpreters available for each distribution on their official websites.
  • When writing a script, you must use a shebang as the first line of your script. This indicates to your Linux system which interpreter to use when running the script. For example, if you are writing a Bash script, the beginning of your script should include the following line:

    1
    2
    
    #!/bin/bash
        

    Or, if you are writing a Python script, the beginning of your script should include the following line:

    1
    2
    
    #!/usr/bin/env python
          

Import a StackScript

Your scripts can import any Account StackScript that you own or any Community StackScript. This allows you to reuse code minimizing what you need to write in your own scripts.

  • The example below shows the syntax to import another StackScript. As a result of including this line in a script, the imported StackScript will be downloaded as ssinclude-[NUMBER] to your Linode. However, it must be run in order to execute its contents.

    1
    2
    
    <ssinclude StackScriptID="[NUMBER]">
        

    In Bash, you can download and run the script in the following way:

    1
    2
    
    source <ssinclude StackScriptID="[NUMBER]">
        

    If you’re scripting in another language, import the StackScript, then execute it on a second line:

    1
    2
    3
    
    <ssinclude StackScriptID="[NUMBER]">
    ./ssinclude-[NUMBER]
        
  • Linode provides a StackScript Bash Library that includes a set of functions that perform various common tasks users might wish to execute on their Linodes. This script creates the functions, but does not run them. A new StackScript can import the Bash Library and then execute functions from it.

    Note
    Linode’s StackScript Bash Library’s ID number is 1.

Access a StackScript’s ID Number

Follow the steps in this section to find the ID number of a StackScript.

  1. Log into the Linode Cloud Manager.

  2. Click on the StackScripts link in the left-hand navigation menu. You will be brought to the StackScripts page.

    Click on the StackScripts link in the left-hand navigation menu.

  3. Click on the Account StackScripts tab or the Community StackScripts tab, depending on the type of StackScript whose ID you’d like to find

  4. Click on the StackScript whose ID you’d like to access. This will bring you to its StackScript detail page.

    View the details and contents of an Account StackScript.

  5. The StackScript detail page’s URL will display the StackScript’s ID number. You can now use this number to import the StackScript into your own script.

    Access a StackScript's ID number.

User Defined Fields (UDFs)

The StackScript system provides a basic markup specification that interfaces with the Linode deployment process so that users can customize the behavior of a StackScript on a per-deployment basis. When a StackScript contains a user defined field (UDF), the Linode Cloud Manager will present the UDF as a form field, so a user can insert a corresponding custom value. The values and their related variables are inserted into the script’s environment when used to deploy a new Linode.

  • Use the following format to insert a UDF tag into a StackScript.

    Note
    The UDF tags are commented out to prevent execution errors, as the StackScript system parses the tags without removing them.
    1
    2
    
    # <UDF name="example-var" label="An example informative label for the user." default="A default value" example="An example value." />
        
  • A UDF tag accepts the following attributes:

    Label Description Data Type
    name The variable name to use within the StackScript.
    Note
    If you would like to create a masked password input field, use the word password anywhere in the UDF name attribute.
    required.
    String. Alphanumeric and underscore, length must be less than 64 characters, and the name must be unique within the StackScript.
    label The form field’s label to present to a user in the Linode Cloud Manager. required. String.
    default The UDF’s default value. If no value is specified by the user, the default value will be used when deploying a new Linode with the StackScript. String.
    example An example input value to present to a user in the Linode Cloud Manager. String.
    oneof A comma separated list of acceptable single values for the field. When this attribute is provided, a dropdown menu will be presented to a user with a list of values to choose from within the Linode Cloud Manager. Only one value can be selected by the user. If your StackScript uses the oneof attribute, you cannot use the manyof attribute. Comma separated list of strings.
    manyof A comma separated list of acceptable values for the field in any quantity, combination, or order. When this attribute is used, a dropdown menu will be presented to a user with a list of acceptable values they can choose from with the Linode Cloud Manager. Multiple values can be selected by the user. If your StackScript uses the manyof attribute, you cannot use the oneof attribute. Comma separated list of strings.

Default Environment Variables

Linode StackScripts provide a set of default environment variables that you can use to provide your script with information about the Linode it has deployed.

Environment Variable Description
LINODE_ID The deployed Linode’s ID number
LINODE_LISHUSERNAME The deployed Linode’s full Linode Shell (LISH) accessible name.
LINODE_RAM The RAM available on this Linode’s plan.
LINODE_DATACENTERID The ID number of the data center containing the Linode. You can use the Linode API to see a list of all data center IDs.
Set your Environment Variables Using an External File

It is possible to set your script’s environment variables using externally hosted files. The example Bash script uses the wget utility to download two files named base.env and $IPADDR.env from the external site http://example.com/. The source command will load the downloaded files into the script.

StackScript
1
2
3
4
5
6
7
8
9
# [...]
IPADDR=$(/sbin/ifconfig eth0 | awk '/inet / { print $2 }' | sed 's/addr://')

wget http://example.com/base.env --output-document=/tmp/base.env
wget http://example.com/$IPADDR.env --output-document=/tmp/system.env

source /tmp/base.env
source /tmp/system.env
# [...]
Note
The files you reference within your script must exist and be accessible via HTTP. Also, ensure that the files you host externally do not contain any sensitive information.

StackScript Examples

Using an External Script

  • If you have an existing deployment script, you can use a StackScript to deploy Linode instances with it. The following example StackScript installs PHP on the Linode, downloads an external PHP script from the URL http://example.com/deployment-script.php, makes it executable, and then runs the downloaded script.

    StackScript
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    #!/bin/bash
    if [ -f /etc/apt/sources.list ]; then
       apt-get upgrade
       apt-get -y install php
    elif [-f /etc/yum.conf ]; then
       yum -y install php
    elif [-f /etc/pacman.conf ]; then
       pacman -Sy
       pacman -S --noconfirm pacman
       pacman -S --noconfirm php
    else
       echo "Your distribution is not supported by this StackScript"
       exit
    fi
    
    wget http://example.com/deployment-script.php --output-document=/opt/deployment-script.php
    chmod +x /opt/deployment-script.php
    
    ./opt/deployment-script.php
        
  • If you do not want to rely on an existing external server to host your scripts for download, you can embed the bootstrapped script into the StackScript.

    StackScript
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    #!/bin/bash
    
    if [ -f /etc/apt/sources.list ]; then
       apt-get upgrade
       apt-get -y install php5
    elif [-f /etc/yum.conf ]; then
       yum -y install php
    elif [-f /etc/pacman.conf ]; then
       pacman -Sy
       pacman -S --noconfirm pacman
       pacman -S --noconfirm php
    else
       echo "Your distribution is not supported by this StackScript"
       exit
    fi
    
    cat >/opt/deployment-script.php <<EOF
    #!/usr/bin/php
    <?php print('Hello World!'); ?>
    EOF
    
    chmod +x /opt/deployment-script.php
    
    ./opt/deployment-script.php
    
        

Next Steps

Join our Community

Find answers, ask questions, and help others.

This guide is published under a CC BY-ND 4.0 license.