Packer: Can't pass command line arguments to a shell script provisioner

Created on 4 Aug 2015  Â·  11Comments  Â·  Source: hashicorp/packer

When invoking a shell script provisioner (the variety in which a file is copied to the remote and then executed, rather than specifying a sequence of commands directly), command line options are not passed to the remote.

Setting environment variables can be used as a workaround to pass values, such as user variables, that one does not want to hard-code into a script before sending it to the remote.

bug provisioneshell

Most helpful comment

:+1: just ran into this. It's pretty useful for being able to pass external knowledge into the build

All 11 comments

Thanks for the report! I think I follow but for clarity, can you provide an example config that demonstrates this?

{
  "variables": {
    "provision_script": "provision.bash",
   "rpm_url": "http://some.host/some/file"
  }
  "builders": [{
    "type": "virtualbox-iso",
    "format": "ova",
    etc. etc. etc.
  }]
  "provisioners": [
    {
      "type": "shell",
      "script": "{{user `provision_script`}}",
      "environment_vars": [
        "RPMURL={{user `rpm_url`}}"
      ]
    }
  ]
}

There is no documented syntax to add command line arguments to the provision.bash script when it runs on the remote. Changing the "execute_command" doesn't seem to enable what I want.

The workarounds are to use environment variables as I've shown, or use a "file" provisioner to upload the script, then use an inline shell to invoke and delete the script.

So you actually want to run something like this?

"script":"{{ user `provision-script` }} {{ user `rpm_url` }}"

I'm not saying this should work now -- just clarifying the syntax you're expecting.

Either that, or something like
"script": ["{{user provision-script}}", "{{user rpm_url}}"]

or
"script": "{{user provision-script }}",
"args": ["{{user rpm_url }}"]

Backticks appearing in the proper places, of course...

Or something like this:

    "type": "shell",
    "execute_command": "echo 'centos'|sudo -S sh '{{.Path}}'",
    "override": {
        "virtualbox-iso": {
            "scripts": [
                "scripts/init.sh",
                "scripts/\"git_app.sh {{user `git_user_id`}} {{user `git_user_pw`}}\"",
                "scripts/cleanup.sh"
            ]
        }
    }

:+1: just ran into this. It's pretty useful for being able to pass external knowledge into the build

This should be working now with the inline parameter

@mwhooker Can you provide an example of how this is working with the inline parameter please? I have a shell script that I need to pass arguments to or pre-set environment variables for it to read. How would I do that with inline?

@wayneworkman taking from the above example, something like this should work.

{
  "variables": {
    "provision_script": "provision.bash",
   "rpm_url": "http://some.host/some/file"
  },
  "builders": [{
    "type": "virtualbox-iso",
    "format": "ova"
  }],
  "provisioners": [
    {
        "type": "file",
        "source": "myscript.sh",
        "destination": "/tmp/{{user `provision_script`}}"
    },
    {
      "type": "shell",
      "inline": [
        "chmod u+x /tmp/{{user `provision_script`}}",
        "/tmp/{{user `provision_script`}} --rpm-url {{user `rpm_url`}}"]
    }
  ]
}

the chmod line may not be necessary if you're using the ssh communicator and the local script is already executable.

We don't plan on modifying packer to allow you to pass arguments inline to the shell provisioner unless there's a reason this method won't work

@mwhooker yessir, it works exactly like that. Here's a snippet of what I did - the aws_account_nickname being a variable that I passed in.

 {
          "type": "shell",
          "execute_command": "chmod +x {{ .Path }}; sudo '{{ .Path }}' {{ user `aws_account_nickname` }}",
          "script": "/home/ec2-user/amis/scripts/provision.sh"
      }

Awesome!
On Mon, Apr 17, 2017 at 12:10 wayneworkman notifications@github.com wrote:

@mwhooker https://github.com/mwhooker yessir, it works exactly like
that. Here's a snippet of what I did - the aws_account_nickname being a
variable that I passed in.

{
"type": "shell",
"execute_command": "chmod +x {{ .Path }}; sudo '{{ .Path }}' {{ user aws_account_nickname }}",
"script": "/home/ec2-user/amis/scripts/provision.sh"
}

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/hashicorp/packer/issues/2552#issuecomment-294562822,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAA9AgLXuZ2p50vEN-EEnAsPekWvk47Oks5rw7kigaJpZM4Fk9a9
.

Was this page helpful?
0 / 5 - 0 ratings