Last active
March 31, 2024 17:04
-
-
Save avoidik/da5bfc6365390e84057c0184f3d08d3f to your computer and use it in GitHub Desktop.
Start Vagrant VM and generate SSH config using triggers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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