Skip to content

Instantly share code, notes, and snippets.

@avoidik
Last active March 31, 2024 17:04
Show Gist options
  • Save avoidik/da5bfc6365390e84057c0184f3d08d3f to your computer and use it in GitHub Desktop.
Save avoidik/da5bfc6365390e84057c0184f3d08d3f to your computer and use it in GitHub Desktop.
Start Vagrant VM and generate SSH config using triggers
require Vagrant.source_root.join("plugins/commands/ssh_config/command")
Vagrant.configure(2) do |config|
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = "ubuntu"
ubuntu.trigger.after :up do |trigger|
trigger.ruby do |env,machine|
ssh_template = VagrantPlugins::CommandSSHConfig::Command.new(["--host", machine.name.to_s, machine.name.to_s], env)
ssh_config = File.join(File.expand_path('~'), '.ssh', 'config.d', machine.name.to_s)
$stdout = File.new(ssh_config, 'w')
ssh_template.execute
$stdout = STDOUT
end
end
ubuntu.trigger.after :destroy do |trigger|
trigger.ruby do |env,machine|
ssh_config = File.join(File.expand_path('~'), '.ssh', 'config.d', machine.name.to_s)
File.delete(ssh_config) if File.exist?(ssh_config)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment