finding ami_name from within a script in provisioners

30 views
Skip to first unread message

sibi das

unread,
Nov 29, 2017, 3:14:26 AM11/29/17
to Packer
Hi,

I am running a script in provisioners section, which requires AMI name used by packer. I know the pre-validation of AMI name by packer happens before this stage. Is there any reliable way in which I can get the ami_name inside my script ?

My script would go like this:

"provisioners": [ { "type": "shell", "execute_command": "sudo {{.Vars}} bash '{{.Path}}'", "scripts": [ "./scripts/packer/myscript.sh" ] } ]

./scripts/packer/myscript.sh:

#!/usr/bin/env bash aminame=$ami_name make_an_external_call "$aminame"

Alvaro Miranda Aguilera

unread,
Nov 29, 2017, 4:14:25 AM11/29/17
to packe...@googlegroups.com
you want to feed there the AMI name of the current running instance?

Your script should be able to get this from the metadata



curl http://169.254.169.254/latest/meta-data/ami-id ami-12345678

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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/a88ca74e-0415-4b96-ad5a-340af42f0d36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

sibi das

unread,
Nov 29, 2017, 6:30:06 AM11/29/17
to Packer
Thanks Alvaro. Basically I need to get the target AMI Name (the one that is given to packer in "builders" section). Packer will use this ami name while creating image right. May be I need to clarify a few things:

The packer json file will be submitted by the user. I will inject a script in provisioners section, which will do few things. I can always parse the packer json to find the ami_name variable in builders section and pass it as a parameter to my script in provisioners. But if the user has passed a dynamic value for ami_name(something suffixed with -timestamp for eg. or a user variable), then I will get to know about the ami name only after the build starts. So somehow I need to findout the target AMI Name used by packer from within my script in provisioners. Is there any environment variable or something pushed by packer, which will allow me to get this value?


On Wednesday, November 29, 2017 at 2:44:25 PM UTC+5:30, Alvaro Miranda Aguilera wrote:
you want to feed there the AMI name of the current running instance?

Your script should be able to get this from the metadata



curl http://169.254.169.254/latest/meta-data/ami-id ami-12345678

Alvaro.

On Wed, Nov 29, 2017 at 9:14 AM, sibi das <sib...@gmail.com> wrote:
Hi,

I am running a script in provisioners section, which requires AMI name used by packer. I know the pre-validation of AMI name by packer happens before this stage. Is there any reliable way in which I can get the ami_name inside my script ?

My script would go like this:

"provisioners": [ { "type": "shell", "execute_command": "sudo {{.Vars}} bash '{{.Path}}'", "scripts": [ "./scripts/packer/myscript.sh" ] } ]

./scripts/packer/myscript.sh:

#!/usr/bin/env bash aminame=$ami_name make_an_external_call "$aminame"

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



--
Alvaro

Alvaro Miranda Aguilera

unread,
Nov 29, 2017, 6:50:12 AM11/29/17
to packe...@googlegroups.com
you want the source ami from where this AMI was created?
or you want the current AMI of the running instance ?


Virusvrij. www.avast.com

sibi das

unread,
Nov 29, 2017, 7:13:48 AM11/29/17
to Packer
I need the AMI name of the image being built. The target AMI name or you can say the futuristic value. The same name that we see in packer logs : Prevalidating AMI Name: *****

Alvaro Miranda Aguilera

unread,
Nov 29, 2017, 8:39:35 AM11/29/17
to packe...@googlegroups.com
why not getting it from the metadata in the script ?

--
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/ffb570a8-9f68-4bcd-8647-40c449dbfe3b%40googlegroups.com.

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



--
Alvaro

sibi das

unread,
Nov 29, 2017, 9:18:25 AM11/29/17
to Packer
The problem is dynamic variables. For eg. for below section, the exact name of ami will be known only during build time.

"builders":[{
  ...
  ...
  "ami_name": "packer-quick-start {{timestamp}}"
}]


On Wednesday, November 29, 2017 at 7:09:35 PM UTC+5:30, Alvaro Miranda Aguilera wrote:
why not getting it from the metadata in the script ?
On Wed, Nov 29, 2017 at 1:13 PM, sibi das <sib...@gmail.com> wrote:
I need the AMI name of the image being built. The target AMI name or you can say the futuristic value. The same name that we see in packer logs : Prevalidating AMI Name: *****

On Wednesday, November 29, 2017 at 5:20:12 PM UTC+5:30, Alvaro Miranda Aguilera wrote:
you want the source ami from where this AMI was created?
or you want the current AMI of the running instance ?


Virusvrij. www.avast.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...@googlegroups.com.



--
Alvaro

sibi das

unread,
Nov 29, 2017, 1:29:32 PM11/29/17
to Packer
I managed to get this done by getting the value of ami_name in builders section using jq, and passing it as it is, to 'execute_command' under provisioners section. Since execute_command also 
honors user variables, it is working fine. Thanks Alvaro.
Reply all
Reply to author
Forward
0 new messages