I'm using Capistrano to deploy configuration files for a legacy non-Ruby application, that for arcane legacy reasons need to be parameterized with the fully-qualified name of the target host, e.g.
name: myservice-stg
identifier: myservice-stg-1.example.org:8675
baseURI: http://myservice-stg-1.example.org:8675
Apart from that, for a given environment, there's no difference between the config files, so I'd like to be able to just define a template (example uses Mustache but could be ERB or whatever):
name: myservice-stg
identifier: {{fqhn}}:8675
baseURI: http://{{fqhn}}:8675
My current idea for a hack is just to use gsub
and a StringIO
:
config_tmpl = File.open('/config/src/config.txt')
config_txt = config_tmpl.gsub('{{fqhn}}', host.hostname)
upload!(StringIO.new(config_txt), 'dest/config.txt')
But it seems like there ought to be a more standard, out-of-the box solution.
Tools like Ansible and Chef are great for this, but might be overkill if this is all you're trying to do.
Your proposed solution looks fairly standard. Using ERB (or other templating system) wouldn't be that much more work and provides flexibility/reusability down the road:
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