python Fabric是否支持动态设置env.hosts?
我想动态更改 env.hosts,因为有时我想先部署到一台机器,检查是否正常,然后部署到多台机器。 目前我需要先设置 env.hosts,如何在方法中设置 env.hosts,而不是在脚本启动时全局设置?
I want to change the env.hosts dynamically because sometimes I want to deploy to one machine first, check if ok then deploy to many machines.
Currently I need to set env.hosts first, how could I set the env.hosts in a method and not in global at script start?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您可以动态设置
env.hosts
。我们使用的一种常见模式是:您可以使用它来链接诸如 fab staging 部署 或 fab production 部署 之类的任务。
Yes you can set
env.hosts
dynamically. One common pattern we use is:You would use this to chain the tasks such as
fab staging deploy
orfab production deploy
.有点晚了,但我用 ec2 实现了这一点,就像这样(注意在 EC2 中你不知道 ip/主机名可能是什么,一般来说 - 所以你几乎必须动态地真正考虑环境/系统如何可能会出现 - 另一种选择是使用 dyndns,但这仍然有用):
其中 get_running_instances 和 get_running_instances_by_regex 是利用 boto 的实用程序函数(http://code.google.com/p/boto/)
例如:
这是我的配置的示例:
Kind of late to the party, but I achieved this with ec2 like so (note in EC2 you do not know what the ip/hostname may be, generally speaking - so you almost have to go dynamic to really account for how the environment/systems could come up - another option would be to use dyndns, but then this would still be useful):
where get_running_instances and get_running_instances_by_regex are utility functions that make use of boto (http://code.google.com/p/boto/)
ex:
Here is a sample of what my config looked like:
老问题的又一个新答案。 :) 但我最近发现自己试图动态设置主机,并且确实不同意主要答案。我的动态想法,或者至少是我试图做的,是获取一个刚刚由
boto
创建的实例 DNS 名称,并使用 fab 访问该实例命令。我无法进行 fab staging 部署 ,因为该实例在 fabfile 编辑时不存在 。幸运的是,fabric 确实支持真正的动态主机分配 执行。 (当然,第一次提出问题时这可能不存在,但现在它存在了)。 执行允许您定义要调用的函数以及该命令应使用的env.hosts。例如:
现在我可以执行 fab build_box,它将触发一个用于创建实例的
boto
调用,以及另一个在其上运行的fabric
调用新实例 - 无需在编辑时定义实例名称。Yet another new answer to an old question. :) But I just recently found myself attempting to dynamically set hosts, and really have to disagree with the main answer. My idea of dynamic, or at least what I was attempting to do, was take an instance DNS-name that was just created by
boto
, and access that instance with a fab command. I couldn't dofab staging deploy
, because the instance doesn't exist at fabfile-editing time.Fortunately,
fabric
does support a truly dynamic host-assignment with execute. (It's possible this didn't exist when the question was first asked, of course, but now it does). Execute allows you to define both a function to be called, and the env.hosts it should use for that command. For example:Now I can do
fab build_box
, and it will fire oneboto
call that creates an instance, and anotherfabric
call that runs on the new instance - without having to define the instance-name at edit-time.