yq 실무 명령어 치트시트: YAML 파싱·수정 및 포맷 변환

💡 핵심 요약 (Executive Summary)

본 가이드는 yq 핵심 가이드 및 실전 아키텍처의 핵심 개념, 실전 구성 명령어 및 아키텍처 베스트 프랙티스를 다룹니다. 클라우드 네이티브 및 인프라 엔지니어링 실무에 즉시 활용 가능하도록 체계적으로 정리했습니다.

📑 Table of Contents
Installation
Debian
yaml
apt install golang-go
# yq4 정상설치
go install github.com/mikefarah/yq/v4@latest
export PATH="$PATH:~/go/bin"
  • snapd
bash
# snap버그있으니 쓰지마셈
  snap install yq
  • apt
yaml
# 이상한버전 0.0.0 다운로드됨 쓰지마셈
  sudo add-apt-repository ppa:rmescandon/yq
  sudo apt update
  sudo apt install yq -y
run
bash
#json to Yaml
$stdout | yq -P

#yaml to JSON
$stdout | yq -P -pyaml -ojson

#helm values.yaml to props
cat values.yaml | yq -o props
flags
bash
-p, --input-format string           [auto|a|yaml|y|json|j|props|p|csv|c|tsv|t|xml|x|base64|uri|toml|lua|l] parse format for input. (default "auto")
  -o, --output-format string          [auto|a|yaml|y|json|j|props|p|csv|c|tsv|t|xml|x|base64|uri|toml|shell|s|lua|l] output format type. (default "auto")
File write
bash
# 수정결과를 표준출력
yq [opt] $file

# 기존 파일을 rewrite
yq -i [opt] $file
배열에 덮어쓰기
bash
yq -i '.mounts = [
	{location: "/etc/docker/certs.d/mydomain", writable: false}
]' $file

yq -i "$path = $array_items" $file
기존 배열에 아이템 병합하기
bash
yq -i '.mounts += [
	{location: "/etc/docker/certs.d/mydomain", writable: false}
]' $file

yq -i "$path += $array_items" $file
yq 표준 들여쓰기 적용
bash
myfile=template.yaml
cat $myfile | yq > /tmp/$myfile && cat /tmp/$myfile > $myfile
kube-manifest 변경
bash
tag=v3.0.10.1.33.54
currentApp=Enterprise Cloud-app
project=stg-portal
harborURL=harbor.Enterprise Cloud.hanacloudia.com
templateName=stg-cmp-Enterprise Cloud-app
# Enterprise Cloud-app
yq eval -i "(select(.kind == \"Deployment\" and .metadata.name == \"admin-$currentApp\") | .spec.template.spec.containers[0].image) = \"${harborURL}/${project}/admin-${currentApp}:${tag}\"" ${templateName}-admin/template.yaml
yq eval -i "(select(.kind == \"Deployment\" and .metadata.name == \"user-$currentApp\") | .spec.template.spec.containers[0].image) = \"${harborURL}/${project}/user-${currentApp}:${tag}\"" ${templateName}-user/template.yaml

tag=v3.0.10.1.33
currentApp=Enterprise Cloud-api
project=stg-portal
harborURL=harbor.Enterprise Cloud.hanacloudia.com
templateName=stg-cmp-common-si
# SI
yq eval -i "(select(.kind == \"Deployment\" and .metadata.name == \"$currentApp\") | .spec.template.spec.containers[0].image) = \"${harborURL}/${project}/${currentApp}:${tag}\"" ${templateName}/template.yaml

tag=v3.0.5.1.25
currentApp=openstack-api
project=stg-portal
harborURL=harbor.Enterprise Cloud.hanacloudia.com
templateName=stg-cmp-common-solution
# Solution
yq eval -i "(select(.kind == \"Deployment\" and .metadata.name == \"$currentApp\") | .spec.template.spec.containers[0].image) = \"${harborURL}/${project}/${currentApp}:${tag}\"" ${templateName}/template.yaml

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *