diff --git a/lib/vagrant/action/builtin/set_hostname.rb b/lib/vagrant/action/builtin/set_hostname.rb index fac86c60c..eb87720af 100644 --- a/lib/vagrant/action/builtin/set_hostname.rb +++ b/lib/vagrant/action/builtin/set_hostname.rb @@ -17,12 +17,12 @@ module Vagrant @app.call(env) hostname = env[:machine].config.vm.hostname - disable_hosts_modification = env[:machine].config.vm.disable_hosts_modification - if !hostname.nil? && !disable_hosts_modification + allow_hosts_modification = env[:machine].config.vm.allow_hosts_modification + if !hostname.nil? && allow_hosts_modification env[:ui].info I18n.t("vagrant.actions.vm.hostname.setting") env[:machine].guest.capability(:change_host_name, hostname) else - @logger.info("`disable_hosts_modification` set to true. Hosts modification has been disabled, skiping changing hostname.") + @logger.info("`allow_hosts_modification` set to false. Hosts modification has been disabled, skiping changing hostname.") end end end diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index 990b0b1b8..f5c870f58 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -24,6 +24,7 @@ module VagrantPlugins attr_accessor :allowed_synced_folder_types attr_accessor :allow_fstab_modification + attr_accessor :allow_hosts_modification attr_accessor :base_mac attr_accessor :base_address attr_accessor :boot_timeout @@ -42,7 +43,6 @@ module VagrantPlugins attr_accessor :box_download_location_trusted attr_accessor :box_download_options attr_accessor :communicator - attr_accessor :disable_hosts_modification attr_accessor :graceful_halt_timeout attr_accessor :guest attr_accessor :hostname @@ -78,7 +78,7 @@ module VagrantPlugins @box_extra_download_options = UNSET_VALUE @box_url = UNSET_VALUE @box_version = UNSET_VALUE - @disable_hosts_modification = UNSET_VALUE + @allow_hosts_modification = UNSET_VALUE @clone = UNSET_VALUE @communicator = UNSET_VALUE @graceful_halt_timeout = UNSET_VALUE @@ -529,7 +529,7 @@ module VagrantPlugins @box_version = nil if @box_version == UNSET_VALUE @box_download_options = {} if @box_download_options == UNSET_VALUE @box_extra_download_options = Vagrant::Util::MapCommandOptions.map_to_command_options(@box_download_options) - @disable_hosts_modification = false if @disable_hosts_modification == UNSET_VALUE + @allow_hosts_modification = true if @allow_hosts_modification == UNSET_VALUE @clone = nil if @clone == UNSET_VALUE @communicator = nil if @communicator == UNSET_VALUE @graceful_halt_timeout = 60 if @graceful_halt_timeout == UNSET_VALUE @@ -997,9 +997,12 @@ module VagrantPlugins if ![TrueClass, FalseClass].include?(@allow_fstab_modification.class) errors["vm"] << I18n.t("vagrant.config.vm.config_type", option: "allow_fstab_modification", given: @allow_fstab_modification.class, required: "Boolean" - if ![TrueClass, FalseClass].include?(@disable_hosts_modification.class) - errors["vm"] << I18n.t("vagrant.config.vm.disable_host_modification_type", - given: @disable_hosts_modification.class + ) + end + + if ![TrueClass, FalseClass].include?(@allow_hosts_modification.class) + errors["vm"] << I18n.t("vagrant.config.vm.config_type", + option: "allow_hosts_modification", given: @allow_hosts_modification.class, required: "Boolean" ) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index a5aad61f2..47ee108b5 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1941,8 +1941,6 @@ en: Checksum type specified but "box_download_checksum" is blank box_download_checksum_notblank: |- Checksum specified but must also specify "box_download_checksum_type" - disable_host_modification_type: |- - Expected configuration `disable_hosts_modification` to be type Bool, got %{given} box_missing: "A box must be specified." box_download_options_type: |- Found "box_download_options" specified as type '%{type}', should be a Hash diff --git a/test/unit/plugins/kernel_v2/config/vm_test.rb b/test/unit/plugins/kernel_v2/config/vm_test.rb index 709fd42f4..39fed95df 100644 --- a/test/unit/plugins/kernel_v2/config/vm_test.rb +++ b/test/unit/plugins/kernel_v2/config/vm_test.rb @@ -50,15 +50,15 @@ describe VagrantPlugins::Kernel_V2::VMConfig do end it "validates disables_host_modification option" do - subject.disable_hosts_modification = true + subject.allow_hosts_modification = true subject.finalize! assert_valid - subject.disable_hosts_modification = false + subject.allow_hosts_modification = false subject.finalize! assert_valid - subject.disable_hosts_modification = "truthy" + subject.allow_hosts_modification = "truthy" subject.finalize! assert_invalid end diff --git a/test/unit/vagrant/action/builtin/set_hostname_test.rb b/test/unit/vagrant/action/builtin/set_hostname_test.rb index f11d141a3..3f2d5e16b 100644 --- a/test/unit/vagrant/action/builtin/set_hostname_test.rb +++ b/test/unit/vagrant/action/builtin/set_hostname_test.rb @@ -15,13 +15,13 @@ describe Vagrant::Action::Builtin::SetHostname do end it "should change hostname if hosts modification enabled" do - allow(machine).to receive_message_chain(:config, :vm, :disable_hosts_modification).and_return(false) + allow(machine).to receive_message_chain(:config, :vm, :allow_hosts_modification).and_return(true) expect(machine).to receive(:guest) subject.call(env) end it "should not change hostname if hosts modification disabled" do - allow(machine).to receive_message_chain(:config, :vm, :disable_hosts_modification).and_return(true) + allow(machine).to receive_message_chain(:config, :vm, :allow_hosts_modification).and_return(false) expect(machine).not_to receive(:guest) subject.call(env) end diff --git a/website/pages/docs/vagrantfile/machine_settings.mdx b/website/pages/docs/vagrantfile/machine_settings.mdx index 6d7cd4bdd..f151c786c 100644 --- a/website/pages/docs/vagrantfile/machine_settings.mdx +++ b/website/pages/docs/vagrantfile/machine_settings.mdx @@ -21,6 +21,9 @@ machine that Vagrant manages. by Vagrant. Note, this may mean that folders will not be automatically mounted on machine reboot. Defaults to true. +- `config.vm.allow_hosts_modification` (boolean) - If false, will prevent Vagrant + from writing to `/etc/hosts`. Defaults to true. + - `config.vm.base_mac` (string) - The MAC address to be assigned to the default NAT interface on the guest. _Support for this option is provider dependent._