Fabric: a System Administrator's Best Friend
Now that you understand the groundwork of Fabric, you can start putting
it to use. For this article, I explain how to make a
simple fabfile for the purpose of installing/removing software and your
machines. First, you need what is called a fabfile. The fabfile
contains all of your Fabric functions. By default, it needs to be named
fabfile.py and be in the working directory, but as mentioned previously, you can
specify the fabfile from the command line if need be. So, open your fabfile
and start it with
from fabric.api import * to include all the Fabric
functionality. Then define all of your functions. Let's start with installing
def install(pkg=None): if pkg is not None: env["pkg"] = pkg elif pkg is None and env.get("pkg") is None: env["pkg"] = prompt("Which package? ") sudo('yum install -y %s' % env["pkg"])
You then can install a package via
yum on all of your machines by running:
$ fab --hosts=host1,host2,host3 install
Then, you'll be prompted for the package to install only once.
Alternatively, since you indicated an optional parameter of
pkg, you can
indicate that from the command line so you won't be prompted on execution,
$ fab --hosts=host1,host2,host3 install:pkg=wormux
$ fab --hosts=host1,host2,host3 install:wormux
Also note that you are prompted for the password for both SSH and sudo only once. Fabric stores this in memory and reuses it, if possible, for every other machine. Congratulations! You've just successfully created your first Fabric script. It's as simple as that!
Tips and Tricks
I've picked up some neat tricks since I've started with Fabric. First, you generally never see a Fabric command as simple as what is above. When fully automated, it looks more like this:
$ fab --skip-bad-hosts -u user -p 12345 -i ~/.ssh/id_dsa --warn-only ↪--hosts=host1,host2,host3,host4,host5,host6,host7,host8,host9,host10 ↪--parallel --pool-size=20 install:pkg=wormux
Who wants to type that out every time they want to run a command? No one! That's why aliasing almost all of that is so convenient and efficient. Add the following to your .bashrc file:
alias f="fab --skip-bad-hosts -u user -p 12345 -i ~/.ssh/id_dsa ↪--warn-only ↪--hosts=host1,host2,host3,host4,host5,host6,host7,host8,host9,host10 ↪--parallel"
Then, all you have to do each time you want to run Fabric is this:
$ f install:pkg=wormux
Even using this technique, your alias can become cumbersome if you have more than a few machines you commonly administer. A simple solution to that is to add this function to your fabfile:
def set_hosts(): env.hosts = open('hosts', 'r').readlines()
Then, put all your hostnames in a file called hosts in the same directory as your fabfile, and modify your alias to look like this:
alias f="fab --skip-bad-hosts -u user -p 12345 -i ~/.ssh/id_dsa ↪--warn-only --parallel set_hosts"
This is particularly convenient if you have a variety of fabfiles that you use on different groups of machines, or in different contexts.
There are occasions when you need to execute certain commands from
within a specific directory. Because each command is a discrete and
non-persistent connection to the machine, this is not inherently simple.
However, simply by enclosing the necessary commands in a
with statement, you
have a solution:
with cd("~/gitrepo"): run('git add --all') run('git commit -m "My super awesome automated ↪commit script for `date`"')
There are several ways to get help with Fabric. The most effective is to use the fab-file mailing list. The developers are generally very prompt in responding. There is also a Fabric Twitter account @pyfabric where Fabric news and announcements are released. You can submit and view bugs through the Fabric Github page. Of course, you also can't discount the #fabric channel on Freenode, where you can connect with the community and get some quick answers. Finally, you always can browse the documentation hosted at http://www.fabfile.org.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Server Hardening
- May 2016 Issue of Linux Journal
- EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager
- The Humble Hacker?
- BitTorrent Inc.'s Sync
- The Death of RoboVM
- The US Government and Open-Source Software
- New Container Image Standard Promises More Portable Apps
- Open-Source Project Secretly Funded by CIA
- ACI Worldwide's UP Retail Payments
In modern computer systems, privacy and security are mandatory. However, connections from the outside over public networks automatically imply risks. One easily available solution to avoid eavesdroppers’ attempts is SSH. But, its wide adoption during the past 21 years has made it a target for attackers, so hardening your system properly is a must.
Additionally, in highly regulated markets, you must comply with specific operational requirements, proving that you conform to standards and even that you have included new mandatory authentication methods, such as two-factor authentication. In this ebook, I discuss SSH and how to configure and manage it to guarantee that your network is safe, your data is secure and that you comply with relevant regulations.Get the Guide