Home > Article > Backend Development > How to automate your tasks with Fabric
First let’s look at an example. We know that under *NIX, the uname
command is to check the release version of the system.
You can write a Fabric script like this:
from fabric.api import run def host_type(): run('uname -s')
Save the above script as fabfile.py, and you can execute the host_type script on multiple hosts through the fab
command:
$ fab -H localhost,linuxbox host_type [localhost] run: uname -s [localhost] out: Darwin [linuxbox] run: uname -s [linuxbox] out: Linux
You may be required to enter your system password during execution.
Installation
If you see this, it means you are interested in Fabric. But the above operations cannot be performed on your site because you have not installed Fabric. Installing Fabric is very simple. You can use pip
or easy_install
, or you can download the original code for installation.
Task function
Very good, installing Fabric is not difficult for you. Maybe you have successfully performed the previous tasks, now let's go a little deeper.
The task in Fabric is a python function, let us call it "task function". Since it is a python function, some usage of functions also applies to task functions. For example, passing parameters, calling each other, returning values, etc.
First look at an example of passing parameters:
def hello(name="world"): print("Hello %s!" % name)
When executing a task, you can pass parameters to the task function through the command line parameters of fab
:
$ fab hello:name=Holbrook Hello Holbrook!
Examples of combined tasks are as follows:
from fabric.api import run def host_type(): run('uname -s') def hello(name="world"): print("Hello %s!" % name) def composite(name="world"): hello(name) host_type()
Commands provided by Fabric
We have seen the run
function in the fabric.api module earlier, its function is to execute commands on the remote host. The local function is also provided in fabric.api for executing local (the host where the Fabric is located) commands.
is as follows:
from fabric.api import local def lslocal(): local('ls')
Similar to remote commands and local commands, Fabric also distinguishes between remote directories and local directories. The operations provided by Fabric on remote and local directories are cd and lcd respectively. This is easy to understand if you have used command line ftp.
Let’s see an example:
def filepath(): remote_dir = '/opt/xxx' with cd(remote_dir): run("touch README")
The function of the above code is to enter the remote /opt/xxx directory and create a README file.
Fabric also provides many commands, such as file operations.
Manage server connections
In the previous examples, you need to specify the server in the fab command line parameters. It is troublesome when you have to manage a large number of servers. Fabric provides a dictionary env of environment variables, which contains hosts dictionary items, which can define the server that needs to be connected.
is as follows:
from fabric.api import env, run env.hosts = ['host1', 'host2'] def mytask(): run('ls /var/www')
You can also specify the host list to execute the task separately for each task:
from fabric.api import env, run def set_hosts(): env.hosts = ['host1', 'host2'] def mytask(): run('ls /var/www')
When you execute fab set_hosts mytask
like this, you can execute mytask
tasks for the two hosts
specified in set_hosts
. If you are too lazy to write a function, it is the same as specifying it on the fab
command line:
fab mytask:hosts="host1;host2"
In order to make batch tasks more convenient, Role is also defined in Fabric. If you are interested, you can read its official documentation.
Manage SSH passwords, users, ports
Although it is more recommended to use SSH public key authentication, Fabric still provides a mechanism for managing passwords. Fabric provides two layers of passwords.
If your servers have the same password, you can set the default password in env.password
; if the server passwords are different, you can also set the (host, password) pair in env.passwords
for each server Separate ssh password.
The host string above is in this format: username@hostname:port. Therefore, when specifying the ssh password, the ssh user is also specified. Like passwords, you can also specify a default user in env.user
. If neither is specified, you will be prompted to enter the password when executing the fab
command.
Using Fabric, you can manage SSH connections of a series of hosts (including host names, users, passwords), define a series of task functions, and then flexibly specify which tasks to perform on which hosts. This is very useful in scenarios where a large number of hosts need to be managed, such as operation and maintenance, private cloud management, automated application deployment, etc.
Summary
This article is just an introductory document, which far does not reflect the power of Fabric. In fact, Fabric also includes a large number of functions, such as Role definition, remote interaction and exception handling, concurrent execution, file operations, etc., and is not limited to the command line method. Fabric can be called in your application.
The above is the entire content of this article. I hope the content of this article can arouse your interest in Fabric and solve problems in your practical applications. If you have any questions, you can leave a message to communicate.