Shell provisioner - call shell scripts with arguments

2,676 views
Skip to first unread message

Larry Liang

unread,
Jan 3, 2016, 10:44:10 PM1/3/16
to Packer
Hi,

I'm trying to invoke a shell script (install.sh) with some arguments (say the version of mongodb)

For example "script": "install.sh '2.4.14'", using "script" or "scripts". 

Not sure if this is possible with the latest version of Packer.

If not, can someone recommend a acceptable work-around. I'm thinking about maybe export some env vars, but it's kind of implicit and not very elegant and readable.

Cheers,

Larry

Alvaro Miranda Aguilera

unread,
Jan 5, 2016, 3:33:57 AM1/5/16
to packe...@googlegroups.com
Hello,

Happy  new year.

Yes, its possible, I will show you 3 ways, but I am sure are more.

You have 2 ways to run an script, one is inline and other is path. Path will copy the file and run it.
Inline will run the commands.

Path A. Feels natural:
So use file provisioner to copy the script.
use shell provisioner inline to run the script.


  "provisioners"             : [

{

    "type": "file",

    "source": "myawesomescript.sh",

    "destination": "/root"

},

{

    "type": "shell",

    "execute_command": "sh '{{ .Path }}'",

    "inline": [

       "bash /root/myawesomescript.sh arg1"

       ]

  }

],


Just make sure you are copying to a directory that does exist.

Do you see execute_command? is a powerful option.


Path B feels hacky, but 100% valid


  "provisioners"             : [

    {

      "type": "shell",

      "execute_command": "echo 'vagrant'| {{ .Vars }} sudo -E -S bash '{{.Path}}' arg1",

      "pause_before": "1s",

      "scripts": [

        "scripts/vagrant.sh"

      ]

    }]


Path C

OR, you could use:

create a variable arguments

  "variables": {

    "arguments": "arg1"

   }


And use

      "execute_command": "echo 'vagrant'| {{ .Vars }} sudo -E -S bash '{{.Path}}'  '{{user `arguments`}}'   " ,


Advantage of this one, is you can override that at run time.


packer build -var "argument=the_new_argument" template.json

Hope this helps.

Alvaro.



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/0dc27455-8fba-41eb-af4c-8994e69e5ea7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages