1.调用方法:
from fabric.state import env
from fabric.state import output
2.env
env vars完整列表(Full list of env vars)
键 : 默认值
'abort_exception': None
'abort_on_prompts': False
'again_prompt': 'Sorry, try again.'
'all_hosts': []
'always_use_pty': True
'colorize_errors': False,
'combine_stderr': True,
'command': None,
'command_prefixes': [],
'command_timeout': None,
'connection_attempts': 1,
'cwd': '',
'dedupe_hosts': True,
'default_port': '22',
'disable_known_hosts': False,
'eagerly_disconnect': False,
'echo_stdin': True,
'effective_roles': [],
'exclude_hosts': [],
'fabfile': 'fabfile',
'forward_agent': False,
'gateway': None,
'hide': ('NO', 'DEFAULT'),
'host': None,
'host_string': None,
'hosts': [],
'keepalive': 0,
'key_filename': None,
'lcwd': '',
'linewise': False,
'local_user': 'root',
'no_agent': False,
'no_keys': False,
'ok_ret_codes': [0],
'output_prefix': True,
'parallel': False,
'password': None,
'passwords': {},
'path': '',
'path_behavior': 'append',
'pool_size': 0,
'port': '22',
'prompts': {},
'rcfile': '/root/.fabricrc',
'real_fabfile': None,
'reject_unknown_hosts': False,
'remote_interrupt': None,
'roledefs': {},
'roles': [],
'shell': '/bin/bash -l -c',
'shell_env': {},
'show': ('NO', 'DEFAULT'),
'skip_bad_hosts': False,
'skip_unknown_tasks': False,
'ssh_config_path': '~/.ssh/config',
'sudo_prefix': "sudo -S -p '%(sudo_prompt)s' ",
'sudo_prompt': 'sudo password:',
'sudo_user': None,
'system_known_hosts': None,
'tasks': [],
'timeout': 10,
'use_exceptions_for': {'network': False},
'use_shell': True,
'use_ssh_config': False,
'user': 'root',
'version': '1.10.1',
'warn_only': False
常用的:
(1)env.hosts=[ip1,ip2] #这里可以写入一个ip列表,这样可以替代-H
(2).env.roledefs = { 角色名:[ip列表],...} #角色定义,用一个字典记录角色和相对应的ip列表,这样,你用@roles(角色名)修饰某个方法的时候,你就可以调用这些ip了。
(3).env.dedupe_hosts=True|False #去重,去掉env.hosts和env.roledefs中重复的ip
(4).env.exclude_hosts = [ip1,ip2] #将列表中的ip排除,这个是用于——当env.hosts或env.passwords中的列表(或字典)过大,而你可以确定哪些ip不执行,这样,你就不用修改列表
(5).env.user = '系统用户名,默认是root' #指定ssh到哪个用户上执行命令
(6).env.skip_bad_hosts = True|False #跳过坏主机,但这个需要和下面那个一起执行
(7).env.timeout = 1 #定义超时时间
(8).env.password= '密码' #默认密码,如果env.hosts或env.roledefs中定义的所有的主机都是这个密码,就可以用它
(9).env.passwords = {'用户名1@ip1:端口':'用户名1@ip1:端口':密码2,...} #如果用户名、密码都不一样的话,可以用这种方法设置自动连接,但是,你必须用所有key生成一个列表,赋给hosts(必须都一样)
如:
#!/bin/env python2.7
from fabric.api import run
from fabric.api import env
from fabric.api import roles,hosts
from fabric.api import hide
#env.user='root'
#env.password = '659171'
env.exclude_hosts = ['root@192.168.1.200:22']
#env.roledefs = { 'test' : ['192.168.1.219','192.168.1.200'] }
env.hosts = ['root@192.168.1.219:22',
'root@192.168.1.200:22',
]
env.passwords = { 'root@192.168.1.219:22' : '123456',
'root@192.168.1.200:22' : '123456',
}
#@hosts('192.168.1.200')
#@roles('test')
def env_exclude_test(cmd):
with hide('everything'):
run(cmd)
2.output #设置输出的,如同context_managers.hide|show,不同的是,这是全局的,那个是局部的
(1)output['status'] = True|False
(2)output['aborts'] = True|False
(3)output['warnings'] = True|False
(4)output['running'] = True|False
(5)output['stdout'] = True|False
(6)output['stderr'] = True|False
(7)output['debug'] = True|False
#这是所有都是True的运行成功的输出结果
[root@salt-monion-1 gyk_fab]# fab -f output_test.py output_test:'ls'
Using fabfile '/gyk_fab/output_test.py'
Commands to run: output_test
Parallel tasks now using pool size of 2
[192.168.1.219] Executing task 'output_test'
[192.168.1.219] run: /bin/bash -l -c "ls"
[192.168.1.200] Executing task 'output_test'
[192.168.1.200] run: /bin/bash -l -c "ls"
Done.
Disconnecting from 192.168.1.200... done.
Disconnecting from 192.168.1.219... done.
#这是所有都是True的运行失败的输出结果
[root@salt-monion-1 gyk_fab]# fab -f output_test.py output_test:'laaa'
Using fabfile '/gyk_fab/output_test.py'
Commands to run: output_test
Parallel tasks now using pool size of 2
[192.168.1.219] Executing task 'output_test'
[192.168.1.219] run: /bin/bash -l -c "laaa"
Fatal error: run() received nonzero return code 127 while executing!
Requested: laaa
Executed: /bin/bash -l -c "laaa"
None
=========================== Standard output ===========================
/bin/bash: laaa: command not found
=======================================================================
Aborting.
Disconnecting from 192.168.1.219... done.
run() received nonzero return code 127 while executing!
Requested: laaa
Executed: /bin/bash -l -c "laaa"
None
=========================== Standard output ===========================
/bin/bash: laaa: command not found
=======================================================================