Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass argument in packer provision script?

Tags:

packer

I am struggling to pass input parameter to packer provisioning script. I have tried various options but no joy.

Objective is my provision.sh should accept input parameter which I send during packer build.

packer build -var role=abc test.json

I am able to get the user variable in json file however I am unable to pass it provision script. I have to make a decision based on the input parameter.

I tried something like

"provisioners": 
{
  "type": "shell",
  "scripts": [
  "provision.sh {{user `role`}}"
 ]
}

But packer validation itself is failed with no such file/directory error message.

It would be real help if someone can help me on this.

Thanks in advance.

like image 905
Thayz Avatar asked Dec 01 '17 15:12

Thayz


People also ask

What is Provisioner in Packer?

Packer Provisioners are the components of Packer that install and configure software into a running machine prior to turning that machine into an image. An example of a provisioner is the shell provisioner, which runs shell scripts within the machines.

What is local provisioning in shell script?

Local Shell Provisioner. shell-local will run a shell script of your choosing on the machine where Packer is being run - in other words, shell-local will run the shell script on your build server, or your desktop, etc., rather than the remote/guest machine being provisioned by Packer.

What is Shell provisioning?

The shell Packer provisioner provisions machines built by Packer using shell scripts. Shell provisioning is the easiest way to get software installed and configured on a machine. Building Windows images? You probably want to use the PowerShell or Windows Shell provisioners.


2 Answers

You should use the environment_vars option, see the docs Shell Provisioner - environment_vars.

Example:

"provisioners": [
  {
    "type": "shell"
    "environment_vars": [
      "HOSTNAME={{user `vm_name`}}",
      "FOO=bar"
    ],
    "scripts": [
      "provision.sh"
    ],
  }
 ]
like image 112
Rickard von Essen Avatar answered Nov 12 '22 06:11

Rickard von Essen


If your script is already configured to use arguments; you simply need to run it as inline rather than in the scripts array.

In order to do this, the script must exist on the system already - you can accomplish this by copying it to the system with the file provisioner.

  "provisioners": [
    {
      "type":        "file",
      "source":      "scripts/provision.sh",
      "destination": "/tmp/provision.sh"
    },
    {
      "type": "shell",
      "inline": [
        "chmod u+x /tmp/provision.sh",
        "/tmp/provision.sh {{user `vm_name`}}"]
    }
  ]
like image 37
TJ Biddle Avatar answered Nov 12 '22 07:11

TJ Biddle