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.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- Stunnel Security for Oracle
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- SourceClear Open
- SUSE LLC's SUSE Manager
- Managing Linux Using Puppet
- My +1 Sword of Productivity
- Google's SwiftShader Released
- Non-Linux FOSS: Caffeine!
- Parsing an RSS News Feed with a Bash Script
- SuperTuxKart 0.9.2 Released
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