Skip to content

Commit 485d9b1

Browse files
committed
Optionally use ssh settings only with vagrant ssh command
This allows bootstrapping the user on a base image, by using default ssh settings during provisioning and user defined ssh settings when doing `vagrant ssh`.
1 parent 2a9f80c commit 485d9b1

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

vagrant/lib/forklift/box_distributor.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,24 @@ def distribute!
6060
end
6161
end
6262

63-
def fetch_from_box_or_settings(box, key)
64-
box.fetch(key) { @settings.fetch(key, nil) }
63+
def fetch_setting(box, key, default = nil)
64+
box.fetch(key) { @settings.fetch(key, default) }
65+
end
66+
67+
def argv_include_any?(tokens)
68+
tokens.any? { |token| ARGV.include?(token) }
69+
end
70+
71+
def add_ssh_setting(box, machine, key)
72+
return if (value = fetch_setting(box, "ssh_#{key}")).nil?
73+
machine.ssh.send("#{key}=", value)
74+
end
75+
76+
def add_ssh_settings(box, machine, keys)
77+
return if fetch_setting(box, 'use_ssh_settings_vagrant_ssh_only', false) && !argv_include_any?(%w[ssh ssh-config])
78+
keys.each do |key|
79+
add_ssh_setting(box, machine, key)
80+
end
6581
end
6682

6783
def define_vm(config, box = {})
@@ -71,11 +87,7 @@ def define_vm(config, box = {})
7187
machine.vm.box = box.fetch('box_name', nil)
7288
machine.vm.box_version = box.fetch('box_version', nil)
7389

74-
%w[username forward_agent keys_only].each do |key|
75-
unless (value = fetch_from_box_or_settings(box, "ssh_#{key}")).nil?
76-
machine.ssh.send("#{key}=", value)
77-
end
78-
end
90+
add_ssh_settings(box, machine, %w[username forward_agent keys_only])
7991

8092
machine.vm.box_check_update = box.fetch('box_check_update', true)
8193

0 commit comments

Comments
 (0)