provisioners/ansible(both): Quote host_vars if needed

This patch is based on @subimage's inputs in the related GitHub issue.
Thanks again!

Fix #8597
This commit is contained in:
Gilles Cornu 2017-09-06 21:22:41 +02:00
parent 6673c14916
commit ac75e409a3
No known key found for this signature in database
GPG Key ID: F6BC2CF7E1FE8FFF
4 changed files with 15 additions and 4 deletions

View File

@ -13,6 +13,7 @@ BUG FIXES:
- guests/shell_expand_guest_path : Properly expand guest paths that include relative path alias [GH-8918]
- hosts/linux: Remove duplicate export folders before writing /etc/exports [GH-8945]
- provisioners/ansible(both): Add single quotes to the inventory host variables, only when necessary [GH-8597]
- provisioners/ansible(both): Add the "all:vars" section to the inventory when defined in `groups` option [GH-7730]
- provisioners/ansible_local: Extra variables are no longer truncated when a dollar ($) character is present [GH-7735]
- provisioners/file: Align file provisioner functionality on all platforms [GH-8939]

View File

@ -148,7 +148,13 @@ module VagrantPlugins
end
s = nil
if vars.is_a?(Hash)
s = vars.each.collect{ |k, v| "#{k}=#{v}" }.join(" ")
s = vars.each.collect {
|k, v|
if v.is_a?(String) && v.include?(' ') && !v.match(/^('|")[^'"]+('|")$/)
v = %Q('#{v}')
end
"#{k}=#{v}"
}.join(" ")
elsif vars.is_a?(Array)
s = vars.join(" ")
elsif vars.is_a?(String)

View File

@ -291,11 +291,15 @@ VF
it "adds host variables (given in Hash format) to the generated inventory" do
config.host_vars = {
machine1: {"http_port" => 80, "comments" => "'some text with spaces'"}
machine1: {
"http_port" => 80,
"comments" => "'some text with spaces and quotes'",
"description" => "text with spaces but no quotes",
}
}
expect(Vagrant::Util::Subprocess).to receive(:execute).with(any_args) {
inventory_content = File.read(generated_inventory_file)
expect(inventory_content).to match("^" + Regexp.quote(machine.name) + ".+http_port=80 comments='some text with spaces'$")
expect(inventory_content).to match("^" + Regexp.quote(machine.name) + ".+http_port=80 comments='some text with spaces and quotes' description='text with spaces but no quotes'")
}.and_return(default_execute_result)
end

View File

@ -93,7 +93,7 @@ Some of these options are for advanced usage only and should not be used unless
ansible.host_vars = {
"host1" => {"http_port" => 80,
"maxRequestsPerChild" => 808},
"comments" => "'text with spaces'",
"comments" => "text with spaces",
"host2" => {"http_port" => 303,
"maxRequestsPerChild" => 909}
}