Please indicate the source of Reprint: http://blog.csdn.net/l1028386804/article/details/79032776
1, View local and remote host information
# -*- coding:UTF-8 -*- ''' //View local and remote host information //This instance calls the local() method to execute the local (Master) command, and adds the "@ runs'once" modifier to ensure that the task function is executed only once Created on 2018 January 11, 2010 @author: liuyazhuang ''' from fabric.api import * env.user = 'root' env.hosts = ['192.168.209.121', '192.168.209.122'] env.password = 'Password' #View the local system information, only run once when there are multiple hosts @runs_once def local_task(): #Local task function local("uname -a") def remote_task(): #The following function of with is to let the statement of the following expression inherit the current state and realize the effect of CD / data / logs & & ls-l with cd("/data/logs"): run("ls -l")
2, Get remote directory list dynamically
# -*- coding:UTF-8 -*- ''' Get remote directory list dynamically This instance uses the @ task modifier to mark the entry function go() to be externally visible, and the "@ runs'once" modifier is used to receive user input, Finally, the worktask() task function is invoked to implement remote command execution. It mainly realizes the function of dynamically inputting the remote directory name and obtaining the directory list. Because we only need to input it once and then display it on all hosts For the list information of the directory, a subfunction, input raw(), is called and the @ runs ﹣ once modifier is configured to achieve this purpose Created on January 11, 2018 @author: liuyazhuang ''' from fabric.api import * env.user = "root" env.hosts = ['192.168.209.121', '192.168.209.122'] env.password = 'password' #During host traversal, only the first one triggers this function @runs_once def input_raw(): return prompt("please input dircectory name:" , default="/home") def worktask(dirname): run("ls -l " + dirname) #Only go function is allowed to be visible to fab command and fab command. After pip install fabric is executed, tasks can be executed through fab go @task def go(): getdirname = input_raw() worktask(getdirname)
3, Upload and execution of gateway mode files
# -*- coding:UTF-8 -*- ''' //Upload and execution of gateway mode files //This example defines the gateway mode through the evn object of Fabric, that is, the transit and bastion environment. The format defined is: env.gateway = '192.168.209.121' Among them, IP'192.168.209.121'For fortress IP,Combined with task function //Upload and execute the target machine file. Compared with paramiko, the implementation method is much simpler, and the task function written does not need to consider the bastion machine environment at all, //Just configure env.gateway. Created on 2018 January 11, 2010 @author: liuyazhuang ''' from fabric.api import * from fabric.context_managers import * from fabric.contrib.console import confirm env.user = 'root' #Define the IP of bastion machine as a transit device for file upload and execution env.gateway = '192.168.209.123' #Remote host list env.hosts = ['192.168.209.121', '192.168.209.122'] #Assuming that the passwords of all hosts are different, you can specify one by one through the env.passwords dictionary variable env.passwords = { 'root@192.168.209.121:22' : 'Password 1', 'root@192.168.209.122:22' : 'Password 2', 'root@192.168.209.123:22' : 'Password 3', } #Local installation package path lpackpath = "/home/install/lnmp0.9.tar.gz" #Remote installation package path rpackpath = "/tmp/install" @task #It can be seen from the fab command that after pip install fabric is executed, the task can be executed through the fab put task def put_task(): run("mkdir -p /tmp/install") with settings(warn_only=True): #Upload installation package result = put(lpackpath, rpackpath) if result.failed and not confirm("put file failed, COntinue[Y/N]?"): abort("Aborting file put task!") @task #It can be seen from the fab command that after pip install fabric is executed, the task can be executed through fab run task def run_task(): #Execute remote command and install lnmp environment with cd("/tmp/install"): run("tar -zxvf lnmp0.9.tar.gz") with cd("lnmp0.9/"): #Use with to continue to inherit / tmp/install directory location status run("./centos.sh") @task #It can be seen from the fab command that after pip install fabric is executed, tasks can be executed through fab go def go(): #Upload and installation combination put_task() run_task()