简体   繁体   English

Python Fabric:在同一脚本中使用不同的密码在不同的主机上执行功能

[英]Python Fabric : Executing functions on different hosts with different passwords, in the same script

I'm currently trying to automate a deployment process, involving 3 different machines : 我目前正在尝试自动化涉及3种不同机器的部署过程:

  • userA@hostA, pwdA userA @ hostA,pwdA
  • userB@hostB, pwdB userB @ hostB,pwdB
  • userC@hostC, pwdC userC @ hostC,pwdC

Here's the scenario I would like to execute, using Python's Fabric library (to which I'm new) : 这是我想使用Python的Fabric库(我是新来的)执行的方案:

def deploy():
    execute(taskA, hosts=[hostA])
    execute(taskB, hosts=[hostB])
    execute(taskC, hosts=[hostC])

I've tried to set the variable env.passwords like this: 我试图像这样设置变量env.passwords

env.passwords = {'userA@hostA:22':pwdA, 'userB@hostB:22':pwdB, 'userC@hostC:22':pwdC}

But it makes the SSH connection hanging. 但这会使SSH连接挂起。

My current workaround is to modify the variables env.user and env.password before each call to execute (I could also do that at the beginning of taskA, taskB and taskC). 我当前的解决方法是在每次execute调用之前修改变量env.userenv.password (我也可以在taskA,taskB和taskC的开头进行此操作)。 I really do not find that very elegant, and totally outside of Fabric's spirit. 我真的觉得那不是很优雅,完全超出了Fabric的精神。

If someone has ran into such situation, found him/herself with a hanging SSH connection while trying to use the env.passwords dict, I'm all yours ! 如果有人遇到这种情况,在尝试使用env.passwords dict时发现其挂起的SSH连接,我全都是你的! And of course, if anyone already managed to make a more elegant multi-host-password handling, I would be glad to hear about any hint. 当然,如果有人已经设法进行更加优雅的多主机密码处理,那么我将很高兴听到任何提示。

It might be better to keep your ssh details in ~/.ssh/config and have Fabric use that. 最好将ssh详细信息保留在~/.ssh/config并让Fabric使用。 See 看到

As said in my comment above, using the use_ssh_config variable works well. 正如我在上面的评论中所述,使用use_ssh_config变量效果很好。 But in case you SSH with a password, Fabric currently doesn't handle per-host/per-user/per-password connection (which is normal, since using SSH passwords is considered as insecure) 但是,如果您使用密码进行SSH,Fabric当前不处理每个主机/每个用户/每个密码的连接(这很正常,因为使用SSH密码被认为是不安全的)

That's why I dug into Fabric's source code, and added that new "feature". 这就是为什么我要研究Fabric的源代码,并添加新的“功能”的原因。 Here is my repository if you want to have a look. 如果您想看一下, 是我的资料库。 Basically, I used the env.roledefs variable, which lists roles as dictionary entries. 基本上,我使用了env.roledefs变量,该变量将角色列为字典条目。 And each role has a list of hosts, a user, and a password. 每个角色都有一个主机列表,一个用户和一个密码。 Now, if you want to execute a task in one or more hosts (which have different usernames and passwords), there are two ways to do it: 现在,如果要在一个或多个主机(具有不同的用户名和密码)中执行任务,有两种方法可以执行:

  • Annotating the task with the decorator @complete_roles('role1', 'role2') 用装饰器@complete_roles('role1', 'role2')注释任务
  • Using the function execute(task, complete_roles=['role1', 'role2']) 使用函数execute(task, complete_roles=['role1', 'role2'])

Just be sure to have 'role1' and 'role2' as keys in env.roledefs 只是一定要拥有“基于role1”和“role2所”作为钥匙env.roledefs

If you want to have more info about my contribution, just check the two latest commits. 如果您想了解有关我的贡献的更多信息,只需检查两个最新提交即可。 I know, my code is not the cleanest... 我知道,我的代码不是最干净的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM