Custom Scripts are scripts that use the
cloud-config format, designed to be run by the cloud-init process. These scripts are generally used for the initial configuration of a server and run on the first boot.
Put simply, deploying a server with a custom script allows you to run arbitrary commands and change several aspects of the server during provisioning.
Here are a few examples of what you can do with a very simple custom script:
- Define machine hostname
- Add SSH keys
- Change user passwords
If you're using a UNIX-like O.S. such as Linux, your scripts must be written using the YAML file format, which uses whitespace and new-lines to delimit lists, associative arrays, and values.
These files are generally fairly intuitive and easy to understand, but there are a few things you need to keep in mind when using them. Here is a good getting started guide from the guys at Ansible that can help.
For Windows images, you'll have to use the cloud-baseinit convention. This is just a fancy way of saying that your custom scripts will have to be written as if you were using the Powershell. Here's a straightforward example:
mkdir c:/test New-Item c:/test/test.txt Set-Content c:/test/test.txt 'Hello World'
Adding a custom script
- When creating a new server, click on New custom script. (Or go to Servers > Custom Scripts)
- The name of your script needs to be preceded by a number and a dash. We use this to define the order of the scripts that you select. For instance, a script named
01-Scriptwill run before
02-Scriptbecause 01 takes priority over 02.
- Click on add and proceed with deploying your server.
You can find great examples on cloud-init's docs here.