Ask Your Question
2

Does Puppet provide a validation/syntax checker for Hiera?

asked 2014-06-27 19:39:16 -0500

The tool puppet parser validate will check the syntax of my Puppet manifests:

[root@puppet3 ~]# puppet parser validate /etc/puppet/manifests/site.pp
Warning: The use of 'import' is deprecated at /etc/puppet/manifests/site.pp:18. See http://links.puppetlabs.com/puppet-import-deprecation
   (at grammar.ra:610:in `_reduce_190')
Error: Could not parse for environment production: No file(s) found for import of 'nodes/*.pp' at /etc/puppet/manifests/site.pp:18
[root@puppet3 ~]#

And I can check my manifests against the Puppet Style Guide using puppet-lint:

[root@puppet3 ~]# puppet-lint /etc/puppet/modules/hosts/manifests/init.pp 
WARNING: class not documented on line 1
[root@puppet3 ~]#

Is there a Puppet validation tool for Hiera or for my YAML files?

edit retag flag offensive close merge delete

6 Answers

Sort by ยป oldest newest most voted
5

answered 2014-06-29 05:42:49 -0500

daff gravatar image

A dead-simple way to check a YAML file for correct syntax is parsing the file with your favourite scripting language. Here's a Ruby one-liner:

ruby -e "require 'yaml'; YAML.load_file('common.yaml')"

If the YAML file is valid there will be no output, otherwise you'll get exceptions of type Psych::SyntaxError.

edit flag offensive delete link more

Comments

And, if I wanted to do this for all files in my Hiera datadir, I could do something like this: `find /etc/puppet/hieradata/ -name "*.yaml" | while read file; do echo "file=$file"; ruby -e "require 'yaml'; YAML.load_file('$file')"; done`

stefanlasiewski gravatar imagestefanlasiewski ( 2014-07-02 18:47:27 -0500 )edit

Unfortunately, this still doesn't catch typos which trigger a failure with `puppet agent --test`. This one, for example: https://ask.puppetlabs.com/question/10999/hiera-how-can-i-tell-which-class-triggered-expected-array-and-got-nilclass/

stefanlasiewski gravatar imagestefanlasiewski ( 2014-07-02 19:00:29 -0500 )edit
1

As I said, this verifies correct YAML syntax, nothing more. Supplying data types incompatible with Hiera lookup functions is not a YAML syntax error. Neither is looking up non-existent keys or keys with empty values. Take care of such cases by using default values and conditionals where appropriate.

daff gravatar imagedaff ( 2014-07-03 05:54:02 -0500 )edit

My error was caused by an empty `classes:` array. Does that not count as a invalid YAML, or is that more of an error with Puppet/Hiera?

stefanlasiewski gravatar imagestefanlasiewski ( 2014-07-08 13:59:03 -0500 )edit
1

answered 2016-12-22 10:40:48 -0500

jack9 gravatar image

for simple checks i use yaml-lint

https://github.com/Pryz/yaml-lint

gem install yaml-lint
yaml-lint filename.yaml
yaml-lint hiera/
edit flag offensive delete link more
0

answered 2014-10-31 14:35:48 -0500

I'm looking at the Kwalify gem for this. You basically define a schema for your hiera yaml files and then it validates against the schema. Makes for a good CI workflow.

edit flag offensive delete link more
0

answered 2016-05-06 22:57:29 -0500

mike.r gravatar image

I wrapped this around

ruby -e "require 'yaml'; YAML.load_file('common.yaml')"

in a bash script to parse a dir and output any files w bad syntax.

Script: https://github.com/perfecto25/bash_sc...

edit flag offensive delete link more
0

answered 2016-12-23 07:40:16 -0500

DarylW gravatar image

In some of your comments, it seems like you want to validate that your hiera data can compile your manifests.

I asked a similar question at https://ask.puppet.com/question/26418... You can loop through all of your roles and do a 'it { should compile.withalldeps } across all of your roles

edit flag offensive delete link more
0

answered 2014-06-28 08:04:57 -0500

starkb gravatar image

There are a couple one is yamllint.com , but in the class they have you use validate_yaml. You can do a search on this on Google and get a lot more info.

edit flag offensive delete link more

Comments

To be clear, that is a shell function we use in the class.

validate_yaml() {
      ruby -ryaml -e "YAML.load_file '$1'"
}

binford2k gravatar imagebinford2k ( 2015-08-20 11:31:34 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-06-27 19:39:16 -0500

Seen: 8,564 times

Last updated: Dec 23 '16