将do dropts与ssh配置结合起来
python-digitalocean-ssh的Python项目详细描述
数字海洋->;ssh配置
安装
python3 -m pip install python-digitalocean-ssh
独立运行,这个python 3脚本将帮助您保持ssh配置与数字海洋水滴同步
$ python3 -m digitalocean_ssh production
· Reading /home/alex/.config/python-digitalocean-ssh/production.json
· Parsing /home/alex/.ssh/config
· Fetching droplets from DO
· Writing into your ssh config file
✓ Done, 11 droplets synced
功能
- 根据液滴的do标记,为每个液滴支持不同的ssh密钥
- 使用不同的配置,可以在ssh配置的不同部分中编写
如何
步骤1:创建json配置文件
把这个保存在~/.config/python-digitalocean-ssh/<name>.json
,其中<name>
是您想如何调用它的,例如production
或testing
或任何其他地方。在这个例子中,我将使用production
。
{"token":"DIGITAL_OCEAN_READ_ONLY_TOKEN_HERE","keys":{"tagToKey":{},"default":{"key":"common","priority":0}},"startMark":"# DO production","endMark":"# /DO production","hostPrefix":"do-prod-"}
note:这是最简单的配置文件,对每个液滴和液滴名称使用与Host
相同的键,有关更多选项,请继续阅读。
- 生成新的个人do api只读访问令牌here
hostPrefix
是要在ssh配置中的Host
键中为通过此配置加载的每个液滴添加的前缀,可以是您想要的任何内容
步骤2:在ssh配置中添加2个标记
上面的json配置包含startMark
和endMark
。这些应该在ssh配置中的某个地方,可以是您想要的任何东西(但是,对于ssh配置注释,可以从#
开始):
# DO production # /DO production
在这两个标记之间,脚本将delete所有内容并添加新条目。注意不要在这两个标记之间添加自己的主机。
第3步:运行脚本
$ python3 -m digitalocean_ssh production
· Reading /home/alex/.config/python-digitalocean-ssh/production.json
· Parsing /home/alex/.ssh/config
· Fetching droplets from DO
· Writing into your ssh config file
✓ Done, 11 droplets synced
现在您的ssh配置如下:
# DO production Host do-prod-control-center1517024146 # control-center1517024146 Hostname X.X.X.X IdentityFile ~/.ssh/common User user Host do-prod-control-center1517027030 # control-center1517027030 Hostname X.X.X.X IdentityFile ~/.ssh/common User user ... 9 more entries # /DO production
如果自动生成了从液滴名称派生的难看的Host
名称,则可以改为使用液滴标记;继续阅读。
将其用作模块
在适当的地方使用配置文件,您可以将其用作一个模块来创建强大的python脚本:
fromdigitalocean_sshimportDOimportsysclient=DO(True)# enable debuggingconfig_type=sys.argv[1]# must pass the configuration type as an argument, e.g. 'production'config=client.get_config(config_type)ssh_config=client.parse_ssh_config(config)droplets=client.fetch_droplets(config)print(droplets)# DO droplets with combined ip/tags/ssh config information
我想使用不同的ssh密钥,而不是common
!
- 更改
keys.default.key
设置
我想使用不同的ssh密钥每滴标签!
- 更改
keys.tagToKey
设置并添加如下条目:
"control-center":{"key":"cc_prv","priority":7},"consul-server":{"key":"cs_prv","priority":6},"postgres-master":{"key":"common","priority":5}
最后的配置如下:
{"token":"DIGITAL_OCEAN_READ_ONLY_TOKEN_HERE","keys":{"tagToKey":{"control-center":{"key":"cc_prv","priority":7},"consul-server":{"key":"cs_prv","priority":6},"postgres-master":{"key":"common","priority":5}},"default":{"key":"common","priority":0}},"startMark":"# DO production","endMark":"# /DO production","hostPrefix":"do-prod-"}
important:一个水滴可以有多个标记,这就是为什么这里有一个名为priority
的字段。在上面的例子中,如果一个液滴同时有control-center
和consul-server
标记,它将使用具有更高优先级的键(这里是control-center
)。如果液滴没有标记或其标记不出现在tagToKey
中,则它将使用默认键。
对于匹配特定标记的液滴,现在ssh配置中的Host
将具有标记的名称,而不是液滴名称:
# DO production Host do-prod-control-center # control-center1517024146 Hostname X.X.X.X IdentityFile ~/.ssh/cc_prv User user Host do-prod-control-center2 # control-center1517027030 Hostname X.X.X.X IdentityFile ~/.ssh/cc_prv User user ... more entries # /DO production
这对于自动生成液滴名称的大型环境非常方便
note:水滴名称在每个条目的第一行中仍然作为注释可见
note:如上例所示,如果两个或多个液滴共享同一个标记,则会在Host
值后面追加一个升序数字。
现在,您可以使用ssh的tab completion轻松查看所有内容,并连接到任何位置:
$ ssh do-prod- <hit TAB key twice>
do-prod-control-center do-prod-mongodb do-prod-load-balancer do-prod-nodejs2 do-prod-postgres-slave do-prod-blog
do-prod-control-center2 do-prod-landing-page do-prod-nodejs do-prod-postgres-master do-prod-redis
我有生产和测试,我在10个不同的公司工作!
只需在~/.config/python-digitalocean-ssh/
下创建不同的配置文件,每个用例一个,比如production.json
和testing.json
。对于每个用例,使用不同的hostPrefix
将非常有用。
另外,在ssh配置文件中添加不同的标记,例如:
# DO production # /DO production # DO testing # /DO testing
现在如果您运行
$ python3 -m digitalocean_ssh production
它将继续从production.json
读取并在ssh配置中写入相应的标记。如果您运行
$ python3 -m digitalocean_ssh testing
它将继续从testing.json
读取并写入相应的标记。
我可以随时安全地重新运行脚本吗?
是的,只要你没有包括在配置中指定的标记之间输入y。每次运行脚本时,标记之间的所有内容都将被删除。