diff --git a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb index d3edf3e93..2ee5e9c54 100644 --- a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb +++ b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb @@ -37,7 +37,27 @@ module VagrantPlugins # The ! indicates that this method modifies its argument. def add_ips_to_env!(env) adapter, host_ip = find_host_only_adapter - machine_ip = read_static_machine_ips || read_dynamic_machine_ip(adapter) + machine_ip = read_static_machine_ips + + if !machine_ip + # No static IP, attempt to use the dynamic IP. + machine_ip = read_dynamic_machine_ip(adapter) + else + # We have static IPs, also attempt to read any dynamic IPs. + # If there is no dynamic IP on the adapter, it doesn't matter. We + # already have a static IP. + begin + dynamic_ip = read_dynamic_machine_ip(adapter) + rescue Vagrant::Errors::NFSNoGuestIP + dynamic_ip = nil + end + + # If we found a dynamic IP and we didn't include it in the + # machine_ip array yet, do so. + if dynamic_ip && !machine_ip.include?(dynamic_ip) + machine_ip.push(dynamic_ip) + end + end raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip diff --git a/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb b/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb index 3cead74f4..c358f6720 100644 --- a/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb +++ b/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb @@ -116,5 +116,14 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do expect(env[:nfs_host_ip]).to eq("1.2.3.4") expect(env[:nfs_machine_ip]).to eq(["11.12.13.14"]) end + + it "allows statically configured guest IPs to co-exist with dynamic host only IPs for NFS" do + env[:machine].config.vm.network :private_network, ip: "11.12.13.14" + + subject.call(env) + + expect(env[:nfs_host_ip]).to eq("1.2.3.4") + expect(env[:nfs_machine_ip]).to eq(["11.12.13.14", "2.3.4.5"]) + end end end