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
This commit is contained in:
Gilles Cornu 2015-12-02 08:37:41 +01:00
parent 77b11a989c
commit c49a146467
2 changed files with 16 additions and 1 deletions

View File

@ -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(" ")

View File

@ -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