hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.02k stars 3.32k forks source link

Allow HTTPIP, HTTPPort, Name to be referenced from user variables #5749

Closed mcandre closed 6 years ago

mcandre commented 6 years ago

I would like to move my boot_command value up into my packer user variables section, but I need to be able to reference {{ .HTTPIP }}, {{ .HTTPPort }}, and {{ .Name }} within the boot_command string. Could packer make these pre-defined variables able to be inserted into strings during user variable declaration?

rickard-von-essen commented 6 years ago

Thanks for reporting this. This is a duplicate of #4387, closing this in favor of that.

akutz commented 5 years ago

I'm not sure this is a duplicate. Trying to use the above variables in a user variables section causes an error with Packer 1.4.1:

{
  "build_name": "centos-7a",
  "distro_name": "centos",
  "os_display_name": "CentOS 7",
  "guest_os_type": "centos-64",
  "iso_url": "http://mirrors.kernel.org/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-NetInstall-1810.iso",
  "iso_checksum": "d74ea11d73e7183fbbd8dcdc4f1a74a5",
  "iso_checksum_type": "md5",
  "ssh_username": "centos",
  "ssh_password": "centos",
  "boot_command": "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/7/ks.cfg<enter><wait>",
  "goss_vars_file": "goss/redhat-vars.yaml"
}
$ packer build -var-file kubernetes.json -var-file centos-7.json packer.json
centos-7a output will be in this color.

render 'packer_user_variables': template: root:1:24: executing "root" at <.HTTPIP>: can't evaluate field HTTPIP in type *ansible.PassthroughTemplate in:

<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/7/ks.cfg<enter><wait>
akutz commented 5 years ago

Hi all,

I think I have a work-around that appears to be working:

      "boot_command": [
        "{{user `boot_command_prefix`}}",
        "http://{{ .HTTPIP }}:{{ .HTTPPort }}",
        "{{user `boot_command_suffix`}}"
      ],

I just have to define any prefix and suffix data for the boot command as part of the user variables, and formatting the above like so in the proper packer.json results in the correct interpolation.

ghost commented 4 years ago

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.