# fabirc 配置多组服务器 密码与密钥一起使用 key_filename的设置
环境说明
myv myv2 是配置在/etc/hosts 的两台 虚拟机 虚拟机ip.
参考英文文档
官方文档的例子不是给的很详细..
http://docs.fabfile.org/en/1.13/usage/env.html
实例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from fabric.api import *
#只有 student@myv2 这个机器用的是密码
env.hosts = ['gsx@myv','student@myv2','gsx@myv2']
# This dictionary is largely for internal use, and is filled automatically as a per-host-string password cache. Keys are full host strings and values are passwords (strings).
env.passwords = {
'student@myv2:22':'1',
}
# http://docs.fabfile.org/en/1.13/usage/env.html
# May be a string or list of strings, referencing file paths to SSH key files to try when connecting. Passed through directly to the SSH layer. May be set/appended to with -i.
env.key_filename = ['~/.ssh/test','~/.ssh/test8']
def remoteRun():
print "ENV %s" %(env.hosts)
out = run('whoami')
print "Output %s"%(out)
@hosts('gsx@myv2','gsx@myv')
def taskSelectHost():
run('ifconfig')
测试执行
fab -f ./multi_user_and_secret.py remoteRun taskSelectHost
说明
密码的填写 evn.passwords
student@myv 这个用户是使用密码登录,单独在 evn.passwords里面配置. 注意配置的方式是 full host strings 即 用户@IP:port 和 密码.
密钥的填写 env.key_filename
gsx@myv 使用 密钥 ./ssh/test
gsx@myv2 使用 密钥 ./ssh/test8
将连接使用到的密钥 填入 env.key_filename 列表中,密钥填写顺序无关. 不需要hosts与 key_filename 对应起来, 也不需要长度相等 , fabric 会自己尝试密钥.(May be a string or list of strings, referencing file paths to SSH key files to try when connecting. Passed through directly to the SSH layer. May be set/appended to with -i.)