diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 1dff85c24..27d9d1ce7 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -170,20 +170,17 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } // Do a check for bad environment variables, such as '=foo', 'foobar' - for _, kv := range p.config.Vars { + for idx, kv := range p.config.Vars { vs := strings.SplitN(kv, "=", 2) if len(vs) != 2 || vs[0] == "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("Environment variable not in format 'key=value': %s", kv)) + } else { + // Single quote env var values + p.config.Vars[idx] = fmt.Sprintf("%s='%s'", vs[0], vs[1]) } } - // Single quote env var values - for key := range p.config.Vars { - vs := strings.SplitN(p.config.Vars[key], "=", 2) - p.config.Vars[key] = fmt.Sprintf("%s='%s'", vs[0], vs[1]) - } - if p.config.RawStartRetryTimeout != "" { p.config.startRetryTimeout, err = time.ParseDuration(p.config.RawStartRetryTimeout) if err != nil { diff --git a/provisioner/shell/provisioner_test.go b/provisioner/shell/provisioner_test.go index ab5308bd2..4fd5a5cad 100644 --- a/provisioner/shell/provisioner_test.go +++ b/provisioner/shell/provisioner_test.go @@ -199,3 +199,21 @@ func TestProvisionerPrepare_EnvironmentVars(t *testing.T) { t.Fatalf("should not have error: %s", err) } } + +func TestProvisionerQuote_EnvironmentVars(t *testing.T) { + config := testConfig() + + config["environment_vars"] = []string{"keyone=valueone", "keytwo=value\ntwo"} + p := new(Provisioner) + p.Prepare(config) + + expectedValue := "keyone='valueone'" + if p.config.Vars[0] != expectedValue { + t.Fatalf("%s should be equal to %s", p.config.Vars[0], expectedValue) + } + + expectedValue = "keytwo='value\ntwo'" + if p.config.Vars[1] != expectedValue { + t.Fatalf("%s should be equal to %s", p.config.Vars[1], expectedValue) + } +}