NAV Navbar
terraform json
  • Repo Link
  • A Quick Start
  • Special Notes
  • Builders
  • Provisioners
  • Post-Processors
  • Examples
  • Repo Link

    Link

    A Quick Start

    This provider provide a data source call "packer_json" and its purpose is not to run packer but rather generate a packer json to be run with packer build

    A Complete Example:

    {
      "builders": [
        {
          "boot_command": [
            "<enter><wait><f6><esc><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "/install/vmlinuz<wait>",
            " auto<wait>",
            " console-setup/ask_detect=false<wait>",
            " console-setup/layoutcode=us<wait>",
            " console-setup/modelcode=pc105<wait>",
            " debconf/frontend=noninteractive<wait>",
            " debian-installer=en_US<wait>",
            " fb=false<wait>",
            " initrd=/install/initrd.gz<wait>",
            " kbd-chooser/method=us<wait>",
            " keyboard-configuration/layout=USA<wait>",
            " keyboard-configuration/variant=USA<wait>",
            " locale=en_US<wait>",
            " netcfg/get_domain=vm<wait>",
            " netcfg/get_hostname=vagrant<wait>",
            " grub-installer/bootdev=/dev/sda<wait>",
            " noapic<wait>",
            " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg",
            " -- <wait>",
            "<enter><wait>"
          ],
          "boot_wait": "10s",
          "disk_size": 81920,
          "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
          "guest_os_type": "Ubuntu_64",
          "headless": true,
          "http_directory": "http",
          "iso_checksum": "0a03608988cfd2e50567990dc8be96fb3c501e198e2e6efcb846d89efc7b89f2",
          "iso_checksum_type": "sha256",
          "iso_urls": [
            "iso/ubuntu-16.04.4-server-amd64.iso",
            "http://releases.ubuntu.com/16.04/ubuntu-16.04.4-server-amd64.iso"
          ],
          "shutdown_command": "echo 'vagrant'|sudo -S shutdown -P now",
          "ssh_password": "vagrant",
          "ssh_port": 22,
          "ssh_username": "vagrant",
          "ssh_wait_timeout": "10000s",
          "type": "virtualbox-iso",
          "vboxmanage": [
            [
              "modifyvm",
              "{{.Name}}",
              "--memory",
              "1024"
            ],
            [
              "modifyvm",
              "{{.Name}}",
              "--cpus",
              "1"
            ]
          ],
          "virtualbox_version_file": ".vbox_version",
          "vm_name": "packer-ubuntu-16.04-amd64"
        }
      ],
      "post-processors": [
        [
          {
            "output": "builds/{{.Provider}}-ubuntu1604.box",
            "type": "vagrant"
          },
          {
            "box_tag": "geerlingguy/ubuntu1604",
            "type": "vagrant-cloud",
            "version": "{{user `version`}}"
          }
        ]
      ],
      "provisioners": [
        {
          "execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
          "script": "scripts/ansible.sh",
          "type": "shell"
        },
        {
          "execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
          "script": "scripts/setup.sh",
          "type": "shell"
        },
        {
          "galaxy_file": "requirements.yml",
          "playbook_file": "ansible/main.yml",
          "type": "ansible-local"
        },
        {
          "execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
          "script": "scripts/cleanup.sh",
          "type": "shell"
        }
      ],
      "variables": {
        "version": ""
      }
    }
    
    locals {
        boot_commands = [
            "<enter><wait><f6><esc><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "/install/vmlinuz<wait>",
            " auto<wait>",
            " console-setup/ask_detect=false<wait>",
            " console-setup/layoutcode=us<wait>",
            " console-setup/modelcode=pc105<wait>",
            " debconf/frontend=noninteractive<wait>",
            " debian-installer=en_US<wait>",
            " fb=false<wait>",
            " initrd=/install/initrd.gz<wait>",
            " kbd-chooser/method=us<wait>",
            " keyboard-configuration/layout=USA<wait>",
            " keyboard-configuration/variant=USA<wait>",
            " locale=en_US<wait>",
            " netcfg/get_domain=vm<wait>",
            " netcfg/get_hostname=vagrant<wait>",
            " grub-installer/bootdev=/dev/sda<wait>",
            " noapic<wait>",
            " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg",
            " -- <wait>",
            "<enter><wait>"
        ]
    }
    
    data "packer_json" "default" {
        builders {
            virtualbox_iso {
                boot_command = ["${local.boot_commands}"]
                boot_wait = "10s"
                disk_size = 81920
                guest_os_type = "Ubuntu_64"
                headless = true
                http_directory = "http"
    
                iso {
                    iso_urls = [
                        "iso/ubuntu-16.04.4-server-amd64.iso",
                        "http://releases.ubuntu.com/16.04/ubuntu-16.04.4-server-amd64.iso"
                    ]
    
                    iso_checksum_type = "sha256"
    
                    iso_checksum = "0a03608988cfd2e50567990dc8be96fb3c501e198e2e6efcb846d89efc7b89f2"
                }
    
                ssh {
                    ssh_username = "vagrant"
                    ssh_password = "vagrant"
                    ssh_port = 22
                    ssh_wait_timeout = "10000s"
                }
    
                shutdown_command = "echo 'vagrant'|sudo -S shutdown -P now"
    
                guest_additions_path = "VBoxGuestAdditions_{{.Version}}.iso"
                virtualbox_version_file = ".vbox_version"
                vm_name = "packer-ubuntu-16.04-amd64"
    
                vboxmanage {
                    values = [ "modifyvm", "{{.Name}}", "--memory", "1024"]
                }
    
                vboxmanage {
                    values = [ "modifyvm", "{{.Name}}", "--cpus", "1" ]
                }
            }
        }
    
        provisioners {
            shell {
                execute_order = 1
                execute_command = "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'"
                script = "scripts/ansible.sh"
            }
    
            shell {
                execute_order = 2
                execute_command = "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'"
                script = "scripts/setup.sh"
            }
    
            ansible_local {
                execute_order = 3
                playbook_file = "ansible/main.yml"
                galaxy_file = "requirements.yml"
            }
    
            shell {
                execute_order = 4
                execute_command = "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'"
                script = "scripts/cleanup.sh"
            }
        }
    
        post_processors {
            vagrant {
                pipeline {
                    set = 1 
                    order = 1
                }
    
                output = "builds/{{.Provider}}-ubuntu1604.box"
            }
    
            vagrant_cloud {
                pipeline {
                    set = 1 
                    order = 2
                }
    
                box_tag = "geerlingguy/ubuntu1604"
                version = "{{user `version`}}"
            }
        }
    
        variables {
            version = ""
        }
    
    }
    

    This is an example with both ordering of provisioners and sequence definition of post-processors

    Special Notes

    Named Build

    data "packer_json" "default" {
        builders {
            null {
                communicator = "ssh"
                name = "test"
                ssh {
                    ssh_username = "test_username"
                    ssh_password = "test_password"
                }
            }
    
            null {
                communicator = "ssh"
                name = "test2"
                ssh {
                    ssh_username = "test_username"
                    ssh_password = "test_password"
                }
            }
        }
    }
    
    {
      "builders": [
        {
          "communicator": "ssh",
          "name": "test",
          "ssh_password": "test_password",
          "ssh_username": "test_username",
          "type": "null"
        },
        {
          "communicator": "ssh",
          "name": "test2",
          "ssh_password": "test_password",
          "ssh_username": "test_username",
          "type": "null"
        }
      ],
      "post-processors": [],
      "provisioners": []
    }
    

    To create named builders to use name parameter in the block

    Ordering in Provisioners

    data "packer_json" "default" {
        provisioners {
            shell {
                execute_order = 1
                script = "echo 'Hi'"
            }
    
            shell {
                execute_order = 2
                script = "echo 'Hi Again'"
            }
        }
    }
    
    {
      "builders": [],
      "post-processors": [],
      "provisioners": [
        {
          "script": "echo 'Hi'",
          "type": "shell"
        },
        {
          "script": "echo 'Hi Again'",
          "type": "shell"
        }
      ]
    }
    

    Because terraform is a declarative language, and provisioners block is execute by the order, a workaround is introduced.

    execute_order is a require parameter in all provisioner, it denote the order of the current provisioner in the resulting json

    Sequence Definition

    data "packer_json" "default" {
        post_processors {
            compress {
                pipeline {
                    set = 1 
                    order = 1
                }
            }
    
            checksum {
                pipeline {
                    set = 1 
                    order = 2
                }
            }
    
            manifest {}
        }
    }
    
    {
      "builders": [],
      "post-processors": [
        {
          "type": "manifest"
        },
        [
          {
            "type": "compress"
          },
          {
            "type": "checksum"
          }
        ]
      ],
      "provisioners": []
    }
    

    Same situation as ordering in provisioners, to create sequence definition in post-processors a workaround was introduced

    pipeline block consists two require parameter, order and set.

    set denote the sequence set, in scenario that a person want multiple sequence definition

    order denote the order of the current post-processor in the sequence

    Override

    data "packer_json" "default" {
        provisioners {
            shell {
                execute_order = 1
    
                execute_command = "Not again"
    
                override =<<EOF
                {
                    "vmware-iso": {
                        "execute_command":"Hi"
                    }
                }
    EOF
            }
        }
    }
    
    {
      "builders": [],
      "post-processors": [],
      "provisioners": [
        {
          "execute_command": "Not again",
          "override": {
            "vmware-iso": {
              "execute_command": "Hi"
            }
          },
          "type": "shell"
        }
      ]
    }
    

    As of now, terraform has issue dealing multi-depth type with map, so a workaround for override is introduce, the parameter accept string, but is recommended to use heredoc, also the string will be validated to ensure is a valid json string

    SSH Block

    data "packer_json" "default" {
        builders {
            virtualbox_iso {
                iso {
                    iso_url = "test"
                    iso_checksum = "test"
                    iso_checksum_type = "none"
                }
    
                ssh {
                    ssh_username = "test"
                    ssh_password = "test"
                }
            }
        }
    }
    
    {
      "builders": [
        {
          "iso_checksum": "test",
          "iso_checksum_type": "none",
          "iso_url": "test",
          "ssh_password": "test",
          "ssh_username": "test",
          "type": "virtualbox-iso"
        }
      ],
      "post-processors": [],
      "provisioners": []
    }
    

    SSH in builders that supported are in its own block

    WinRM Block

    data "packer_json" "default" {
        builders {
            virtualbox_iso {
                iso {
                    iso_url = "test"
                    iso_checksum = "test"
                    iso_checksum_type = "none"
                }
    
                winrm {
                    winrm_username = "test"
                    winrm_password = "test"
                }
            }
        }
    }
    
    {
      "builders": [
        {
          "iso_checksum": "test",
          "iso_checksum_type": "none",
          "iso_url": "test",
          "type": "virtualbox-iso",
          "winrm_password": "test",
          "winrm_username": "test"
        }
      ],
      "post-processors": [],
      "provisioners": []
    }
    

    WinRM in builders that supported are in its own block

    Builders

    Alicloud ECS

    Block Name: alicloud_ecs

    Special Note:

    image_disk_mappings

    data "packer_json" "default" {
        builders {
            alicloud_ecs {
                access_key = "none"
                secret_key = "none"
                region = "none"
                image_name = "none"
                source_image = "none"
                instance_type = "none"
    
                image_disk_mapping {
                    disk_category = "cloud_ssd"
                }
    
                image_disk_mapping {
                    disk_category = "cloud"
                }
            }
        }
    
    }
    
    {
      "builders": [
        {
          "access_key": "none",
          "image_disk_mappings": [
            {
              "disk_category": "cloud_ssd"
            },
            {
              "disk_category": "cloud"
            }
          ],
          "image_name": "none",
          "instance_type": "none",
          "region": "none",
          "secret_key": "none",
          "source_image": "none",
          "type": "alicloud-ecs"
        }
      ],
      "post-processors": [],
      "provisioners": []
    }
    

    Amazon Chroot

    Block Name: amazon_chroot

    Amazon EBS

    Block Name: amazon_ebs

    Amazon EBS Surrogate

    Block Name: amazon_ebssurrogate

    Amazon EBS Volume

    Block Name: amazon_ebsvolume

    Amazon Instance

    Block Name: amazon_instance

    AzureARM

    Block Name: azurearm

    CloudStack

    Block Name: cloudstack

    DigitalOcean

    Block Name: digitalocean

    Docker

    Block Name: docker

    File

    Block Name: file

    GoogleCompute

    Block Name: googlecompute

    Hyper-V ISO

    Block Name: hyperv_iso

    Hyper-V VMCX

    Block Name: hyperv_vmcx

    LXC

    Block Name: lxc

    LXD

    Block Name: lxd

    NCloud

    Block Name: ncloud

    Null

    Block Name: null

    1&1

    Block Name: oneandone

    OpenStack

    Block Name: openstack

    Oracle Classic

    Block Name: oracle_classic

    Oracle OCI

    Block Name: oracle_oci

    Parallels ISO

    Block Name: parallels_iso

    Parallels PVM

    Block Name: parallels_pvm

    Profitbricks

    Block Name: profitbricks

    QEMU

    Block Name: qemu

    Scaleway

    Block Name: scaleway

    Triton

    Block Name: triton

    Virtualbox ISO

    Block Name: virtualbox_iso

    data "packer_json" "default" {
        builders {
            virtualbox_iso {
                communicator = "ssh"
                iso {
                    iso_url = "test"
                    iso_checksum = "test"
                    iso_checksum_type = "none"
                }
    
                vboxmanage {
                    values = [ "modifyvm", "{{.Name}}", "--memory", "1024"]
                }
    
                vboxmanage {
                    values = [ "modifyvm", "{{.Name}}", "--cpus", "1" ]
                }
    
            }
        }
    }
    
    {
      "builders": [
        {
          "communicator": "ssh",
          "iso_checksum": "test",
          "iso_checksum_type": "none",
          "iso_url": "test",
          "type": "virtualbox-iso",
          "vboxmanage": [
            [
              "modifyvm",
              "{{.Name}}",
              "--memory",
              "1024"
            ],
            [
              "modifyvm",
              "{{.Name}}",
              "--cpus",
              "1"
            ]
          ]
        }
      ],
      "post-processors": [],
      "provisioners": []
    }
    

    Special Note:

    vboxmanage and vboxmanage_post

    Virtualbox OVF

    Block Name: virtualbox_ovf

    VMware ISO

    Block Name: vmware_iso

    VMware VMX

    Block Name: vmware_vmx

    Provisioners

    Ansible

    Block Name: ansible

    Ansible Local

    Block Name: ansible_local

    Chef Client

    Block Name: chef_client

    Chef Solo

    Block Name: chef_solo

    Converge

    Block Name: converge_solo

    File

    Block Name: file_solo

    PowerShell

    Block Name: powershell

    Puppet Masterless

    Block Name: puppet_masterless

    Puppet Server

    Block Name: puppet_server

    Salt Masterless

    Block Name: salt_masterless

    Shell

    Block Name: shell

    Shell Local

    Block Name: shell_local

    Windows Restart

    Block Name: windows_restart

    Windows Shell

    Block Name: windows_shell

    Post-Processors

    Alicloud Import

    Block Name: alicloud_import

    Amazon Import

    Block Name: amazon_import

    Artifice

    Block Name: artifice

    Atlas

    Block Name: atlas

    Checksum

    Block Name: checksum

    Compress

    Block Name: compress

    Docker Import

    Block Name: docker_import

    Docker Push

    Block Name: docker_push

    Docker Save

    Block Name: docker_save

    Docker Tag

    Block Name: docker_tag

    GoogleCompute Export

    Block Name: googlecompute_export

    Manifest

    Block Name: manifest

    Shell Local

    Block Name: shell_local

    Vagrant

    Block Name: vagrant

    Vagrant Cloud

    Block Name: vagrant_cloud

    vSphere

    Block Name: vsphere

    vSphere Template

    Block Name: vsphere_template

    Examples