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
=======================================================================
python