Interpolate shell inline config

This commit is contained in:
Moss 2020-03-12 17:54:31 +01:00
parent 7b705545d9
commit a3740bb9be
2 changed files with 45 additions and 12 deletions

View File

@ -70,12 +70,6 @@ type Provisioner struct {
config Config
}
type ExecuteCommandTemplate struct {
Vars string
EnvVarFile string
Path string
}
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
func (p *Provisioner) Prepare(raws ...interface{}) error {
@ -181,7 +175,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
return nil
}
func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error {
func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error {
if generatedData == nil {
generatedData = make(map[string]interface{})
}
scripts := make([]string, len(p.config.Scripts))
copy(scripts, p.config.Scripts)
@ -201,6 +199,11 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
writer := bufio.NewWriter(tf)
writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang))
for _, command := range p.config.Inline {
p.config.ctx.Data = generatedData
command, err := interpolate.Render(command, &p.config.ctx);
if err != nil {
return fmt.Errorf("Error interpolating Inline: %s", err)
}
if _, err := writer.WriteString(command + "\n"); err != nil {
return fmt.Errorf("Error preparing shell script: %s", err)
}
@ -279,11 +282,12 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
defer f.Close()
// Compile the command
p.config.ctx.Data = &ExecuteCommandTemplate{
Vars: flattenedEnvVars,
EnvVarFile: p.config.envVarFile,
Path: p.config.RemotePath,
}
// These are extra variables that will be made available for interpolation.
generatedData["Vars"] = flattenedEnvVars
generatedData["EnvVarFile"] = p.config.envVarFile
generatedData["Path"] = p.config.RemotePath
p.config.ctx.Data = generatedData
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
if err != nil {
return fmt.Errorf("Error processing command: %s", err)

29
template.json Normal file
View File

@ -0,0 +1,29 @@
{
"builders": [
{
"type": "amazon-ebs",
"ami_name": "moss-packer-whee",
"instance_type": "t2.micro",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": ["099720109477"],
"most_recent": true
},
"ssh_username": "ubuntu"
}
],
"provisioners": [
{
"type": "shell-local",
"inline": ["echo MOSS packer run uuid is '{{ build `PackerRunUUID`}}'"]
},
{
"type": "shell",
"inline": ["echo MOSS packer run uuid is '{{ build `PackerRunUUID`}}'"]
}
]
}