Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use user variables with file provisioner in Packer?

Tags:

packer

I have a packer json like:

"builders": [{...}],
"provisioners": [
          {
                "type": "file",
                "source": "packer/myfile.json",
                "destination": "/tmp/myfile.json"
           }
  ],
"variables": {
        "myvariablename": "value"
 }

and myfile.json is:

{
   "var" : "{{ user `myvariablename`}}"
}

The variable into the file does get replaced, is a sed replacement with shell provisioner after the file the only option available here?

Using packer version 0.12.0

like image 438
MarMan Avatar asked Jun 21 '17 10:06

MarMan


People also ask

What is Provisioner in Packer?

Provisioners use built-in and third-party software to install and configure the machine image after booting. Provisioners prepare the system, so you may want to use them for the following use cases: installing packages. patching the kernel. creating users.

How can we make sure the sensitive variable are not printed in the Packer logs?

You can make sure that sensitive variables won't get printed to the logs by adding them to the "sensitive-variables" list within the Packer template: { "variables": { "my_secret": "{{env `MY_SECRET`}}", "not_a_secret": "plaintext", "foo": "bar" }, "sensitive-variables": ["my_secret", "foo"], ... }

Can input variables be called via environment variables?

Additionally, input variable values can also be set using Terraform environment variables. To do so, simply set the environment variable in the format TF_VAR_<variable name> . The variable name part of the format is the same as the variables declared in the variables.tf file.

What user does Packer run as?

So Packer is running as root but the ansible user is not root, and needs access to environment variables? You can use the option ansible_env_vars to inject specific environment variables into the ansible environment.


2 Answers

So far I've come just with the solution to use file & shell provisioner. Upload file and then replace variables in file via shell provisioner which can be fed from template variables provided by e.g. HashiCorp Vault

like image 63
aRagornol Avatar answered Sep 30 '22 18:09

aRagornol


You have to pass these as environment variables. For example:

  "provisioners": [
    {
      "type": "shell"
      "environment_vars": [
        "http_proxy={{user `proxy`}}",
      ],
      "scripts": [
        "some_script.sh"
      ],
    }
  ],
  "variables": {
    "proxy": null
  }

And in the script you can use $http_proxy

like image 20
Rickard von Essen Avatar answered Sep 30 '22 18:09

Rickard von Essen