How do I use custom variables with file backing in Packer?

I have a json wrapper like:

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

      

and myfile.json:

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

      

Variable in file being replaced, is sed replacing by shell provisioning tool after the file is only available here?

Using packer version 0.12.0

+3


source to share


2 answers


You must pass them as environment variables. For example:

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

      



And in your script you can use $http_proxy

+5


source


Custom variables must first be defined in the variables section in your template. Even if you want the default user variable to have an empty string, it must be defined. This clarity helps reduce the time it takes for newbies to understand what can be changed with variables in your template.

The variable section is a key / value mapping of a user variable name to a default value. The default can be an empty string. An example is shown below:



 {
 "variables": {
"aws_access_key": "",
 "aws_secret_key": ""
 },

"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
// ...
}]
}

      

check this link for more info

-1


source







All Articles