简体   繁体   English

如何从python脚本'刀EC2服务器创建'

[英]how to do 'knife ec2 server create' from python script

I am trying to convert my ant script to python. 我正在尝试将蚂蚁脚本转换为python。 The ant script runs knife ec2 server create command. ant脚本运行knife ec2 server create命令。 What is the best practice to run knife ec2 server create from Python? 从Python运行刀ec2服务器创建的最佳实践是什么?

BTW, is python the right scripting technology for automation? 顺便说一句,python是正确的自动化脚本技术吗?

I'm not familiar with a Python interface for knife, but I see no reason for this not to work: 我对刀的Python接口不熟悉,但我认为没有理由不起作用:

import sh

sh.knife.ec2.server.create(r='role[x]', I='ami-xxxxxxx', f='t2.micro', aws-access-key-id=ACCESS_KEY, aws-secret-access-key=SECRET_KEY)

You'll need to pip install sh for it, so you can also use subprocess for it instead if you don't want extra dependencies: 您需要为此pip install sh ,因此,如果您不需要额外的依赖项,也可以使用子进程代替它:

import subprocess

subprocess.Popen('knife ec2 server create ...', shell=True)

If I were you, i'd write a small client for running knife commands more comfortably though, as if you use knife, this is probably not the only command you're going to run. 如果我是您,那么我会编写一个小型客户端来更舒适地运行刀命令,就像您使用刀一样,这可能不是您要运行的唯一命令。 sh would be ideal for that. sh是最理想的选择。

Regarding your second question, you should open another issue for it. 关于第二个问题,您应该打开另一个问题。

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

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