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.
Webinar: 8 Signs You’re Beyond Cron
On Demand NOW
Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.View Now!
|My Humble Little Game Collection||May 28, 2015|
|New Linux Based OS Brings Internet of Things Closer to Reality||May 27, 2015|
|Non-Linux FOSS: All the Bitcoin, None of the Bloat||May 26, 2015|
|Dr Hjkl on the Command Line||May 21, 2015|
|Initializing and Managing Services in Linux: Past, Present and Future||May 20, 2015|
|Goodbye, Pi. Hello, C.H.I.P.||May 18, 2015|
- My Humble Little Game Collection
- New Linux Based OS Brings Internet of Things Closer to Reality
- Initializing and Managing Services in Linux: Past, Present and Future
- Dr Hjkl on the Command Line
- Using Hiera with Puppet
- Non-Linux FOSS: All the Bitcoin, None of the Bloat
- Infinite BusyBox with systemd
- Gartner Dubs DivvyCloud Cool Cloud Management Vendor
- Goodbye, Pi. Hello, C.H.I.P.
- It's Easier to Ask Forgiveness...