First let's look at an example first. We know that under the *nix,
uname
The command is to view the release version of the system.
You can write a fabric script like this:
From Fabric.api import Rundef host_type (): Run (' uname-s ')
By saving the above script as fabfile.py, you can fab
execute the Host_type script on multiple hosts by command:
$ fab-h Localhost,linuxbox Host_type[localhost] run:uname-s[localhost] out:darwin[linuxbox] run:uname-s[linuxbox] ou T:linux
You may be required to enter the system password during execution.
Installation
If you see this, you're starting to get interested in fabric. However, the above operation cannot be performed in your place because you have not installed the fabric yet. Installation of fabric is simple, can be used pip
or easy_install
, can also download the original code installation.
Task function
Well, installing fabric doesn't baffle you. Perhaps you have successfully performed the previous task and now let's go a little deeper.
The task in fabric is a python function, let's call it "task function". Since it is a Python function, some usage of the function also applies to task functions. such as passing parameters, calling each other, returning values, and so on.
First look at an example of passing parameters:
def hello (name= "World"): Print ("Hello%s!"% name)
When performing a task, you can fab
pass parameters for the task function through the command line arguments:
$ fab Hello:name=holbrookhello holbrook!
Examples of combinatorial tasks are as follows:
From Fabric.api import Rundef host_type (): Run (' uname-s ') def hello (name= "World"): Print ("Hello%s!"% name) def composite ( Name= "World"): Hello (name) Host_type ()
The commands provided by fabric
We've seen the functions in the Fabric.api module in the front run
, and the function is to execute the command on the remote host. The local function is also available in the Fabric.api to execute the locally (fabric host) command.
As follows:
From Fabric.api import Localdef lslocal (): local (' ls ')
Similar to remote commands and local commands, fabric also distinguishes between remote directories and local directories. The remote and local directory operations provided by fabric are CD and LCD respectively. If you've ever used command-line FTP, it's easy to understand.
Let's look at an example:
def filepath (): Remote_dir = '/opt/xxx ' with CD (REMOTE_DIR): run ("Touch README")
The function of the above code is to go to the remote/opt/xxx directory and create a readme file.
Fabric also provides a number of commands, such as file manipulation.
Manage Server connections
In the previous example, you would need to specify the server in the Fab command-line parameters. It is cumbersome to manage a large number of servers. Fabric provides a dictionary of environment variables, env, which contains the hosts dictionary entry that defines the server that needs to be connected.
As follows:
From FABRIC.API import env, runenv.hosts = [' host1 ', ' Host2 ']def mytask (): Run (' ls/var/www ')
You can also specify the host list for each task individually to perform the task:
From FABRIC.API import env, Rundef set_hosts (): env.hosts = [' host1 ', ' Host2 ']def mytask (): Run (' ls/var/www ')
When you do this fab set_hosts mytask
, you can set_hosts
perform tasks for the two specified in host
mytask
. If you are too lazy to write functions, you can specify them on the fab
command line as well:
Fab mytask:hosts= "Host1;host2"
To make it easier to perform bulk tasks, role is defined in fabric and is interested in reading its official documentation.
Manage SSH passwords, users, ports
Although it is more recommended to use SSH public key authentication, fabric provides a mechanism for managing passwords. Fabric provides a two-layer password.
If your server has the same password, you can set the default password in, and env.password
if the server password is different, you can set the env.passwords
(Host,password) pair in, and set a separate SSH password for each server.
The host string above takes this format: Username@hostname:port. Therefore, the SSH password is specified while the SSH user is specified. As with the password, you can also env.user
specify a default user in. If none is specified, fab
you are prompted to enter the password when the command is executed.
With fabric, you can manage a series of host SSH connections (including host names, users, passwords), define a series of task functions, and then flexibly specify which hosts perform which tasks on which host. This is very useful for scenarios where you need to manage a host of hosts, such as operations, private cloud management, application automation deployment, and more.
Summarize
This article is just a getting started document, far from showing the power of fabric. In fact, Fabric also includes a number of functions, such as role definition, remote interaction and exception handling, concurrent execution, file manipulation, and not just the command line, which can call fabric in your app.
The above is the entire content of this article, I hope that the content of this article can arouse your interest in fabric, and in your practical application to solve the problem. If in doubt, you can leave a message to communicate.