From c49a146467f7d7a0e441564a2418a1b8a42bb8a6 Mon Sep 17 00:00:00 2001 From: Gilles Cornu Date: Wed, 2 Dec 2015 08:37:41 +0100 Subject: [PATCH] provisioners/ansible(both): alias String-to-Symbol String and Symbol types are different when used as a Hash key. By default the Vagrant machine names are set in Symbol format, but users may write their `host_vars` entries with String keys. This is a very simple way to ensure smooth experience, without having to coerce the data types during the config validation (e.g. with a library like Hashie, which is currently not in the Vagrant dependencies) See also: - https://bugs.ruby-lang.org/issues/5964#note-17 - https://github.com/intridea/hashie#keyconversion --- plugins/provisioners/ansible/provisioner/base.rb | 7 ++++++- .../plugins/provisioners/ansible/provisioner_test.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/plugins/provisioners/ansible/provisioner/base.rb b/plugins/provisioners/ansible/provisioner/base.rb index 51377a61f..981cbf5d1 100644 --- a/plugins/provisioners/ansible/provisioner/base.rb +++ b/plugins/provisioners/ansible/provisioner/base.rb @@ -71,7 +71,12 @@ module VagrantPlugins end def get_inventory_host_vars_string(machine_name) - vars = config.host_vars[machine_name] + # In Ruby, Symbol and String values are different, but + # Vagrant has to unify them for better user experience. + vars = config.host_vars[machine_name.to_sym] + if !vars + vars = config.host_vars[machine_name.to_s] + end s = nil if vars.is_a?(Hash) s = vars.each.collect{ |k, v| "#{k}=#{v}" }.join(" ") diff --git a/test/unit/plugins/provisioners/ansible/provisioner_test.rb b/test/unit/plugins/provisioners/ansible/provisioner_test.rb index cd76d68f8..4a352a29e 100644 --- a/test/unit/plugins/provisioners/ansible/provisioner_test.rb +++ b/test/unit/plugins/provisioners/ansible/provisioner_test.rb @@ -246,6 +246,16 @@ VF expect(inventory_content).to match("^" + Regexp.quote(machine.name) + ".+http_port=80 maxRequestsPerChild=808") } end + + it "retrieves the host variables by machine name, also in String format" do + config.host_vars = { + "machine1" => "http_port=80 maxRequestsPerChild=808" + } + expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args| + inventory_content = File.read(generated_inventory_file) + expect(inventory_content).to match("^" + Regexp.quote(machine.name) + ".+http_port=80 maxRequestsPerChild=808") + } + end end describe "with groups option" do