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.
- Android Browser Security--What You Haven't Been Told
- Epiq Solutions' Sidekiq M.2
- Readers' Choice Awards 2013
- The Many Paths to a Solution
- Nativ Disc
- Synopsys' Coverity
- Securing the Programmer
- RPi-Powered pi-topCEED Makes the Case as a Low-Cost Modular Learning Desktop
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Identity: Our Last Stand
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide