From 5c50c50d73b3af11543d310ee69299338d50d69c Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Sat, 4 Mar 2017 19:49:28 -0500 Subject: [PATCH 1/3] Fix #8328: Use special quote and backslash formatting for Windows when setting Salt pillar. --- plugins/provisioners/salt/provisioner.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/plugins/provisioners/salt/provisioner.rb b/plugins/provisioners/salt/provisioner.rb index 7a6376fd1..8b69b7915 100644 --- a/plugins/provisioners/salt/provisioner.rb +++ b/plugins/provisioners/salt/provisioner.rb @@ -176,7 +176,19 @@ module VagrantPlugins ## Actions # Get pillar string to pass with the salt command def get_pillar - " pillar='#{@config.pillar_data.to_json}'" if !@config.pillar_data.empty? + if !@config.pillar_data.empty? + if @machine.config.vm.communicator == :winrm + # ' doesn't have any special behavior on the command prompt, + # so '{"x":"y"}' becomes '{x:y}' with literal single quotes. + # However, """ will become " , and \\""" will become \" . + # Use \\"" instead of \\""" for literal inner-value quotes + # to avoid issue with odd number of quotes. + # --% disables special PowerShell parsing on the rest of the line. + " --% pillar=#{@config.pillar_data.to_json.gsub(/(? Date: Sat, 4 Mar 2017 22:14:27 -0500 Subject: [PATCH 2/3] Specify Salt pillar dependency on PowerShell 3.0 due to --% syntax. --- website/source/docs/provisioning/salt.html.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/source/docs/provisioning/salt.html.md b/website/source/docs/provisioning/salt.html.md index ce0c90e74..680c8b73d 100644 --- a/website/source/docs/provisioning/salt.html.md +++ b/website/source/docs/provisioning/salt.html.md @@ -183,6 +183,8 @@ times. The data passed in should only be hashes and lists. Here is an example:: end ``` +On Windows guests, this requires PowerShell 3.0 or higher. + ## Preseeding Keys Preseeding keys is the recommended way to handle provisioning From 96a00ad692ed25733bf430885a2b56bb300c9fdf Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 17 Sep 2018 14:32:17 -0700 Subject: [PATCH 3/3] Include test for get_pillar salt function --- .../provisioners/salt/provisioner_test.rb | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/unit/plugins/provisioners/salt/provisioner_test.rb b/test/unit/plugins/provisioners/salt/provisioner_test.rb index e6bfece89..c47e2b854 100644 --- a/test/unit/plugins/provisioners/salt/provisioner_test.rb +++ b/test/unit/plugins/provisioners/salt/provisioner_test.rb @@ -50,6 +50,32 @@ describe VagrantPlugins::Salt::Provisioner do end end + describe "#get_pillar" do + context "windows" do + it "escapes pillar data for powershell and returns as json" do + allow(machine.config.vm).to receive(:communicator).and_return(:winrm) + allow(config).to receive(:pillar_data).and_return({"cat"=>"qubit"}) + + expect(subject.get_pillar).to eq(" --% pillar={\"\"\"cat\"\"\":\"\"\"qubit\"\"\"}") + end + end + + context "linux" do + it "returns pillar data as json" do + allow(machine.config.vm).to receive(:communicator).and_return(:false) + allow(config).to receive(:pillar_data).and_return({"cat"=>"shimi"}) + expect(subject.get_pillar).to eq(" pillar='{\"cat\":\"shimi\"}'") + end + end + + context "empty data" do + it "returns nothing if pillar data is empty" do + allow(config).to receive(:pillar_data).and_return({}) + expect(subject.get_pillar).to eq(nil) + end + end + end + describe "#call_highstate" do context "master" do it "passes along extra cli flags" do