Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passthrough of system env for local-exec provisioner #23193

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion builtin/provisioners/local-exec/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"runtime"
"strings"

"github.com/armon/circbuf"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -41,6 +42,10 @@ func Provisioner() terraform.ResourceProvisioner {
Type: schema.TypeMap,
Optional: true,
},
"system_environment": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
},

ApplyFunc: applyFn,
Expand All @@ -57,7 +62,18 @@ func applyFn(ctx context.Context) error {
}

// Execute the command with env
environment := data.Get("environment").(map[string]interface{})
environment := make(map[string]interface{})
// Start with the system env (if desired)
if data.Get("system_environment").(bool) {
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
environment[pair[0]] = pair[1]
}
}
// Apply the explicitly specified environment
for k, v := range data.Get("environment").(map[string]interface{}) {
environment[k] = v
}

var env []string
for k := range environment {
Expand Down
34 changes: 34 additions & 0 deletions builtin/provisioners/local-exec/resource_provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,37 @@ func TestResourceProvider_ApplyCustomEnv(t *testing.T) {
t.Errorf("wrong output\ngot: %s\nwant: %s", got, want)
}
}

func TestResourceProvider_ApplySystemEnv(t *testing.T) {
// Preserve original system env values (if any)
origFoo := os.Getenv("FOO")
defer os.Setenv("FOO", origFoo)
origBam := os.Getenv("BAM")
defer os.Setenv("FOO", origBam)

os.Setenv("FOO", "SystemFOO")
os.Setenv("BAM", "SystemBAM")

c := testConfig(t, map[string]interface{}{
"command": "echo $FOO $BAR $BAZ $BAM",
"system_env": true,
"environment": map[string]interface{}{
"FOO": "BAR",
"BAR": 1,
"BAZ": "true",
},
})

output := new(terraform.MockUIOutput)
p := Provisioner()

if err := p.Apply(output, nil, c); err != nil {
t.Fatalf("err: %v", err)
}

got := strings.TrimSpace(output.OutputMessage)
want := "BAR 1 true SystemBAM"
if got != want {
t.Errorf("wrong output\ngot: %s\nwant: %s", got, want)
}
}
5 changes: 5 additions & 0 deletions website/docs/provisioners/local-exec.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ The following arguments are supported:
* `environment` - (Optional) block of key value pairs representing the
environment of the executed command. inherits the current process environment.

* `system_environment` - (Optional) If true, the environment of the terraform
process will be passed through to the executed command. Variables explicitly specified
in `environment` will take precedence over any variables inherited from the system
environment.

### Interpreter Examples

```hcl
Expand Down