TExcelTAD

yamllint

A YAML parser tells you whether a file is valid. yamllint tells you whether it is good: consistent indentation, no duplicate keys, no tabs, no boolean-looking words like yes and on, no trailing spaces, sane line lengths. Many of the traps in YAML Gotchas are yamllint rules, and running it in CI stops them before a parser ever gets the chance to misread them. The sessions use yamllint 1.33.

A first run

bash · try it yourself
cat > values.yaml <<'YAML'
replicaCount: 2
image:
  repository: registry.example.com/shop-api
  tag: 1.4.2
service:
    type: ClusterIP
    port: 80
ingress:
  enabled: yes
replicaCount: 3
env:
- name: LOG_LEVEL
  value: info   
YAML
yamllint values.yaml; echo "-> status $?"
values.yaml
  1:1       warning  missing document start "---"  (document-start)
  6:5       error    wrong indentation: expected 2 but found 4  (indentation)
  9:12      warning  truthy value should be one of [false, true]  (truthy)
  10:1      error    duplication of key "replicaCount" in mapping  (key-duplicates)
  12:1      error    wrong indentation: expected 2 but found 0  (indentation)
  13:14     error    trailing spaces  (trailing-spaces)

-> status 1

Each line is line:column level message (rule). Here yamllint found:

  • the missing --- document start (a warning, and a rule many projects switch off);
  • 4-space indentation under service where the rest of the file uses 2;
  • the yes that a YAML 1.1 parser reads as a boolean (truthy, a warning by default);
  • the duplicated replicaCount: the second one silently wins in most parsers;
  • the env list written flush with its key, which the default indentation rule rejects (it can be relaxed, below);
  • trailing spaces after info.

Errors give exit status 1; warnings alone give 0 unless you add --strict.

Configuring the rules

The default configuration is strict about style. Most projects keep it and adjust a few rules in a .yamllint file at the repository root:

bash · try it yourself
cat > .yamllint <<'YAML'
extends: default
rules:
  document-start: disable          # most Kubernetes/Helm files omit ---
  line-length:
    max: 120
    level: warning
  indentation:
    spaces: 2
    indent-sequences: consistent   # allow "key:\n- item" and "key:\n  - item", but one style per file
  truthy:
    allowed-values: ["true", "false"]
    check-keys: false              # GitHub Actions uses "on:" as a key
  comments:
    min-spaces-from-content: 1
ignore: |
  charts/*/templates/
YAML
mkdir -p .github/workflows charts/shop/templates
cat > .github/workflows/ci.yaml <<'YAML'
name: ci
on:
  push:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v5
    - run: make test # unit tests
YAML
printf 'metadata:\n  name: {{ .Release.Name }}\n' > charts/shop/templates/deploy.yaml
yamllint . && echo "all YAML files pass"
echo "--- machine-readable output for CI annotations"
printf 'enabled: on\n' > flags.yaml
yamllint -f parsable flags.yaml; echo "-> status $?"
all YAML files pass
--- machine-readable output for CI annotations
flags.yaml:1:10: [warning] truthy value should be one of [false, true] (truthy)
-> status 0
  • yamllint . finds YAML files recursively (*.yaml, *.yml, .yamllint) and applies .yamllint from the current directory.
  • ignore takes gitignore-style patterns. Helm templates are not YAML until they are rendered: lint the chart with helm lint, or lint the output of helm template, but not the template files themselves.
  • check-keys: false keeps the truthy rule for values while accepting on: as a key.
  • -f parsable prints file:line:col: [level] message (rule), one per line; -f github prints GitHub Actions annotation commands so findings appear on the pull request diff.

Disabling a rule for one line

yaml
password: !vault |  # yamllint disable-line rule:line-length
  $ANSIBLE_VAULT;1.1;AES256;61626364656667686970717273747576777879...

# yamllint disable rule:truthy
legacy_flag: yes
# yamllint enable rule:truthy

As with ShellCheck, disable the specific rule on the specific line and leave a reason nearby.

Where yamllint fits

Check Tool Catches
is it YAML at all? any parser, yq . file syntax errors
is it good YAML? yamllint style, duplicates, truthy values, tabs
does it have the right shape? JSON Schema: check-jsonschema, kubeconform missing fields, wrong types, unknown keys
does it make sense? the tool itself: helm lint, kubectl apply --dry-run=server, terraform validate semantic errors

The next lesson, JSON Schema Checks, adds the third row.

Test yourself

Which yamllint rule flags enabled: yes?

  1. indentation
  2. truthy
  3. key-duplicates
  4. document-start

Why should a .yamllint config usually ignore charts/*/templates/?

  1. Helm forbids linting
  2. Helm templates contain Go template syntax and are not valid YAML until rendered
  3. yamllint cannot read subdirectories
  4. Templates are always correct

With the default settings, what exit status does yamllint return if it finds only warnings?

  1. 0
  2. 1
  3. 2
  4. It depends on the file count