SSHKit命令或Capistrano任务,用于在上传时过滤/替换令牌

时间:2018-12-19 20:06:42

标签: capistrano sshkit

我正在使用Capistrano为旧版非Ruby应用程序部署配置文件,出于不可思议的旧版原因,需要使用目标主机的标准名称进行参数设置,例如

name: myservice-stg
identifier: myservice-stg-1.example.org:8675
baseURI: http://myservice-stg-1.example.org:8675

除此之外,对于给定的环境,配置文件之间没有区别,因此我希望能够仅定义模板(示例使用Mustache,但可以是ERB或其他):

name: myservice-stg
identifier: {{fqhn}}:8675
baseURI: http://{{fqhn}}:8675

我当前的骇客想法只是使用gsubStringIO

config_tmpl = File.open('/config/src/config.txt')
config_txt = config_tmpl.gsub('{{fqhn}}', host.hostname)
upload!(StringIO.new(config_txt), 'dest/config.txt')

但是似乎应该有一个更标准的,开箱即用的解决方案。

1 个答案:

答案 0 :(得分:2)

Ansible和Chef之类的工具非常适合此操作,但是如果您只是想这样做,可能会显得过分杀伤。

您提出的解决方案看起来相当标准。使用ERB(或其他模板系统)将不会花费太多的工作,并且会在以后提供灵活性/可重用性:

template_path = File.open('/config/src/config.txt.erb')
config_txt = ERB.new(File.new(template_path).read).result(binding)
upload! StringIO.new(config_txt), 'dest/config.txt', mode: 0644

ERB:

name: myservice-stg
identifier: <%= host.hostname %>:8675
baseURI: http://<%= host.hostname %>:8675
相关问题