add hcl examples to all standalone code examples in the amazon-chroot page

This commit is contained in:
Megan Marsh 2020-07-17 13:32:09 -07:00
parent 0bb4bf92da
commit 51bc7da964
1 changed files with 194 additions and 21 deletions

View File

@ -127,24 +127,35 @@ Here is a basic example. It is completely valid except for the access keys:
<Tab heading="HCL2"> <Tab heading="HCL2">
```hcl ```hcl
// To make Packer read these variables from the environment into the var object,
// set the environment variables to have the same name as the declared
// variables, with the prefix PKR_VAR_.
// You could also hardcode them into the file, but we recommend against that.
// export PKR_VAR_aws_access_key=$YOURKEY
variable "aws_access_key" { variable "aws_access_key" {
type = string type = string
// default = "hardcoded_key"
} }
// export PKR_VAR_aws_secret_key=$YOURSECRETKEY
variable "aws_secret_key" { variable "aws_secret_key" {
type = string type = string
// default = "hardcoded_secret_key"
} }
source "amazon-chroot" "basic-example" { source "amazon-chroot" "basic-example" {
access_key = var.aws_access_key access_key = var.aws_access_key
secret_key = var.aws_secret_key secret_key = var.aws_secret_key
ami_name = "example-chroot"
source_ami = "ami-e81d5881" source_ami = "ami-e81d5881"
} }
build { build {
source "sources.amazon-chroot.basic-example" { sources = [
ami_name = "packer-amazon-chroot {{timestamp}}" "source.amazon-chroot.basic-example"
} ]
} }
``` ```
@ -163,20 +174,42 @@ chroot by Packer:
- `/dev/pts` (devpts) - `/dev/pts` (devpts)
- `/proc/sys/fs/binfmt_misc` (binfmt_misc) - `/proc/sys/fs/binfmt_misc` (binfmt_misc)
These default mounts are usually good enough for anyone and are sane defaults. These default mounts are usually good enough for anyone and are reasonable
However, if you want to change or add the mount points, you may using the defaults. However, if you want to change or add the mount points, you may using
`chroot_mounts` configuration. Here is an example configuration which only the `chroot_mounts` configuration. Here is an example configuration which only
mounts `/proc` and `/dev`: mounts `/proc` and `/dev`:
<Tabs>
<Tab heading="JSON">
```json ```json
{ ...
"builders": [{
"type": "amazon-chroot"
...
"chroot_mounts": [ "chroot_mounts": [
["proc", "proc", "/proc"], ["proc", "proc", "/proc"],
["bind", "/dev", "/dev"] ["bind", "/dev", "/dev"]
] ]
}]
```
</Tab>
<Tab heading="HCL2">
```hcl
source "amazon-chroot" "basic-example" {
// ... other builder options
chroot_mounts = [
["proc", "proc", "/proc"],
["bind", "/dev", "/dev"]
]
} }
``` ```
</Tab>
</Tabs>
`chroot_mounts` is a list of a 3-tuples of strings. The three components of the `chroot_mounts` is a list of a 3-tuples of strings. The three components of the
3-tuple, in order, are: 3-tuple, in order, are:
@ -210,23 +243,56 @@ For debian based distributions you can setup a
file which will prevent packages installed by your provisioners from starting file which will prevent packages installed by your provisioners from starting
services: services:
```json
({
"type": "shell",
"inline": [
"echo '#!/bin/sh' > /usr/sbin/policy-rc.d",
"echo 'exit 101' >> /usr/sbin/policy-rc.d",
"chmod a+x /usr/sbin/policy-rc.d"
]
},
// ...
{ <Tabs>
"type": "shell", <Tab heading="JSON">
"inline": ["rm -f /usr/sbin/policy-rc.d"]
}) ```json
"provisioners": [
{
"type": "shell",
"inline": [
"echo '#!/bin/sh' > /usr/sbin/policy-rc.d",
"echo 'exit 101' >> /usr/sbin/policy-rc.d",
"chmod a+x /usr/sbin/policy-rc.d"
]
},
{
"type": "shell",
"inline": ["rm -f /usr/sbin/policy-rc.d"]
}
]
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
// ...
build {
sources = [
"source.amazon-chroot.basic-example"
]
// Set policy
provisioner "shell" {
inline = [
"echo '#!/bin/sh' > /usr/sbin/policy-rc.d",
"echo 'exit 101' >> /usr/sbin/policy-rc.d",
"chmod a+x /usr/sbin/policy-rc.d"
]
}
// Un-set policy
provisioner "shell" {
inline = ["rm -f /usr/sbin/policy-rc.d"]
}
}
```
</Tab>
</Tabs>
### Ansible provisioner ### Ansible provisioner
Running ansible against `amazon-chroot` requires changing the Ansible Running ansible against `amazon-chroot` requires changing the Ansible
@ -242,6 +308,9 @@ involving the `nvme_device_path` option above. Read that for more information.
A working example for mounting an NVMe device is below: A working example for mounting an NVMe device is below:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"variables": { "variables": {
@ -276,6 +345,60 @@ A working example for mounting an NVMe device is below:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
// export PKR_VAR_aws_access_key=$YOURKEY
variable "aws_access_key" {
type = string
}
// export PKR_VAR_aws_secret_key=$YOURSECRETKEY
variable "aws_secret_key" {
type = string
}
source "amazon-chroot" "basic-example" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
region = "us-east-1"
source_ami_filter {
filter {
key = "virtualization-type"
value = "hvm"
}
filter {
key = "name"
value = "amzn-ami-hvm-*"
}
filter {
key = "root-device-type"
value = "ebs"
}
owners = ["137112412989"]
most_recent = true
}
ena_support = true
ami_name = "amazon-chroot-test-{{timestamp}}"
nvme_device_path = "/dev/nvme1n1p"
device_path = "/dev/sdf"
}
build {
sources = [
"source.amazon-chroot.basic-example"
]
provisioner "shell" {
inline = ["echo Test > /tmp/test.txt"]
}
}
```
</Tab>
</Tabs>
Note that in the `nvme_device_path` you must end with the `p`; if you try to Note that in the `nvme_device_path` you must end with the `p`; if you try to
define the partition in this path (e.g. `nvme_device_path`: `/dev/nvme1n1p1`) define the partition in this path (e.g. `nvme_device_path`: `/dev/nvme1n1p1`)
and haven't also set the `"mount_partition": 0`, a `1` will be appended to the and haven't also set the `"mount_partition": 0`, a `1` will be appended to the
@ -289,6 +412,9 @@ The device setup commands partition the device with one partition for use as an
HVM image and format it ext4. This builder block should be followed by HVM image and format it ext4. This builder block should be followed by
provisioning commands to install the os and bootloader. provisioning commands to install the os and bootloader.
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "amazon-chroot", "type": "amazon-chroot",
@ -311,6 +437,53 @@ provisioning commands to install the os and bootloader.
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
// This example assumes that AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID are
// set in your environment, or a ~/.aws/credentials file is configured.
source "amazon-chroot" "basic-example" {
region = "us-east-1"
ami_name = "packer-from-scratch {{timestamp}}"
from_scratch = true
ami_virtualization_type = "hvm"
pre_mount_commands = [
"parted {{.Device}} mklabel msdos mkpart primary 1M 100% set 1 boot on print",
"mkfs.ext4 {{.Device}}1"
]
root_volume_size = 15
root_device_name = "xvda"
ami_block_device_mappings {
device_name = "xvda"
delete_on_termination = true
volume_type = "gp2"
}
}
build {
sources = [
"source.amazon-chroot.basic-example"
]
provisioner "shell" {
inline = [
"echo '#!/bin/sh' > /usr/sbin/policy-rc.d",
"echo 'exit 101' >> /usr/sbin/policy-rc.d",
"chmod a+x /usr/sbin/policy-rc.d"
]
}
provisioner "shell" {
inline = ["rm -f /usr/sbin/policy-rc.d"]
}
}
```
</Tab>
</Tabs>
## Build template data ## Build template data
In configuration directives marked as a template engine above, the following In configuration directives marked as a template engine above, the following