passing command line arguments to packer fails with "No such file or directory"

1,071 views
Skip to first unread message

Bubunia Patra

unread,
Mar 5, 2018, 6:33:14 AM3/5/18
to Packer
Hi all,

I want to pass some command arguments to the provisioning script in packer for creating an AMI image.  But I am getting "No such file or directory". Can anyone help me in this regard?
I also having issue when I have multiline strings being passed to packer. I checked the below thread and its somehow adding \n doesnt work.


Can anyone help me in this regard?

Regards
Pradeep

{
  "variables": {
    "aws_access_key": "{{env `PACKER_AWS_ACCESS_KEY`}}",
    "aws_secret_key": "{{env `PACKER_AWS_SECRET_KEY`}}",
    "aws_region": "{{env `PACKER_AWS_REGION`}}",
    "prod_name": "abcd",
    "image_name": "abc",
    "version_number": "v1",
    "ami_filter": "CentOS Linux 7 x86_64 HVM EBS 1704_01-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-d52f5bc3.4",
    "ssh_user_name": "centos",
    "env_type": "test",
    "provision_script": "/home/centos/packer/provision.sh",
    "arg1" : "hello",
    "arg2" : "world"

   "builders": [
    {
                "name": "packer_eu_west_ebs",
                "type": "amazon-ebs",
                "access_key": "{{user `aws_access_key`}}",
                "secret_key": "{{user `aws_secret_key`}}",
                "region": "{{user `aws_region`}}",
                "source_ami_filter": {
                        "filters": {
                          "name": "{{user `ami_filter`}}"
                        },
                        "most_recent": true
                },
                "instance_type": "t2.small"
          }
        ]


   "provisioners": [
    {
      "type": "shell",
          "execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'",
           "inline": [
                      "{{user `provision_script`}} {{user `arg1`}} {{user `arg2`}}"


            ]
          }
       ]
    }


Error:


==> packer_eu_west_ebs: Pausing after run of step 'StepRunSourceInstance'. Press enter to continue. ==> packer_eu_west_ebs: Pausing after run of step 'StepTagEBSVolumes'. Press enter to continue. ==> packer_eu_west_ebs: Pausing after run of step 'StepGetPassword'. Press enter to continue.
==> packer_eu_west_ebs: Waiting for SSH to become available...
==> packer_eu_west_ebs: Connected to SSH!
==> packer_eu_west_ebs: Pausing after run of step 'StepConnect'. Press enter to continue.
==> packer_eu_west_ebs: Provisioning with shell script: /tmp/packer-shell705621081
    packer_eu_west_ebs: /tmp/script_619.sh: line 2: /home/centos/packer/provision.sh: No such file or directory

Bubunia Patra

unread,
Mar 5, 2018, 7:41:28 AM3/5/18
to Packer
Please note that the file exists (ls -la </home/centos/packer/provision.sh>) and it has execute permission as well.

Bubunia Patra

unread,
Mar 5, 2018, 8:24:01 AM3/5/18
to Packer
I fixed the issue myself. For some reason the inline thing doesnt work. So used scripts with arguments and the "No such file or directory" problem is gone.

Rickard von Essen

unread,
Mar 5, 2018, 8:36:10 AM3/5/18
to packe...@googlegroups.com
You reference a script in "inline" that is on your host, of course the guest don't have that script. When you use "scripts" packer automatically upload the script from the host to the guest before it executes it. That why it works. Usually you use inline to specify the command you want to run, not to run another script that you have on the host. If you would like to do that for some special reason you should first use the "file" provisioner to upload the script to the guest.

--
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/58a71ce8-a7b7-4c7f-a05f-9d188d13418f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Bubunia Patra

unread,
Mar 5, 2018, 8:38:04 AM3/5/18
to Packer
Packer version I am using 1.0.2. But the problem is that  arguments are not getting passed to the provisioning script and all are null. I used the second method(hack ) that is described in below thread using  "scripts" tag.




   "provisioners": [
    {
      "type": "shell",
          "execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}} {{user `arg1`}} '",
           "inline": [
                      "{{user `provision_script`}} {{user `arg1`}} {{user `arg2`}}"


            ]
          }
       ]
    }

On Monday, March 5, 2018 at 7:33:14 PM UTC+8, Bubunia Patra wrote:

Rickard von Essen

unread,
Mar 5, 2018, 8:43:16 AM3/5/18
to packe...@googlegroups.com
arguments should be in the "execute_command"

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

Bubunia Patra

unread,
Mar 5, 2018, 9:50:11 AM3/5/18
to Packer
1. "execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}' '{{user `arg1`}}' '{{user `arg2`}}' "  Is this correct? I tried this but since i have lot of arguments to be passed it is less readable and error prone.


 2.The problem i am facing is I have lot of variables to be passed as arguments and when I run as below:
"execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}' '{{user `arg1`}}' '{{user `arg2`}}' \n
                                   '{{user `arg3`}}'   '{{user `arg4`}}' '{{user `arg5`}}' \n

Failed to parse template: Error parsing JSON: invalid character '\n' in string literal. Could you please help how I can pass multiline arguments?
                                    " 

To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.

Rickard von Essen

unread,
Mar 5, 2018, 10:25:00 AM3/5/18
to packe...@googlegroups.com
You can't have \n in a Json string and I don't understand why you would need one it that example code? I recommend that you pass any invariants as environment variables 1) instead since that is more generic. An example is how it's done in bento 2)




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/6d306445-6b20-4177-95fd-1abade411e9e%40googlegroups.com.

Bubunia Patra

unread,
Mar 5, 2018, 12:35:41 PM3/5/18
to Packer
Thanks for your quick help. But I am not able to retrieve the values from the provisional scripts. I guess the scope is getting lost. I was assuming the environment variables gives this advantage. I did the similar as is done in bento(2). Any idea what would have went wrong?

Alvaro Miranda Aguilera

unread,
Mar 5, 2018, 1:32:41 PM3/5/18
to packe...@googlegroups.com
you can try like this


"execute_command": "{{.Vars}} var1={{user `arg1`}} var2={{user `arg2`}} sudo -E -S bash '{{.Path}' "

then on the script check the content of var1 and var2

alvaro 


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/6d306445-6b20-4177-95fd-1abade411e9e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Rickard von Essen

unread,
Mar 5, 2018, 2:09:51 PM3/5/18
to packe...@googlegroups.com
If you still have trouble try to supply a full example (template, scripts, etc) and it's simpler for us to point out what is wrong. 

Bubunia Patra

unread,
Mar 6, 2018, 2:37:07 AM3/6/18
to Packer
Thanks  alvaro it works. But if the key var1 contains "-" it could not recognize and fails with command not found. I tried to use \ (escape) for it but the JSON validation fails. 
If the key contains (dot) - . also it fails for example if the key contains test.abc.def it fails. Any workaround to get rid of this issue? I am using packer 1.0.2.



--
Alvaro

Alvaro Miranda Aguilera

unread,
Mar 6, 2018, 3:14:52 AM3/6/18
to packe...@googlegroups.com
hello

if you can share what and how are you doing (and theres time explaining why?) then will be easier to help you

so far you are to the point you can have variables in packer and then use them in the guest vm

if you are having issues, perhaps you can use a file

packer can provision a file and then on the script you can source the file to have all the variables.

special characters are hard since it may be interpreted somewhere.

if I had to do this, probably will use shell local to put put the variables in a file, or have them in a file already and then use them in the VM.

Alvaro

Bubunia Patra

unread,
Mar 6, 2018, 10:02:49 AM3/6/18
to Packer

Hi,

The below method you suggested works perfect for me :

"execute_command": "{{.Vars}} var1={{user `arg1`}} var2={{user `arg2`}} sudo -E -S bash '{{.Path}' "

But when the var1=test-packer-prov or var1=test.packer.prov then it fails with the "command not found" error.
 Where as if var1=test_packer_prov it works perfectly.

What I tried to add the escape \ with - the JSON parsing fails.

I am trying to use inline/file provision upload method(https://groups.google.com/forum/#!topic/packer-tool/G2TKbBQOGcQ)

Regards
Pradeep

Bubunia Patra

unread,
Mar 7, 2018, 6:29:47 AM3/7/18
to Packer
Hi,

Is there a way I can pass the inline params to the provisioner scripts for example instead of var1 /varlist[0]/[1]  or that needs to be  passed script and script need to do parsing??

Regards
Pradeep

execute_command": "{{.Vars}} var1={{user `arg1`}} var2={{user `arg2`}} sudo -E -S bash '{{.Path}' "

Alvaro Miranda Aguilera

unread,
Mar 7, 2018, 7:46:07 AM3/7/18
to packe...@googlegroups.com
hello

I think you are trying to go to a complex workaround and you could make things simple.

create a file with all the variables you need

then using file provisioning copy the file to the VM/Docker
then using shell provisioning run an script that use that file



--
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/fb6e09f8-e4e7-43e8-b6af-8588e3b737ea%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Reply all
Reply to author
Forward
0 new messages