Deploy postfix configuration to multiple servers with Fabric

If you are running more than one postfix server, administration overhead becomes an issue. Every time you want to update the main.cf file you have to manually connect to each server and run the postconf command.

As with every manual task, this leads to errors and inconsistencies in the configuration files.
One way of dealing with this problem is using an automated deployment tool such as Fabric.
Fabric is a python library that enables you to deploy files, run commands and administer multiple servers over SSH.
Here is a Fabric script that will use the postconf command and restart the Postfix service on each server.

Installation

In order to install Fabric you need to have a local python installation and have the setuptools or pip utilities.
Install using pip:

pip install fabric

Using easy_install (setuptools):

easy_install fabric

Easy.

The Fab file

Create a file named “fabfile.py”.

from fabric.api import env ,sudo
env.hosts = ['user@host1','user@host2','user@host3']

def postconf(command):
        sudo('postconf -e "'+command+'"')
        sudo('service postfix restart')

Deploy the postfix configuration to multiple servers

Now we can run the postconf command on all of our servers:

fab postconf:"message_size_limit \= 20480000" 

Don’t forget to escape the “=” sign.

If you are working with passwords for ssh connections, you will be prompted for each server (Seriously, you are using ssh keys, right?).
And thats it.

Now you may want to send multiple commands at once. We can dump all of those in a file and read from it.
Lets create a file named commands.txt

message_size_limit = 20480000
mailbox_size_limit = 0
default_destination_concurrency_limit = 1

Our updated script will look like this:

from fabric.api import run, env, put, sudo
env.hosts = ['user@host1','user@host2','user@host3']

def postconf(filename):
        f = open(filename)

        for command in f:
                sudo('postconf -e "'+command.strip()+'"')
        sudo('service postfix restart')
        f.close()

And to run the commands:

fab postconf commands.txt

From what you can see, the library is very simple to use and modify if you are familiar with python.

If you need any help with setting this up or modify it to your own needs just leave me a message.

, , ,

Join my Online Postfix Class
Join my Online Postfix Class
I am working on a new Postfix training class that will be launching soon. The course includes:
  • Understanding the Logs files
  • Troubleshooting
  • Performance
  • Rate limiting
  • DKIM/DMARC/SPF
  • And much More

Powered by WordPress. Designed by Woo Themes