Tutorials 3 min read

How to Modify Files on your Server in an Automated Way

Editing (config) files on servers during setup or later operation is one of the standard tasks of every server administrator. Learn how to automate this with Pipetower.
By Luna Jenkins • September 26, 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.

Introduction

Sysadmins often need to tweak files on servers, whether it's for general configuration, updates, security patches, or performance improvements. But when you often install a new server or you're managing multiple servers, manually changing files becomes a time-consuming chore. It's prone to errors and can lead to inconsistencies.

That's why automating file modifications is crucial for sysadmins. In this tutorial, we'll explain Pipetower’s file modification features, so you can get started right away.

General Action explanations

To perform a file change in Pipetower, you can create an Action and change the type of an individual Action step to Modify file. The Action step then looks like this:

Modify file Action type

Now you can define which file should be changed and how.

  • File: full path to the file that you want to modify
  • Sudo: wether to execute the modification as sudo or not
  • Find: the text you want to search for in the file
  • Replace: the text that should replace what was found

The search can also be refined with various options:

  • Cc: search with case-sensitive
  • W: search for "whole words only"
  • .*: search and replace with regular expressions

The following examples will demonstrate each option.

Simple search and replace

The most basic search and replace operation is to not activate any of the options. It searches for the text in FIND (regardless of upper and lower case) and replaces it with the text from REPLACE.

Simple search and replace

This example modifies the file _"/etc/ssh/sshdconfig" as sudo and replaces all occurrences of "PermitRootLogin no" with "PermitRootLogin yes".

If you want to perform a case-sensitive search, you can simply turn this option on. Just click on the Cc button to activate it.

Search for whole words

If you want to perform a search for "whole words only", you can simply turn this option on. Just click on the W button to activate it.

Assume you have a file with the following content:

; Automatically adjust config values.
auto_config = on

; System output character encoding.
output_encoding = "UTF-8"

; Default balancing strategy.
default_balancing = auto

Now you want to the change the value of default_balancing from auto to none.

Performing a simple search/replace for the word auto would lead to changing the key auto_config to none_config too.

With the option to search for whole words only, the key auto_config would be ignored.
Only the value auto would be modified to none.

Regular expressions

The most powerful and advanced option is to use regular expressions for searching and replacing. With turning on this option, you can literally change anything in your server (config) files.

Imagine you have the following config file:

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Now you want to enable all gzip options at once.

This can be achieved fairly easily using the following regular expression modification:

Regex search and replace

After Pipetower changed the file, you will have the following result:

gzip on;

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Conclusion

Pipetower offers an easy-to-use but very powerful feature to modify server files in an automated way. No matter if just a basic search and replace action is needed or an advanced regex modification, with Pipetower you can configure your desired file changes and keep all actions in one central and organized system.

This automation allows you to make the same file changes over and over again on every new server or across multiple servers without manual editing.

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