DevOps,  Python

Quick YAML Validation Without Installing `yamllint`

If Python and the PyYAML module are available, you can perform a simple YAML syntax check without installing yamllint:

Bash
python -c "import yaml; yaml.safe_load(open('main.yml')); print('YAML OK')"

If the file is valid YAML, the command prints:

Bash
YAML OK

If the parser encounters invalid YAML syntax, PyYAML raises an exception and reports the location of the error.

This approach verifies that the file can be successfully parsed, making it useful for quick checks in scripts, CI jobs, or restricted environments where additional tools cannot be installed.

However, it is important to understand that parsing is not the same as linting. PyYAML only checks whether the YAML syntax is valid. It does not detect style issues, formatting inconsistencies, duplicate keys, excessive line lengths, trailing spaces, or violations of project-specific conventions.

Tools such as yamllint provide more comprehensive validation by combining syntax checking with configurable style and quality rules. As a result, a file that passes the PyYAML check may still produce warnings or errors when analyzed with yamllint.

In summary:

  • PyYAML (yaml.safe_load): Verifies that the YAML document is syntactically valid and can be parsed.
  • yamllint: Verifies syntax and enforces formatting, style, and best-practice rules.

For quick syntax validation, the PyYAML approach is often sufficient. For production environments and collaborative projects, yamllint remains the preferred option.

Leave a Reply

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