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

Injecting environment_vars does not work when overriding execute_command #12687

Closed
tobiassoppa opened this issue Nov 8, 2023 · 2 comments
Closed

Comments

@tobiassoppa
Copy link

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

When filing a bug, please include the following headings if possible. Any
example text in this template can be deleted.

Overview of the Issue

When executing a shell with admin rights and overwriting the execute_command to do so, the defined environment_vars are no longer getting injected into the shell, even though {{ .Vars }} are passed.

Reproduction Steps

Execute packer build . in a directory in which a Packer file similar to the simplified one below is present, together with the provided shell script.

Packer version

$ packer version
Packer v1.9.4

Simplified Packer Template

#!/bin/bash

# ./scripts/hello_world.sh

if [ "$(id -u)" -ne 0 ]; then
    echo "Must run as root"
    exit 1
fi

echo "Hello, $WORLD!"

# Expected output: Hello, World!
# Actual output: Hello, !
packer {
  required_plugins {
    amazon = {
      source  = "github.com/hashicorp/amazon"
      version = "~> 1"
    }
  }
}

source "amazon-ebs" "example" {
  ami_name      = "ami_which_uses_amazon_linux_2"
  instance_type = "t2.micro"
  region        = "us-west-2"
  source_ami_filter {
    filters = {
      "tag_key" = "unique_tag_value"
    }
    most_recent = true
    owners      = ["self"]
  }
  ssh_interface        = "session_manager"
  iam_instance_profile = "a_profile_that_we_use"
  ssh_username         = "ec2-user"
}

build {
  name = "packer-example"
  sources = [
    "source.amazon-ebs.example"
  ]

  provisioner "shell" {
    environment_vars  = [
        "WORLD=World"
    ]
    script            = "./scripts/hello_world.sh"
    execute_command   = "{{ .Vars }} sudo bash {{ .Path }}"
  }
}

Operating system and Environment details

OSX Sonoma 14.1

$ aws --version
aws-cli/2.11.6 Python/3.11.2 Darwin/23.1.0 exe/x86_64 prompt/off

$ session-manager-plugin --version                                       
1.2.497.0
@nywilken
Copy link
Contributor

Hi @tobiassoppa it looks like you are passing in the environment variables but they are being set before you sudo so the root user will not see the environment variables. What you want to do is update the execute_command so that the sudo user is the one setting the environment variables.

    execute_command   = "sudo {{ .Vars }}  bash {{ .Path }}"

I believe the script has the executable flag set after it gets uploaded so you can make the execute_command a bit clearer by updating it to

    execute_command   = "sudo bash -c '{{ .Vars }} {{ .Path }}'"

I'm going to close this issue as it appears to be a user configuration issue. If you find that you are still running into issues after making the update or believe that this is still a bug please feel free to drop a comment.

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants