How to Create and Use Templates for Dynamic Server Configuration
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:
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:
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.
Check on your server the directory /etc/nginx/sites-available/. The file example.com.conf was created with the following content:
This is exactly our Template. The placeholder variables were replaced by the server environment variables during execution.