From ac75e409a3470897d56a0841a575e981d60e2e3d Mon Sep 17 00:00:00 2001 From: Gilles Cornu Date: Wed, 6 Sep 2017 21:22:41 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + plugins/provisioners/ansible/provisioner/base.rb | 8 +++++++- .../unit/plugins/provisioners/ansible/provisioner_test.rb | 8 ++++++-- website/source/docs/provisioning/ansible_common.html.md | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f4c4b648..8702b05fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/plugins/provisioners/ansible/provisioner/base.rb b/plugins/provisioners/ansible/provisioner/base.rb index c2add455a..97bc04852 100644 --- a/plugins/provisioners/ansible/provisioner/base.rb +++ b/plugins/provisioners/ansible/provisioner/base.rb @@ -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) diff --git a/test/unit/plugins/provisioners/ansible/provisioner_test.rb b/test/unit/plugins/provisioners/ansible/provisioner_test.rb index 861a4e1c6..c6b2d795c 100644 --- a/test/unit/plugins/provisioners/ansible/provisioner_test.rb +++ b/test/unit/plugins/provisioners/ansible/provisioner_test.rb @@ -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 diff --git a/website/source/docs/provisioning/ansible_common.html.md b/website/source/docs/provisioning/ansible_common.html.md index f6fb84f07..a528f1a05 100644 --- a/website/source/docs/provisioning/ansible_common.html.md +++ b/website/source/docs/provisioning/ansible_common.html.md @@ -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} }