1. Call Method:
From fabric.state Import env
From fabric.state Import output
2.env
Env VARs complete list (full list of env VARs)
Key: Default value
' 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
Commonly used for:
(1) env.hosts=[ip1,ip2] #这里可以写入一个ip列表, this can replace-h
(2). env.roledefs = {role name: [IP List], ...} #角色定义, use a dictionary to record roles and corresponding IP lists so that when you decorate a method with @roles (role name), you can call these IPs.
(3). env.dedupe_hosts=true| False #去重, remove duplicate IP in env.hosts and env.roledefs
(4). Env.exclude_hosts = [IP1,IP2] #将列表中的ip排除, this is for-when the list (or dictionary) in env.hosts or env.passwords is too large, and you can determine which IP is not executed, so that you do not have to modify the list
(5). Env.user = ' System user name, default is root ' #指定ssh到哪个用户上执行命令
(6). env.skip_bad_hosts = true| False #跳过坏主机, but this needs to be executed with the following
(7). Env.timeout = 1 #定义超时时间
(8). env.password= ' Password ' #默认密码, if all the hosts defined in env.hosts or env.roledefs are this password, you can use it
(9). Env.passwords = {' User name [email protected]: Port ': ' username [email protected]: Port ': Password 2,...} #如果用户名, passwords are different, you can use this method to set up automatic connection, but you must use all keys to generate a list, assigned to the hosts (must be the same)
Such as:
#!/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 = [' [Email protected]:22 ']
#env. roledefs = {' Test ': [' 192.168.1.219 ', ' 192.168.1.200 ']}
env.hosts = [' [Email protected]:22 ',
' [Email protected]:22 ',
]
Env.passwords = {' [email protected]:22 ': ' 123456 ',
' [Email protected]:22 ': ' 123456 ',
}
# @hosts (' 192.168.1.200 ')
# @roles (' test ')
def env_exclude_test (cmd):
With hide (' everything '):
Run (CMD)
2.output #设置输出的, like Context_managers.hide|show, is different, it's global, that's local.
(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的运行成功的输出结果
[Email protected] 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的运行失败的输出结果
[Email protected] 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
=======================================================================
This article from "My Sky" blog, declined reprint!
State statistics for fabric (or set environment-system and fabric itself)