SSHKit 命令或 Capistrano 任务上传到 filter/replace 个令牌

SSHKit command or Capistrano task to filter/replace tokens on upload

我正在使用 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')

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

像 Ansible 和 Chef 这样的工具非常适合这个,但如果这就是您想要做的全部,则可能会过大。

您提出的解决方案看起来相当标准。使用 ERB(或其他模板系统)不会有更多的工作,并提供 flexibility/reusability 的道路:

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

雇员再培训局:

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