yq
は、YAMLファイルをコマンドライン上で読み書き・変換できるツールです。
JSONの操作に jq
を使うのと同じように、yq
を使えば YAML ファイルの内容を簡単に抽出・更新・変換できます。
公式ドキュメント
詳しい使い方や機能については、公式GitHubリポジトリをご覧ください
インストール方法
Macでは、以下のようにHomebrewで簡単にインストールできます。
brew install yq
インストール確認
yq --version
バージョンが表示されればインストール成功です
基本的な使い方
例えば inventory.yml
というyamlファイルがあったとします。
all:
children:
platform:
hosts:
platform-proxy:
platform-store:
services:
children:
egg_yolk_ruxt_01:
children:
production:
hosts:
egg-yolk-ruxt-01-production-web-1:
egg-yolk-ruxt-01-production-web-2:
egg-yolk-ruxt-01-production-batch-1:
test:
hosts:
egg-yolk-ruxt-01-test-web-1:
egg-yolk-ruxt-01-test-web-2:
egg-yolk-ruxt-01-test-batch-1:
.all
を指定して中身を確認
yq '.all' inventory.yml
出力例:
children:
platform:
hosts:
platform-proxy:
platform-store:
services:
children:
egg_yolk_ruxt_01:
children:
production:
hosts:
egg-yolk-ruxt-01-production-web-1:
egg-yolk-ruxt-01-production-web-2:
egg-yolk-ruxt-01-production-batch-1:
test:
hosts:
egg-yolk-ruxt-01-test-web-1:
egg-yolk-ruxt-01-test-web-2:
egg-yolk-ruxt-01-test-batch-1:
.all.children
を見る
yq '.all.children' inventory.yml
出力例:
platform:
hosts:
platform-proxy:
platform-store:
services:
children:
egg_yolk_ruxt_01:
children:
production:
hosts:
egg-yolk-ruxt-01-production-web-1:
egg-yolk-ruxt-01-production-web-2:
egg-yolk-ruxt-01-production-batch-1:
test:
hosts:
egg-yolk-ruxt-01-test-web-1:
egg-yolk-ruxt-01-test-web-2:
egg-yolk-ruxt-01-test-batch-1:
.all.children.platform
を見る
yq '.all.children.platform' inventory.yml
出力例:
hosts:
platform-proxy:
platform-store:
.all.children.platform.hosts
を見る
yq '.all.children.platform.hosts' inventory.yml
出力例:
platform-proxy:
platform-store:
まとめ
手動でYAMLを編集する手間を省き、構成管理の自動化に役立ててください!