1.如何调用该方法:
from fabric import context_managersps:上下文管理需要用with关键字启用: 如: with context_managers.cd('/tmp'): run(...) 这样才能保证是在该目录下操作。 2.方法:(1)with context_managers.cd(一个目录的路径): 指定进入一个目录,然后在该目录下执行操作。 例: def cd(path): with context_managers.cd(path): run('touch a.txt') 在path的值的目录下创建一个a.txt文件。 (2)with context_managers.hide(['warnings','running','stdout','stderr','debug','everything']): 在hide中指定要隐藏的类型,warnings:警告、running:运行、stdout:标准输出、stderr:标准错误、debug:调试信息 例: #1.不适用hide def no_hide(cmd): run('echo nimei') 显示: [192.168.1.219] Executing task 'no_hide' [192.168.1.219] run: echo nimei [192.168.1.219] Login password for 'root': [192.168.1.219] out: nimei [192.168.1.219] out: Done. Disconnecting from 192.168.1.219... done. #2.running def hide_running(cmd): with context_managers.hide('running'): run('echo nimei') 显示: [192.168.1.219] Executing task 'running_hide' [192.168.1.219] Login password for 'root': #此行本是[192.168.1.219] run: echo nimei,但被hide过滤掉了 [192.168.1.219] out: nimei [192.168.1.219] out: Done. Disconnecting from 192.168.1.219... done. #3.stdout def stdout_hide(cmd): with context_managers.hide('stdout'): run(cmd) 显示 [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f hide_test.py stdout_hide:'echo nimei' [192.168.1.219] Executing task 'stdout_hide' [192.168.1.219] run: echo nimei [192.168.1.219] Login password for 'root': #这一行的[192.168.1.219] out: nimei和[192.168.1.219] out: 没有了,这就是过滤标准输出 Done. Disconnecting from 192.168.1.219... done. (3)with context_managers.show('debug'): 这个方法和hide正相反,hide是掩藏,而show是显示,它的作用只是为了开启默认掩藏的debug模式。(4)with context_managers.path(path,behavior='[append|prepend|replace]'): 这个方法类似于在系统中执行:export PATH=$PATH:path1:path2,是指定命令路径的。 而path方法除了传入一个path字符串参数以外,还要换入一个behavior=参数,参数有3个: 1.append添加在$PATH的最后 2.prepend添加在$PATH的最前方(个人认为,这个参数是为了让你写的执行文件不至于和系统命令重名,它会优先执行你的执行文件) 3.replace将$PATH中的内容清空,然后将你将的path变量赋给它 例: #脚本1:exec_file_echo.sh ##!/bin/bash #echo nimei #脚本2:ls ##!/bin/bash #echo nimei #1.append def append_path(path,exec_file): with context_managers.path(path,behavior='append'): run(exec_file) 显示: [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f path_test.py append_path:'/gyk_fab/','exec_file_echo.sh' [192.168.1.219] Executing task 'append_path' [192.168.1.219] run: exec_file_echo.sh [192.168.1.219] Login password for 'root': [192.168.1.219] out: nimei [192.168.1.219] out: Done. Disconnecting from 192.168.1.219... done. #2.prepend def prepend_path(path,exec_file): with context_managers.path(path,behavior='prepend'): run(exec_file) 显示: [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f path_test.py prepend_path:'/gyk_fab/','ls'#ls本身不执行,而执行我写的ls脚本,这就是将路径添加在最前端的用处 [192.168.1.219] Executing task 'prepend_path' [192.168.1.219] run: ls [192.168.1.219] Login password for 'root': [192.168.1.219] out: nimei [192.168.1.219] out: Done. Disconnecting from 192.168.1.219... done. #replace #范例1 [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f path_test.py replace_path:'/gyk_fab/','which pwd' [192.168.1.219] Executing task 'replace_path' [192.168.1.219] run: which pwd [192.168.1.219] Login password for 'root': [192.168.1.219] out: /bin/bash: which: command not found [192.168.1.219] out: Fatal error: run() received nonzero return code 127 while executing! Requested: which pwd Executed: /bin/bash -l -c "export PATH=\"\"/gyk_fab/\"\" && which pwd" Aborting. Disconnecting from 192.168.1.219... done. run() received nonzero return code 127 while executing! Requested: which pwd Executed: /bin/bash -l -c "export PATH=\"\"/gyk_fab/\"\" && which pwd" [root@salt-monion-1 gyk_fab]# which pwd /bin/pwd #范例2 [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f path_test.py replace_path:'/gyk_fab/','ls' [192.168.1.219] Executing task 'replace_path' [192.168.1.219] run: ls [192.168.1.219] Login password for 'root': [192.168.1.219] out: /gyk_fab/ls: line 4: find: command not found [192.168.1.219] out: Fatal error: run() received nonzero return code 127 while executing! Requested: ls Executed: /bin/bash -l -c "export PATH=\"\"/gyk_fab/\"\" && ls" Aborting. Disconnecting from 192.168.1.219... done. run() received nonzero return code 127 while executing! Requested: ls Executed: /bin/bash -l -c "export PATH=\"\"/gyk_fab/\"\" && ls" [root@salt-monion-1 gyk_fab]# cat ls #!/bin/bash #echo nimei find / -name tmp (5)with context_managers.prefix(系统命令): 该方法的作用和cmd1&&cmd2相同,就是当本命令执行成功后才能执行其他操作。 例: #!/bin/env python2.7 from fabric import context_managers from fabric.operations import run def no_prefix(prefix_cmd,run_cmd): run(prefix_cmd) run(run_cmd) def prefix_test(prefix_cmd,run_cmd): with context_managers.prefix(prefix_cmd): run(run_cmd) [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f test_prefix.py prefix_test:'cd /tmp','ls' [192.168.1.219] Executing task 'prefix_test' [192.168.1.219] run: ls [192.168.1.219] Login password for 'root': [192.168.1.219] out: dir pip_build_root timer_creategFkcio.c yum.log yum_save_tx-2015-03-16-23-09uxAPw7.yumtx [192.168.1.219] out: Done. Disconnecting from 192.168.1.219... done. [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f test_prefix.py prefix_test:'cd tmp','ls' [192.168.1.219] Executing task 'prefix_test' [192.168.1.219] run: ls [192.168.1.219] Login password for 'root': [192.168.1.219] out: /bin/bash: line 0: cd: tmp: 没有那个文件或目录 [192.168.1.219] out: Fatal error: run() received nonzero return code 1 while executing! Requested: ls Executed: /bin/bash -l -c "cd tmp && ls" Aborting. Disconnecting from 192.168.1.219... done. run() received nonzero return code 1 while executing! Requested: ls Executed: /bin/bash -l -c "cd tmp && ls" [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f test_prefix.py no_prefix:'cd /tmp','ls' [192.168.1.219] Executing task 'no_prefix' [192.168.1.219] run: cd /tmp [192.168.1.219] Login password for 'root': [192.168.1.219] run: ls [192.168.1.219] out: anaconda-ks.cfg install.log install.log.syslog [192.168.1.219] out: Done. Disconnecting from 192.168.1.219... done. [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f test_prefix.py no_prefix:'cd tmp','ls' [192.168.1.219] Executing task 'no_prefix' [192.168.1.219] run: cd tmp [192.168.1.219] Login password for 'root': [192.168.1.219] out: /bin/bash: line 0: cd: tmp: 没有那个文件或目录 [192.168.1.219] out: Fatal error: run() received nonzero return code 1 while executing! Requested: cd tmp Executed: /bin/bash -l -c "cd tmp" Aborting. Disconnecting from 192.168.1.219... done. run() received nonzero return code 1 while executing! Requested: cd tmp Executed: /bin/bash -l -c "cd tmp" (6)with context_managers.setting(env的key=env相应的值,...): 暂时(在with的作用域里),覆盖或更新任何提到的关键字的env变量的值,用env.键设置是全局变量,用这个可以设置局部变量。 范例暂时不写,等整理到env的时候在说。 (7)with context_managers.quiet(): 它等同于with context_managers.settings(context_managers.hide('everything'),warn_only=True): 例: #!/bin/env python2.7 from fabric.operations import run from fabric import context_managers from fabric import colors from fabric.api import env env.hosts=['192.168.1.219'] def quiet_test(cmd): with context_managers.quiet(): status=run(cmd) if status.succeeded: print colors.green('nimei') [root@salt-monion-1 gyk_fab]# fab -f quiet_test.py quiet_test:'ls' [192.168.1.219] Executing task 'quiet_test' [192.168.1.219] Login password for 'root': nimei Done. Disconnecting from 192.168.1.219... done. [root@salt-monion-1 gyk_fab]# fab -f quiet_test.py quiet_test:'lasdddd' [192.168.1.219] Executing task 'quiet_test' [192.168.1.219] Login password for 'root': Done. Disconnecting from 192.168.1.219... done. 同功能代码: #!/bin/env python2.7 from fabric.operations import run from fabric import context_managers from fabric import colors from fabric.api import env env.hosts=['192.168.1.219'] env.password='659171' def quiet_test(cmd): #context_managers.quiet() with context_managers.quiet(): status=run(cmd) if status.succeeded: print colors.green('nimei') def equest_quiet(cmd): #context_managers.settings() with context_managers.settings(context_managers.hide('everything'),warn_only=True): status=run(cmd) if status.succeeded : print colors.green('nimei') else: print colors.red('cao nimei') [root@salt-monion-1 gyk_fab]# fab -f quiet_test.py equest_quiet:'lsaaa' [192.168.1.219] Executing task 'equest_quiet' cao nimei Done. Disconnecting from 192.168.1.219... done. [root@salt-monion-1 gyk_fab]# fab -f quiet_test.py equest_quiet:'ls' [192.168.1.219] Executing task 'equest_quiet' nimei Done. Disconnecting from 192.168.1.219... done. (7)with context_managers.warn_only(): 等同于context_mangers.setting(warn_only=True): (8)context_managers.shell_env(键=值): 为shell命令设置环境变量。比如说安装ACE时需要制定ACE_HOME=安装包路径,这时候就可以使用shell_env 例: