How to Automate Initial Setup of One or Multiple Servers with Pipetower
Introduction
In today's world of server management and infrastructure, saving time and reducing errors is crucial. Setting up one or multiple servers manually can be a real hassle. That's where Pipetower comes in. It's a tool that makes automated server setup and configuration a breeze.
In this article, we'll take a practical look at automating the initial setup of your servers with Pipetower. Say goodbye to tedious manual configurations and say hello to an easier and more reliable server setup with Pipetower. Let's get started.
Overview of the Automated Tasks
Set up your Workflow once, and use it for every server after. This Pipetower Workflow consists of the following Actions:
- Disable SSH root login
- Update all system packages to the latest version
- Install several new packages
- Setup and configure the UFW firewall
Of course, you can create Actions according to your needs and as many as you want. Through a Workflow, all Actions are bundled together and executed one after the other. You can run your Workflow on one or multiple servers in parallel.
The following examples are targeted for Ubuntu. Simply customize the commands to suit your operating system.
Let's go through the whole process step by step.
1. Add an Action to disable SSH root login
First, we want to disable the ability to log in as the root user via SSH on the server. This is a good safety precaution in many cases. Make sure that you created another user before disabling root access!
Go to Actions and click on Create action.
Give your Action a name.
Choose as Type the value Modify file.
Set up the first step as shown in the following screenshot.
This Action step will change the SSH configuration file "/etc/ssh/sshd_config". It will change "PermitRootLogin" from yes to no. Make sure to switch the Sudo toggle on.
Click on the plus icon to add a second step to the Action.
Set up the second step as shown in the following screenshot.
This Action step will execute the specified command to restart the ssh service.
2. Add an Action to update all Packages
The next task on a fresh provisioned server is to update all installed packages to the latest version.
Create a new Action and set it up as shown in the following screenshot:
The advantage of splitting these 2 commands into 2 separate steps is that the log output of the execution is also divided into 2 sections, which makes the output a bit clearer, especially in the event of an error.
Handling the Do you want to continue? question:
Many console commands (like apt-get upgrade) ask for input and wait to continue until the user has made this input. Since the Action is supposed to be automated, we can tell Pipetower what questions we expect and what we want to send in this case.
We could setup these expect/send rules directly at each Action step (Expect/Send toggle).
But we can also define them globally. This is especially useful for questions that could be asked from different commands (like "Do you want to continue?"). These global rules are then automatically applied every time an Action is executed.
Go to Actions and click the button Expect.
Click on Edit and define your rule with the following values:
EXPECT
Do you want to continue\? \[Y\/n\]
SEND
Y
With this rule in place, whenever a command writes something to the terminal that matches the regular expression "Do you want to continue\? [Y\/n]", Pipetower responds with "Y".
Especially during the installation of packages, additional questions can often be asked that need to be responded to. For our example, the following Expect/Send rules can be defined:
3. Add an Action to install new Packages
In addition to updating all current packages, we want to install some new packages on the server.
Again, go to Actions and click on Create action and give your Action a name.
Choose as Type Execute command and insert your installation command:
sudo apt install \
snapd \
curl \
vim \
git
You can add or remove packages to your liking to meet your needs.
4. Add an Action to setup a Firewall
A firewall is essential for any server connected the internet. You can have Pipetower ensure UFW (Uncomplicated Firewall) is installed and configured.
Create a new Action with 3 steps. Set for each step the Type to Execute Command.
The first step installs the ufw package:
sudo apt install ufw
The second step configures the firewall rules:
sudo ufw default deny incoming;
sudo ufw default allow outgoing;
sudo ufw allow ssh;
sudo ufw allow http;
sudo ufw allow https;
The third step enables and starts ufw:
sudo ufw enable
When this command is executed on the server, it will prompt for a confirmation. Make sure to add a Expect/Send Rule for this Action step. Send back a "y" when it asks for the following:
Proceed with operation \(y\|n\)\?
In the end the complete Action should look something like this:
5. Add a Workflow to put everything together
The last step before running your Actions is to put them together in a Workflow. A Workflow in Pipetower bundles the Actions together and executes them one after the other. It's also possible to add conditions to the Workflow in order to skip or to run each Action.
Go to Workflows and click on Create workflow.
In the first Workflow step, select the Action to disable the SSH root access.
Add a second step by clicking on the plus icon. Select the Action to update all packages.
Add the following steps and select the Actions that we created.
The Workflow should look like this:
Save your Workflow. It's ready for execution.
6. Run your complete Workflow
You’re now ready to run this Workflow on one or more servers.
Go to the Workflow and click the play button to run it.
A modal will open where you have to select the servers on which the Workflow should be executed. You can select each server individually, or by tags.
With tags such as "prod", "dev", "beta", "west-1", "west-2" etc., you can group specific servers and execute Actions or Workflows on an entire group of servers.
After clicking on Run workflow, the actual execution is started on both servers in parallel and you will see an overview similar to the following:
Just click on one of the server executions to see a realtime log output of the run.
During execution and at any time afterwards, you can view the log and see what was executed on which server.
Conclusion
Automating the initial server setup can help you save time and ensure that all your servers are set up in the same way. This is important because it makes it easier to manage and customize them as needed. In today's world, where applications are spread out and you need consistency across different staging environments, automation is a must.
In this tutorial, we demonstrated how to use Pipetower to automate tasks you should do when you set up a new server. These tasks include disabling SSH access for the root user, updating all installed packages, installing some new packages and installing and configuring the UFW firewall.
Don't wait, start to automate ;)