Skip to content

Instantly share code, notes, and snippets.

@xiii
Last active April 20, 2020 06:10
Show Gist options
  • Save xiii/96381e41f66e5cf6a72d to your computer and use it in GitHub Desktop.
Save xiii/96381e41f66e5cf6a72d to your computer and use it in GitHub Desktop.
Validate YAML in ruby - useful for hiera
#!/bin/bash
#
# Efstathios Xagoraris <sxagoraris@gmail.com>
# Validate YAML files using ruby
#
if [ $# -eq 0 ]
then
echo "Please provide a yaml file as argument eg $0 file.yaml"
exit 1
fi
ruby -ryaml -e "YAML.parse(File.open('${1}'))"
if [[ $? -ne 0 ]]
then
echo "$1 is not valid YAML"
exit 1
else
echo "$1 is a valid YAML"
fi
@circa10a
Copy link

Alternative solution

#!/usr/bin/env ruby

require 'yaml'

Dir.glob('**/*{yml,yaml}') { |file|
    begin
        YAML.parse(File.open(file))
        puts "#{file} \e[32mvalid\e[0m"
    rescue => exception
        puts "#{file} \e[31minvalid\e[0m"
        fail
    end
}

@xiii
Copy link
Author

xiii commented Apr 20, 2020

Looks much better! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment