How to pass vm_name to script-local provisioner?

89 views
Skip to first unread message

Andreas Sommer

unread,
Jan 26, 2018, 11:57:29 AM1/26/18
to Packer
{
"type": "shell-local",
"command": "env vm_name={{user `vm_name`}} ./packer/scripts/do-something-with-name.sh",
}

This won't work because it's not a user variable. Since I have several builders (different hypervisors to choose from), the name might differ between them. Is it possible somehow to pass the builder-specific vm_name value to a provisioner?

Best,
Andreas

Rickard von Essen

unread,
Jan 26, 2018, 12:06:25 PM1/26/18
to packe...@googlegroups.com

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/98cca778-50ff-448b-a662-6504fe4d5db2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andreas Sommer

unread,
Jan 26, 2018, 4:49:48 PM1/26/18
to Packer
That is for shell. For shell-local, those environment variables are undefined.


On Friday, January 26, 2018 at 6:06:25 PM UTC+1, Rickard von Essen wrote:
On Jan 26, 2018 17:57, "'Andreas Sommer' via Packer" <packe...@googlegroups.com> wrote:
{
"type": "shell-local",
"command": "env vm_name={{user `vm_name`}} ./packer/scripts/do-something-with-name.sh",
}

This won't work because it's not a user variable. Since I have several builders (different hypervisors to choose from), the name might differ between them. Is it possible somehow to pass the builder-specific vm_name value to a provisioner?

Best,
Andreas

--
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.

Gabo Kete

unread,
Jan 26, 2018, 5:04:08 PM1/26/18
to Packer
What I did is create a wrapper script that takes the name as an argument and then I create dynamically the JSON file passing this variable to the JSON file as a user Variable..

Something like

This is my usage Function from the wrapper script

usage() {
  echo -e "
Usage: $(basename $0) -i <IP ADDRESS> -n <SERVER NETMASK> -g <SERVER GATEWAY> -s <SERVER HOSTNAME> [ -d|-h|-v|-t <SERVER TYPE|-f <BULK SERVER LIST> ]

and This is where I dynamically create the JSON file 

create_json_template () {

cat > $_JSONFILE << __EOF
{
  "_comment": "This si a centos7-x64 Machine for VmWare ESXi 6.0",
  "variables": {
    "nameVm": "`echo $_HN`-centos-7.1-vmware",


Here is where I define the variable name from the options passed to the script

if [[ $# != 0 ]];then


while getopts ":i:n:g:s:t:f:dvh" Option
do
  case $Option in
i   ) _IP=$OPTARG;;
n   ) _NM=$OPTARG;;
      g   ) _GW=$OPTARG;;
      s   ) _HN=$OPTARG;;
Screen Shot 2018-01-27 at 10.58.48 AM.png
Screen Shot 2018-01-27 at 10.57.00 AM.png

Gabo Kete

unread,
Jan 26, 2018, 5:09:03 PM1/26/18
to Packer
what I suggest you can do within your wrapper script is to have 2 arguments defined 

1) The Builder type
2) An Extra argument added to the VM name based on the builder chosen by the user who executes the wrapper script

Something like 

 "nameVm": "`echo $_HN`-`echo $_BUILDTYPE`" 

so the final outcome will be a text like

ServerName1-AWS
ServerName1-VMWare
ServerName1-Azure

I guess this goes as far as the imagination and the scripts skills can go.. 

Rickard von Essen

unread,
Jan 27, 2018, 2:16:04 AM1/27/18
to packe...@googlegroups.com
Please feel free to open a feature request or a PR adding this to shell-local local. 

To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/9c5336f2-1111-4683-9d70-ea4b72b222d3%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages