0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🚀 Macにyqをインストール

Posted at

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を編集する手間を省き、構成管理の自動化に役立ててください!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?