Packer: Running a HCL Packer image in Devops Pipeline

Created on 10 Nov 2020  路  12Comments  路  Source: hashicorp/packer

## Overview of the Issue

I can run a packer build locally from my machine in VS Code. I have a Gold-build.pkr.hcl and variables.pkr.hcl file. I run this build by "packer build .\" this works and builds an image.

However, within a Devops pipeline, i point the template location to the gold-build file and the variables file to the variables.pkr file.

This gives me the error

2020-11-10T11:27:32.4587173Z on D:\a\r1\a_spring2020-automation-CI\drop\Terraform\packer\variables.pkr.hcl line 5, in variable "managed_image_name":
2020-11-10T11:27:32.4588881Z 5: variable "managed_image_name"{
2020-11-10T11:27:32.4589225Z
2020-11-10T11:27:32.4589650Z A .pkrvar file is used to assign values to variables that have already been
2020-11-10T11:27:32.4590274Z declared in .pkr files, not to declare new variables. To declare variable
2020-11-10T11:27:32.4590917Z "managed_image_name", place this block in one of your .pkr files, such as
2020-11-10T11:27:32.4591400Z variables.pkr.hcl
2020-11-10T11:27:32.4591662Z

See the attached for my release pipeline.

packerbuild

question

All 12 comments

Hey @jparr93, thanks for opening; the error states that at the top of your variable file there is the following block declation:

variable "managed_image_name"{

And this cannot be inside of a variable hcl file; have you checked the contents of that file ?

@azr thank you for the reply, however it actually errors on all the variables in the variables.pkr.hcl file. These files do work running them in VS Code

@jparr93, I trust that error. It's here to help you too 馃檪 . Is there a way to cat or head the contents of variables.pkr.hcl & Gold-build.pkr.hcl before you run anything ?

I'm sorry I'm repeating myself a lot but variables.pkr.hcl should contain only variable settings so stuff like: managed_image_name = "value". That error is displayed because we found a variable declaration ( variable "managed_image_name"{.

@azr not sure on the cat or head. So, if i change the variable.pkr.hcl file to contain variable = "value", should this work, or will i need another file declaring that there are variables?

I'm sorry I'm repeating myself a lot but variables.pkr.hcl should contain only variable settings so stuff like: managed_image_name = "value". That error is displayed because we found a variable declaration ( variable "managed_image_name"{.

@azr I'm sorry to burge in but the docs https://www.packer.io/guides/hcl/variables says to put variable declaration in variables.pkr.hcl and the values in some variables.pkrvars.hcl. I have it like that and in fact works just fine 馃槙

I'm sorry I'm repeating myself a lot but variables.pkr.hcl should contain only variable settings so stuff like: managed_image_name = "value". That error is displayed because we found a variable declaration ( variable "managed_image_name"{.

@azr I'm sorry to burge in but the docs https://www.packer.io/guides/hcl/variables says to put variable declaration in variables.pkr.hcl and the values in some variables.pkrvars.hcl. I have it like that and in fact works just fine 馃槙

Like i said, my code works fine, it's when i try running it through a Devops pipeline, it errors. I do suspect that the Packer add on for DevOps doesnt support HCL yet, see my screengrab below? It is wanting a JSON file still
packdevops

@azr I'm sorry to burge in but the docs https://www.packer.io/guides/hcl/variables says to put variable declaration in variables.pkr.hcl and the values in some variables.pkrvars.hcl. I have it like that and in fact works just fine 馃槙

@vheon 馃憢馃徏 ehm yes, that's how it should be 馃檪 , that doc statement is correct and I'm just saying the same thing here.

@jparr93:

Like i said, my code works fine, it's when i try running it through a Devops pipeline, it errors. I do suspect that the Packer add on for DevOps doesnt support HCL yet, see my screengrab below? It is wanting a JSON file still

Does it work to pass the folder instead of the configuration file ? So in packer template location, pass ".../Terraform/packer/" Packer should list all .pkr.hcl files as HCL code. The .pkrvars.hcl variables file setting should just work here. If you suffix it .auto.pkrvars.hcl packer should be able to just see it there. Without needing to pass it as a special argument. It can also be split.

Also, if the tool only wants JSON files: it's not so nice but you _can_ set variable files through JSON. So for example in .pkrvars.json:

{
  "variable_name": "value"
}

@azr worked when specifying the auto,pkrvars file, thank you :) having a weird issue now where it can't find the scripts to run during build, even though they are in the root of where the template is

@jparr93 great news ! There are path variables you could use to make things more relative to the directory of the build https://www.packer.io/docs/from-1.5/path-variables

@azr so it would be something like path.root\script.ps1

more something like: key = "${path.root}/script.ps1"

Was this page helpful?
0 / 5 - 0 ratings