MeWrite Docs

YAML syntax error

YAML形式のファイルで構文エラーが発生した場合

概要

YAML形式の設定ファイル(docker-compose.yml、.github/workflows/*.yml、kubernetes manifests等)で構文エラーが発生した場合のエラーです。

エラーメッセージ

yaml: line 10: mapping values are not allowed in this context
yaml: line 5: did not find expected key
Error: yaml: line 3: found character that cannot start any token
Error parsing YAML file: could not find expected ':'

原因

  1. インデントの問題: スペースとタブの混在、インデント数の不一致
  2. コロンの後のスペース: key:valueではなくkey: value
  3. 特殊文字のエスケープ: 引用符なしの特殊文字
  4. マルチラインの書き方: 複数行文字列の記法ミス
  5. ブール値/数値の意図しない解釈: yesnoonなどの予約語

解決策

1. インデントの修正

1
2
3
4
5
6
7
8
9
# Bad: タブを使用
services:
	web:    # タブ文字
		image: nginx

# Good: スペースを使用(2スペース推奨)
services:
  web:
    image: nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Bad: インデントが不揃い
services:
  web:
   image: nginx   # 1スペース
    ports:        # 4スペース
      - "80:80"

# Good: 一貫したインデント
services:
  web:
    image: nginx
    ports:
      - "80:80"

2. コロンの後にスペース

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Bad: スペースなし
name:John
age:30

# Good: スペースあり
name: John
age: 30

# Bad: 値にコロンがある場合
message: Error: Something went wrong

# Good: 引用符で囲む
message: "Error: Something went wrong"

3. 特殊文字のエスケープ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Bad: 特殊文字をそのまま使用
password: p@ss:word!
command: echo "hello" && echo "world"

# Good: 引用符で囲む
password: "p@ss:word!"
command: 'echo "hello" && echo "world"'

# アンカー記号 & を含む場合
url: "https://example.com?a=1&b=2"

4. マルチライン文字列

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# リテラルブロック(|)- 改行を保持
description: |
  This is a long description
  that spans multiple lines.
  Each line break is preserved.  

# 折りたたみブロック(>)- 改行をスペースに
description: >
  This is a long description
  that spans multiple lines.
  Lines are joined with spaces.  

# インデントを保持
script: |
  if [ -f file.txt ]; then
    echo "exists"
  fi  

5. ブール値と数値

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Bad: 意図しない型変換
country: NO      # ブール値として解釈される可能性
version: 1.0     # 浮動小数点として解釈
port: 080        # 8進数として解釈される可能性

# Good: 引用符で文字列を明示
country: "NO"
version: "1.0"
port: "080"

# 明示的なブール値
enabled: true
disabled: false

6. リストの書き方

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 複数行リスト
fruits:
  - apple
  - banana
  - orange

# インラインリスト
fruits: [apple, banana, orange]

# Bad: インデントなしのリスト
fruits:
- apple    # Error

# Good: 正しいインデント
fruits:
  - apple

7. ネストしたマップ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 正しいネスト
database:
  host: localhost
  port: 5432
  credentials:
    username: admin
    password: secret

# インライン形式
database: {host: localhost, port: 5432}

8. アンカーとエイリアス

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# アンカー定義
defaults: &defaults
  adapter: postgres
  host: localhost

# エイリアスで参照
development:
  <<: *defaults
  database: dev_db

production:
  <<: *defaults
  database: prod_db

9. null値

1
2
3
4
# null値の表現
value1: null
value2: ~
value3:    # 空の値もnull

10. GitHub Actionsの例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test
        env:
          CI: true

11. docker-compose.ymlの例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# docker-compose.yml
version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - "DATABASE_URL=postgres://user:pass@db:5432/app"
    depends_on:
      - db

  db:
    image: postgres:15
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: app
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  postgres-data:

12. バリデーションツール

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# yamllint をインストール
pip install yamllint

# YAMLファイルを検証
yamllint file.yml
yamllint -d relaxed file.yml

# Pythonで検証
python -c "import yaml; yaml.safe_load(open('file.yml'))"

# オンラインツール
# https://www.yamllint.com/

よくあるパターン

記号意味
``
>折りたたみブロック
&アンカー定義
*エイリアス参照
<<マージキー
~null

よくある間違い

  • タブとスペースを混在させる
  • コロンの後にスペースを入れ忘れる
  • 引用符なしで特殊文字を使う
  • yes/noを文字列として使いたいのに引用符を忘れる

関連エラー

参考リンク

YAML の他のエラー

最終更新: 2025-12-13