Fabric, Some Python Powered System Admin Greatness

Jan 21, 2014
Linux Administration
Python Fabric

An introduction to Fabric with a short tutorial:

There are so many great open-source projects out there and sometimes you come across one and think – “How the hell did I miss this one?” Fabric is one of these for me. Now that I’ve found it, I’m seeing mentions throughout StackOverflow and other places about how great it is….and it is, absolutely great.

For those that are not familiar with Fabric, let me introduce you: Fabric is a Python library that allows system administrators to easily work on servers through SSH.  It’s described on their github page as “Simple, Pythonic remote execution and deployment.”

As web developers these days, we are also definitely system admins. Every day I find myself doing various tasks on my servers such as, setting up a new sites, reloading apache after updating Django files, tailing some log files, installing python projects through PIP, and syncing files from a development server through rsync, among other things. To do each of these I would need to SSH into the required server and execute these tasks, some of which require several commands.

Now, in the past, I just wrote bash scripts that I could execute from my home directory to really speed up the process. These would be for several step tasks like setting up a new site in Apache and creating the appropriate directories for the new site.  Fabric not only simplifies this by allowing us to avoid clunky bash scripting in favor of beautiful Python, but runs from your local dev machine, so you don’t have to connect to each server to work with it.

So, here’s a quick example on how it works:

You can install Fabric through PIP with:

sudo pip install fabric

Once installed, if you run the command fab, Fabric will look for a file named fabfile.py in the current directory, which is where you’ll create python functions using the Fabric library to do pretty much anything you would need to do over SSH.

So, create a file named fabfile.py and paste this in:

 

Modify the above to fit your connection settings.

So, what we’ve done in that code is set up the environment settings that Fabric uses when connecting. And we then created a function to add a user on the remote server. The function has a single command sudo(), which will run the string you provide using the sudo command, allowing root privileges.

Once that’s ready, save and close the file and run:

fab -l

This will list out the available functions that you can run from Fabric using this file. You should see the one function we made. To execute that function, run:

fab add_user

You’ll first be prompted for your sudo password on the server and then you’ll see that fabric responds out with all of the prompts that your remote server provides when you add a user through adduser.

So, you can now at any time quickly add a user named ‘steve’ to your remote server from a single command. If every user was named ‘steve’, we would be done here. But since you likely would want to create user with a name you choose, let’s look at fabric “prompt” command:

Add this to the imports at the top your fabfile.py

and the modify your add_user() function to look like this:


It’s pretty obvious what’s happening here, but now when you run fab add_user, you’ll be prompted for the new username by Fabric and then the name you type will be piped into the command we’re running through sudo().

So with that example, you can see how great Fabric can be for those of us that find ourselves doing repetitive tasks on remote servers. I’ll have another post soon that shows a how I work with several servers from a single fabfile.py

For more information on Fabric, check it:

Read the docs here
Fork or help out on GitHub

Leave a Comment

Your email address will not be published. Required fields are marked *