Tutorials 1 min read

How to Create and Use Templates for Dynamic Server Configuration

Templates allow you to automatically create or overwrite files on your servers and dynamically fill them with content, saving you work and time when setting up servers.
By Luna Jenkins • September 28, 2023
Preview image
New to Pipetower? It’s a tool that helps admins, devs and ops engineers automate away their repetitive server tasks — without any setup. Sign up for free.

Templates are frequently used for configuring services based on variable values that can be set up on the server, team or user environment or in the Action itself. This enables you to create adaptable setups that adjust their behavior based on dynamic information.

Let's go through the following example to demonstrate the functionality.
As a template we will use a nginx config for a server block.

First, create a new Template in Pipetower with the following content:

server {
    listen 80 default_server;
    server_name $DOMAIN;

    root /var/www/$FOLDER/htdocs;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

Save it with the file name $DOMAIN.conf.

This Template uses in total 2 variables that we have to fill later with values during the run:

  • $DOMAIN
  • $FOLDER

Now, create a new Action and set the type of the first Action step to Copy template.

Select the Template you want to copy to the server and specify the destination path.
You will have an Action similar to this example:

Copy template action step

Save your Action.

Since the Template uses 2 variables, we have to define the appropriate values. For this we go to the Server and then to the tab Environment variables.

Click on the button Change and add 2 rows and fill them with the variable name and the value, like in the following example:

Server environment variables

With this in place, we have set up everything we need to copy our dynamically filled template to the server.

Go to the created Action and click the Play button. Select the server on which you want to execute the Action (should be the same server on which you defined the environment variables) and start the run.

The Action was executed successfully.

Log output

Check on your server the directory /etc/nginx/sites-available/. The file example.com.conf was created with the following content:

File content terminal

This is exactly our Template. The placeholder variables were replaced by the server environment variables during execution.

Automate away your repetitive server work and get your time back, with Pipetower.