컨텍스트 변수
컨텍스트 변수 (Contextual variables)
이 문서는 구성 컨텍스트에 따라 연결 및 기본 인스턴스 상태 정보를 반환하는 특수 변수들에 대한 참조 정보를 제공해요.
출처: Packer 공식 문서
본문
Source Variables (소스 변수)
프로비저너와 포스트-프로세서에서 source 의 name 과 type 에 접근하려면 다음 문법을 사용해요.
<build-name>.<source-type>.<source-name>
다음 예시는 빌드에서 사용된 소스 이름을 조회해요.
source "null" "first-example" {
communicator = "none"
}
build {
name = "roles"
source "null.first-example" {
name = "consul"
}
source "null.first-example" {
name = "nomad"
}
source "null.first-example" {
name = "vault"
}
sources = ["null.first-example"]
provisioner "shell-local" {
inline = ["echo ${source.name} and ${source.type}"]
}
}
예시는 다음 값을 반환해요.
roles.null.consul:consul와null반환roles.null.nomad:nomad와null반환roles.null.vault:vault와null반환roles.null.first-example:first-example와null반환
Build Variables (빌드 변수)
빌드 변수는 빌더에 대한 연결 정보와 기본 인스턴스 상태 정보에 접근할 수 있게 해 줘요. 모든 특수 빌드 변수는 build 변수에 저장돼요.
source "null" "first-example" {
communicator = "none"
}
build {
name = "my-build-name"
sources = ["null.first-example"]
provisioner "shell-local" {
environment_vars = ["TESTVAR=${build.PackerRunUUID}"]
inline = ["echo source.name is ${source.name}.",
"echo build.name is ${build.name}.",
"echo build.PackerRunUUID is $TESTVAR"]
}
}
사용 가능한 빌드 변수 목록은 다음과 같아요.
name— 실행 중인 build 블록의 이름을 나타내요. 이는 실행 중인 source 블록의 이름과는 달라요.ID— 프로비저닝되는 VM을 나타내요. 예를 들어 Amazon에서는 인스턴스 ID, DigitalOcean에서는 Droplet ID, VMware에서는 VM 이름이에요.Host,Port,User그리고Password— Packer가 머신에 접근할 때 사용하는 호스트, 포트, 사용자, 비밀번호예요. 프로비저닝된 인스턴스에 대해 Ansible이나 Inspec을 실행하기 위해 shell local 프로비저너를 사용할 때 유용해요.ConnType— 사용 중인 커뮤니케이터의 타입. 예를 들어 SSH 커뮤니케이터면 "ssh"가 돼요.PackerRunUUID— 현재 빌드의 고유 ID. 빌드 아티팩트를 지정하는 데 사용할 수 있어요. 예시로, 여러 빌드가 동시에 실행되어 같은 아티팩트를 만들 때가 있어요. 이 아티팩트들을 빌드의 고유 ID로 이름을 지어 구분할 수 있어요.PackerHTTPIP,PackerHTTPPort, 그리고PackerHTTPAddr— Packer가 "http" 디렉터리의 항목을 VM에 제공하기 위해 만드는 파일 서버의 HTTP IP, 포트, 주소예요. HTTP 주소는IP:PORT형식으로 표시돼요.SSHPublicKey와SSHPrivateKey— Packer가 인스턴스에 연결하는 데 사용하는 공개·개인 키예요. 이들은 SSH 커뮤니케이터에 고유하며 다른 커뮤니케이터를 사용할 때는 설정되지 않아요. SSHPublicKey와 SSHPrivateKey는 이스케이프 시퀀스와 특수 문자를 가질 수 있으므로, 출력을 단일 인용부호로 감싸 예상 밖의 일을 피해야 해요. 예:
provisioner "shell" {
inline = ["echo '${build.SSHPrivateKey}' > /tmp/packer-session.pem"]
}
하위 호환성을 위해 WinRMPassword 도 이 엔진을 통해 사용할 수 있어요. 다만 더 일반적인 Password 를 사용하는 것과 다르지 않아요.
모든 빌드 변수는 HCL2 함수와 함께 사용할 수 있어요. upper를 사용해 빌드 ID를 대문자로 만드는 예시:
post-processor "shell-local" {
inline = ["echo ${upper(build.ID)}"]
}
빌더별 빌더 변수에 대해서는 빌더 문서도 참고하세요.
- Amazon EC2: chroot, EBS Volume, EBS, EBS Surrogate, Instance.
HCL2 Special Build Variables 는 베타 상태예요. 이슈나 요청이 있으면 GitHub의 Packer 이슈 트래커에 보고해 주세요.
Packer Version (Packer 버전)
이 변수는 현재 실행 중인 Packer 버전으로 설정돼요.
source "null" "first-example" {
communicator = "none"
}
build {
sources = ["null.first-example"]
provisioner "shell-local" {
inline = ["echo packer_version is '${packer.version}'"]
}
}
개발 버전의 Packer를 실행 중이라면 버전 변수는 릴리스 버전 번호, dev 플래그, 그리고 현재 커밋을 담아요.
PACKER_LOG=0 packer build packer_version_demo.pkr.hcl
null.first-example: output will be in this color.
==> null.first-example: Running local shell script: /var/folders/8t/0yb5q0_x6mb2jldqq_vjn3lr0000gn/T/packer-shell083160352
null.first-example: packer_version is 1.6.5-dev (a69392129+CHANGES)
릴리스 버전의 Packer를 실행 중이라면 버전 변수는 릴리스 버전 번호만 담아요.
PACKER_LOG=0 packer build packer_version_demo.pkr.hcl
null.first-example: output will be in this color.
==> null.first-example: Running local shell script: /var/folders/8t/0yb5q0_x6mb2jldqq_vjn3lr0000gn/T/packer-shell718995312
null.first-example: packer_version is 1.6.5
반환되는 문자열을 이스케이프하려면 변수를 단일 인용부호로 감싸야 해요. 개발 버전의 packer를 실행 중이라면 괄호가 셸 이스케이프를 어긋나게 할 수 있어요.
HCP Packer Iteration ID (HCP Packer 반복 ID)
참고: packer.iterationID 변수는 이제 사용 중단되었고 향후 Packer 버전에서 제거될 거예요. HCP Packer 버전은 fingerprint로 접근해야 해요. packer.versionFingerprint 변수가 새 HCP Packer 데이터 소스들과 함께 그 자리를 대신하도록 이제 노출돼 있어요.
빌드가 HCP Packer 레지스트리로 메타데이터를 푸시한다면, 이 변수는 이 실행과 연결된 Iteration ID 값으로 설정돼요.
source "null" "example" {
communicator = "none"
}
data "hcp-packer-version" "hardened-source" {
bucket_name = "example"
channel_name = "latest"
}
data "hcp-packer-artifact" "file" {
bucket_name = "example"
version_fingerprint = "${data.hcp-packer-version.hardened-source.fingerprint}"
platform = "aws"
region = "us-east-1"
}
build {
hcp_packer_registry {
bucket_name = "simple"
}
sources = [
"source.null.example"
]
provisioner "shell-local" {
inline = [
"echo data is ${packer.iterationID}"
]
}
}
==> mybuild.null.example: Running local shell script: /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-shell842419427
mybuild.null.example: data is 01HN3KCRPVKR5PBQ28TS6B12W0
HCP Packer Version Fingerprint (HCP Packer 버전 지문)
빌드가 HCP Packer 레지스트리로 메타데이터를 푸시한다면, 이 변수는 이 실행과 연결된 Version Fingerprint 값으로 설정돼요.
source "null" "example" {
communicator = "none"
}
data "hcp-packer-version" "hardened-source" {
bucket_name = "example"
channel_name = "latest"
}
data "hcp-packer-artifact" "file" {
bucket_name = "example"
version_fingerprint = "${data.hcp-packer-version.hardened-source.fingerprint}"
platform = "aws"
region = "us-east-1"
}
build {
hcp_packer_registry {
bucket_name = "simple"
}
sources = [
"source.null.example"
]
provisioner "shell-local" {
inline = [
"echo data is ${packer.versionFingerprint}"
]
}
}
==> mybuild.null.example: Running local shell script: /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-shell842419427
mybuild.null.example: data is 01HN3KCRPVKR5PBQ28TS6B12W0
이 값을 포스트-프로세서에 추가해서 manifest 파일에 넣을 수도 있어요.
post-processor "manifest" {
output = "manifest.json"
strip_path = true
custom_data = {
version_fingerprint = "${packer.versionFingerprint}"
iteration = "${packer.iterationID}"
}
}
더 알아보기 (Learn more)
- 이 페이지는 Packer 공식 문서에서 가져왔어요.