Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

packer string templates not working -- doc issue? #12651

Closed
matthewsht opened this issue Oct 6, 2023 · 2 comments · Fixed by #13049
Closed

packer string templates not working -- doc issue? #12651

matthewsht opened this issue Oct 6, 2023 · 2 comments · Fixed by #13049
Labels
bug docs help wanted stage/accepted Confirmed, and intend to work on. No timeline commitment though.

Comments

@matthewsht
Copy link

matthewsht commented Oct 6, 2023

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Overview of the Issue

String templates are not really documented and therefore hard to use.
Based on https://developer.hashicorp.com/packer/docs/templates/hcl_templates/expressions#string-templates
I attempted to use the docs from templatefile but apply them to a heredoc string. (As implied by that linked).

It doesn't work. I apologize if this is known issue or documented elsewhere.

Any suggestions?

Reproduction Steps

I have the following:

locals {
  # These are all strings...
  history_file_data   = {
    "build_by"          = local.build_by
    "build_timestamp"   = local.build_timestamp
    "build_author"      = local.build_author
    "build_sha"         = local.build_sha
    "build_repo_clean"  = local.build_repo_clean
    "runtime_username"  = var.runtime_username
    "build_username"    = var.build_username
    "vm_name"           = local.vm_name
  }
  build_description   = <<-EOF
  %{ for history_key, history_value in ${local.history_file_data} ~}
    ${history_key}="${history_value}"
  %{ endfor ~}
  EOF
}

A packer validate run yields:

> packer validate builds/vsphere-iso/rhel/8
Error: Invalid character

  on builds/vsphere-iso/rhel/8/main.pkr.hcl line 68, in locals:
  68:   %{ for history_key, history_value in ${local.history_file_data} }

This character is not used within the language.

Error: Invalid expression

  on builds/vsphere-iso/rhel/8/main.pkr.hcl line 68, in locals:
  68:   %{ for history_key, history_value in ${local.history_file_data} }

Expected the start of an expression, but found an invalid expression token.

Packer version

> packer version
Packer v1.9.4
@matthewsht matthewsht added the bug label Oct 6, 2023
@nywilken nywilken added docs help wanted stage/accepted Confirmed, and intend to work on. No timeline commitment though. labels Oct 17, 2023
@nywilken
Copy link
Contributor

Hi @matthewsht thanks for sharing your example. No examples make it difficult for sure. I will label this as a bug for the docs. We can benefit from having an example or two. If you are interested in contributing to the docs please feel free to open a PR.

That said, It looks like you are escaping the local.history_file_date local variable within the template sequence, using ${...} which is not needed. If you write your snippet to look like the following it should work as expected.

locals {
  # These are all strings...
  history_file_data   = {
    "build_by"          = local.build_by
    "build_timestamp"   = local.build_timestamp
    "build_author"      = local.build_author
    "build_sha"         = local.build_sha
    "build_repo_clean"  = local.build_repo_clean
    "runtime_username"  = var.runtime_username
    "build_username"    = var.build_username
    "vm_name"           = local.vm_name
  }
  build_description   = <<-EOF
  %{ for history_key, history_value in local.history_file_data ~}
    ${history_key}="${history_value}"
  %{ endfor ~}
  EOF
}

Example run with bogus data

~>  packer console source.pkr.hcl
> local.build_description
>     build_author="foobar"
    build_by="foobar"
    build_repo_clean="foobar"
    build_sha="ftobar"
    build_timestamp="foobar"
    build_username="foobar"
    runtime_username="ftobar"
    vm_name="ftobar"

nywilken pushed a commit that referenced this issue Jun 14, 2024
Adds an example of a string template being used.

The example demonstrates how a template sequence can be used to embed the value of a variable into a string that can be used as script content.

Ref: #12651

Signed-off-by: Ryan Johnson <[email protected]>
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug docs help wanted stage/accepted Confirmed, and intend to work on. No timeline commitment though.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants