toowoxx / terraform-provider-packer

Terraform Provider for HashiCorp Packer
Mozilla Public License 2.0
32 stars 7 forks source link

Missing environment variables #2

Closed furkantektas closed 2 years ago

furkantektas commented 2 years ago

Hello, I found this plugin quite useful, and easy to use. Thank you very much. I gave it a try on a packer script where I configure my docker container with an ansible provisioner. Although my current $PATH contains ansible-playbook executabe (ansible is installed in a conda environment), I got the following error:

│ * Error running "ansible-playbook --version": exec: "ansible-playbook":
│ executable file not found in $PATH
│ * user: Current requires cgo or $USER, $HOME set in environment
│ * user: could not determine current user from environment.

I assume the spawned process for building packer image is detached from the current environment.

Any idea how can I sort this out? Many thanks,

simaotwx commented 2 years ago

Hi there,

thanks for your feedback. Looks like your assumption is true. Packer gets an entirely new environment, not sharing the same one as the provider (Packer is embedded in the provider but the provider executable is started by the provider in "Packer mode"). You can set environment variables using the environment attribute which may already be enough to solve your issue. Regardless, I'll consider adding an attribute for passing through the current environment so that this situation is avoided.

We use the ansible-local provisioner which works fine with this plugin, perhaps that's worth a try:

  provisioner "ansible-local" {
    playbook_file = "playbook.yml"
    # In case you need extra arguments
    extra_arguments = [
      "--extra-vars", <<-EOT
        "foo=bar foo2=bar2" \
      EOT
    ]
  }

For this, ansible needs to be installed on the build host (docker container or VM that builds the image), though.

simaotwx commented 2 years ago

I added the attribute to v0.8.0. Feel free to reopen the issue in case it doesn't help with your issue.

furkantektas commented 2 years ago

Simão you are incredible! I was working on a workaround to fix that issue on the terraform-side, yet you have fixed the issue before I could even complete it. It's much more cleaner and maintainable now.

Unbelievable. I really appreciate your quick response and fix.

simaotwx commented 2 years ago

I'm happy it helped and I appreciate your kind feedback! :)