diff --git a/plugins/provisioners/chef/provisioner/base.rb b/plugins/provisioners/chef/provisioner/base.rb index 1ae4a0808..3f5ec7106 100644 --- a/plugins/provisioners/chef/provisioner/base.rb +++ b/plugins/provisioners/chef/provisioner/base.rb @@ -24,18 +24,27 @@ module VagrantPlugins @logger = Log4r::Logger.new("vagrant::provisioners::chef") if !present?(@config.node_name) - cache = @machine.data_dir.join("chef_node_name") - - if !cache.exist? - @machine.ui.info I18n.t("vagrant.provisioners.chef.generating_node_name") - cache.open("w+") do |f| - f.write("vagrant-#{SecureRandom.hex(4)}") + # First attempt to get the node name from the hostname, and if that + # is not present, generate/retrieve a random hostname. + hostname = @machine.config.vm.hostname + if present?(hostname) + @machine.ui.info I18n.t("vagrant.provisioners.chef.using_hostname_node_name", + hostname: hostname, + ) + @config.node_name = hostname + else + cache = @machine.data_dir.join("chef_node_name") + if !cache.exist? + @machine.ui.info I18n.t("vagrant.provisioners.chef.generating_node_name") + cache.open("w+") do |f| + f.write("vagrant-#{SecureRandom.hex(4)}") + end end - end - if cache.file? - @logger.info("Loading cached node_name...") - @config.node_name = cache.read.strip + if cache.file? + @logger.info("Loading cached node_name...") + @config.node_name = cache.read.strip + end end end end diff --git a/plugins/provisioners/chef/provisioner/chef_client.rb b/plugins/provisioners/chef/provisioner/chef_client.rb index 5a9ec918e..1d7dc5420 100644 --- a/plugins/provisioners/chef/provisioner/chef_client.rb +++ b/plugins/provisioners/chef/provisioner/chef_client.rb @@ -133,7 +133,7 @@ module VagrantPlugins end def delete_from_chef_server(deletable) - node_name = @config.node_name || @machine.config.vm.hostname + node_name = @config.node_name if !present?(node_name) @machine.ui.warn(I18n.t("vagrant.provisioners.chef.missing_node_name", diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 3ca78008d..4929d9a38 100755 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1950,6 +1950,8 @@ en: client_key_folder: "Creating folder to hold client key..." generating_node_name: |- Auto-generating node name for Chef... + using_hostname_node_name: |- + Using hostname "%{hostname}" as node name for Chef... install_failed: |- Vagrant could not detect Chef on the guest! Even after Vagrant attempted to install Chef, it could still not find Chef on the system. diff --git a/test/unit/plugins/provisioners/chef/provisioner/base_test.rb b/test/unit/plugins/provisioners/chef/provisioner/base_test.rb index 033051b1b..10536f035 100644 --- a/test/unit/plugins/provisioners/chef/provisioner/base_test.rb +++ b/test/unit/plugins/provisioners/chef/provisioner/base_test.rb @@ -22,6 +22,35 @@ describe VagrantPlugins::Chef::Provisioner::Base do allow(config).to receive(:node_name=) end + describe "#node_name" do + let(:env) { double("env") } + let(:root_path) { "/my/root" } + + before do + allow(machine).to receive(:env).and_return(env) + allow(env).to receive(:root_path).and_return(root_path) + end + + it "defaults to node_name if given" do + config = OpenStruct.new(node_name: "name") + instance = described_class.new(machine, config) + expect(instance.config.node_name).to eq("name") + end + + it "defaults to hostname if given" do + machine.config.vm.hostname = "by.hostname" + instance = described_class.new(machine, OpenStruct.new) + expect(instance.config.node_name).to eq("by.hostname") + end + + it "generates a random name if no hostname or node_name is given" do + config = OpenStruct.new(node_name: nil) + machine.config.vm.hostname = nil + instance = described_class.new(machine, OpenStruct.new) + expect(instance.config.node_name).to match(/vagrant\-.+/) + end + end + describe "#encrypted_data_bag_secret_key_path" do let(:env) { double("env") } let(:root_path) { "/my/root" }