Lets get started with userdata scripts

Home / Lets get started with userdata scripts

In the past i had a simple website with some contact info. Maintaining the website and webserver became too much hassle and the site got hacked a couple of times. Restoring it involved setting up a clean server, dropping Joomla on it and restoring the backups. All done by hand, so the last time i restored it has been some years ago. Surely these days this can be done smarter right?

Choosing the automation tool

Ansible is perfect for maintaining a relatively small number of servers. It is easy to setup and modify and has gained a lot of usable modules in the past three years. I use it a lot, so getting started will be easy. For a simple lamp server like this it still seems like too much work. Keep in mind that my preferred way to perform maintenance to it will involve just throwing it away and creating a new server.
You can also deploy to most clouds like OpenStack with Ansible, however this does not feel natural in Ansible. I will go into Ansible another time, it is really a great tool. For now, its just not the right tool for the job.

Salt is a perfect tool for managing a large set of servers like the cloud itself at CloudVPS. Salt works with salt minions (agents) which give the benefit of decentralized running tasks. That makes execution on a large set of servers much faster. Salt is not quick and easy to get started with and would surely not be done in one weekend for me.

There is one more automation tool. It’s really easy to learn, and fast to get started with, BASH!
It wont scale very well, wont automate your patches etc. But is is fast and easy to setup. And most important for this article, we can just put in in a userdata script.

What are userdata scripts?

Every cloud provider like AWS, RackSpace, Digital Ocean or other OpenStack based clouds like the one where i work allow you to customize your server with a script. Different providers use different names like userdata, customization scripts or droplet metadata. They all mean the same, a script that will be executed by a service called cloud-init on the first boot.

These scripts can contain anything from bash to Powershell, depending on your needs and OS. Typically you will use it to do some basic config like

  • installing python that will be required for Ansible
  • install your Salt minions so the Salt master kan then take over
  • update your apt cache so you wont have to wait for it later on

Start your userdata script with #!/bin/bash -v on a Linux instance. The -v makes sure that all output goes to the console log as well. A simple script for Ubuntu can look like the one below. It will update the apt cache, perform a dist-upgrade (keeping any default or existing config) and install python.

#!/bin/bash -v
apt update
DEBIAN_FRONTEND=noninteractive apt -y \
-o Dpkg::Options::="--force-confdef" \
-o Dpkg::Options::="--force-confold" \
dist-upgrade
apt install -y python

When using Windows start with #PS1
If you want to include a external script, powershell, bash or other use #Include http://your-script-url

​You could also write a bash script that will install a full lamp stack, configure it, deploy WordPress and setup letsencrypt. The script only runs once, so you won’t be able to manage the configuration later on as you would with automation tools like Ansible. For this usecase it will be fine since i will just setup a new instance instead. Also i will use it to setup a demo server when deploying it with other tools.

All that’s left is to either deploy it with Terraform (super cool tool, more on that later), with the OpenStack Horizon interface or with the CLI:

openstack server create --image "$image" \
--flavor $flavor \
--security-group "$securitygroup" \
--key-name "$keyname"\
--nic net-id=net-public\
--user-data ./userdata/Ubuntu/lamp_wordpress.sh cmto_lamp

If you would like to see more userdata scripts that might come in handy, take a look at my GitHub repo. You will also find the lamp_wordpress.sh there.

Last but not least, if you want to try for free how easy userdata scripts work, try out the free tier at AWS or request a free test account at one of the OpenStack providers like CloudVPS.

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *