Packer: HCL2, how to use variable please

Created on 5 Apr 2020  ยท  6Comments  ยท  Source: hashicorp/packer

Hi,

I've checked

but didn't find a clear instruction on how to use variable.
Given the example in
https://blog.deimos.fr/2015/01/16/packer-build-multiple-images-easily/

i.e.,

    "variables": {
        "debian_version": "7.7.0",
        "iso_checksum": "5cb6e4fea55fbb5173f90c3a545b843c6c193e29c3aa32b3306c9bbdfb1ad6a6a36ae8be50e91af9d03d5f21c472bd05d04d3508172e0b519e76714333c7c74b",
        "ansible_inventory_file": "ansible_inventory_file",
        "ansible_environment": "dev",
        "soft_version": "noversion"
    },

. . . 

    {
      "type": "docker-import",
      "repository": "smash",
      "tag": "wheezy_{{user `debian_version`}}-{{user `soft_version`}}",
      "only": ["docker"]
    }

How to turn the above from .json format to HCL2?
If a full HCL2 file can be provided from the .json enclosed at the end, that'd be much more appreciated.

PS. I know that Issues on GitHub are intended to be related to bugs or feature requests, so consider it as a feature request for better documentation please.

PPS. full .json configure file from above url

{
    "variables": {
        "debian_version": "7.7.0",
        "iso_checksum": "5cb6e4fea55fbb5173f90c3a545b843c6c193e29c3aa32b3306c9bbdfb1ad6a6a36ae8be50e91af9d03d5f21c472bd05d04d3508172e0b519e76714333c7c74b",
        "ansible_inventory_file": "ansible_inventory_file",
        "ansible_environment": "dev",
        "soft_version": "noversion"
    },
  "builders": [
    {
      "type": "virtualbox-iso",
      "boot_command": [
        "<esc><wait>",
        "install <wait>",
        "preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg <wait>",
        "debian-installer=en_US <wait>",
        "auto <wait>",
        "locale=en_US <wait>",
        "kbd-chooser/method=us <wait>",
        "keyboard-configuration/xkb-keymap=us <wait>",
        "netcfg/get_hostname={{ .Name }} <wait>",
        "netcfg/get_domain=vagrantup.com <wait>",
        "fb=false <wait>",
        "debconf/frontend=noninteractive <wait>",
        "console-setup/ask_detect=false <wait>",
        "console-keymaps-at/keymap=us <wait>",
        "<enter><wait>"
      ],
      "boot_wait": "10s",
      "disk_size": 32768,
      "guest_os_type": "Debian_64",
      "headless": true,
      "http_directory": "http",
      "iso_checksum": "{{user `iso_checksum`}}",
      "iso_checksum_type": "sha512",
      "iso_url": "http://cdimage.debian.org/cdimage/release/{{user `debian_version`}}/amd64/iso-cd/debian-{{user `debian_version`}}-amd64-netinst.iso",
      "ssh_username": "vagrant",
      "ssh_password": "vagrant",
      "ssh_port": 22,
      "ssh_wait_timeout": "10000s",
      "shutdown_command": "echo 'vagrant'|sudo -S /sbin/shutdown -hP now",
      "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
      "virtualbox_version_file": ".vbox_version",
      "vm_name": "debian-{{user `debian_version`}}-amd64",
      "vboxmanage": [
        [ "modifyvm", "{{.Name}}", "--memory", "512" ],
        [ "modifyvm", "{{.Name}}", "--cpus", "1" ]
      ]
    },
    {
      "type": "docker",
      "image": "debian:wheezy",
      "export_path": "debian.tar"
    }
  ],
  "post-processors": [
    {
      "type": "vagrant",
      "compression_level": "9",
      "output": "wheezy_{{user `debian_version`}}-{{user `soft_version`}}.box",
      "only": ["virtualbox-iso"]
    },
    {
      "type": "docker-import",
      "repository": "smash",
      "tag": "wheezy_{{user `debian_version`}}-{{user `soft_version`}}",
      "only": ["docker"]
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "execute_command": "echo 'vagrant' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
      "scripts": [
        "scripts/update.sh",
        "scripts/sshd.sh",
        "scripts/networking.sh",
        "scripts/ansible_prequesites.sh",
        "scripts/sudoers.sh",
        "scripts/vagrant.sh",
        "scripts/vbaddguest.sh",
        "scripts/ansible_bin.sh"
      ],
      "only": ["virtualbox-iso"]
    },
    {
      "type": "shell",
      "scripts": [
        "scripts/update.sh",
        "scripts/sshd.sh",
        "scripts/networking.sh",
        "scripts/ansible_prequesites.sh",
        "scripts/sudoers.sh",
        "scripts/vagrant.sh",
        "scripts/ansible_bin.sh"
      ],
      "only": ["docker"]
    },
    {
        "type": "ansible-local",
        "playbook_dir": "..",
        "playbook_file": "../site.yml",
        "inventory_file": "{{user `ansible_inventory_file`}}",
        "extra_arguments": ["-e", "set_env={{user `ansible_environment`}}", "-v"]
    },
    {
      "type": "shell",
      "execute_command": "echo 'vagrant' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
      "scripts": [
        "scripts/cleanup.sh"
      ],
      "only": ["virtualbox-iso"]
    },
    {
      "type": "shell",
      "scripts": [
        "scripts/cleanup.sh"
      ],
      "only": ["docker"]
    }
  ]
}

Thanks

hcl2 question

Most helpful comment

I feel your pain... Documentation around Packer HCL2 support is limited. However, this link should give you some ideas: https://packer.io/docs/configuration/from-1.5/variables.html

For a simple working HCL2 example, you can also checkout my repo here: https://github.com/zmingxie/amzn2-wireguard-ami

All 6 comments

I feel your pain... Documentation around Packer HCL2 support is limited. However, this link should give you some ideas: https://packer.io/docs/configuration/from-1.5/variables.html

For a simple working HCL2 example, you can also checkout my repo here: https://github.com/zmingxie/amzn2-wireguard-ami

checking... thanks a lot for your help, @zmingxie!

Yes what @zmingxie said :) also please note that we want to build a transpiler, you give your json file to it and it creates a nice Packer HCL2 folder for you. ( I just created the issue here #9015 ).

Yes what @zmingxie said :) also please note that we want to build a transpiler, you give your json file to it and it creates a nice Packer HCL2 folder for you. ( I just created the issue here #9015 ).

You're welcome @azr. I actually have another HCL2 question related to using tags. If you have some suggestions, could you please comment this? https://discuss.hashicorp.com/t/managing-tags-using-hcl2/7442

Thanks in advance!

Okay I'm going to go ahead and close this issue as is it sounds solved. @suntong please ask on the discuss forum or open a new issue if you need some more informations. :)

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sourav82 picture sourav82  ยท  3Comments

DanielBo picture DanielBo  ยท  3Comments

tleyden picture tleyden  ยท  3Comments

brettswift picture brettswift  ยท  3Comments

mvermaes picture mvermaes  ยท  3Comments