Puppetの文法チェックまとめ。
マニフェスト(pp)とテンプレート(erb)とHiera(yaml)。
puppet実行時エラーだと、シンタックスエラーが原因なのが分かりにくい場合があるのでメモ。
マニフェスト(pp)
*.pp
puppet parser validate example.pp
エラー例
[root@web01 manifests]# puppet parser validate site.pp
Error: Could not parse for environment production: Syntax error at '}' at /etc/puppet/manifests/site.pp:9
テンプレート(erb)
*.erb
erb -P -x -T '-' example.erb | ruby -c
エラー例
<% %>を閉じ忘れた例ですが、どの行がエラーか分からない。。。壊れていないかのチェックぐらい?
[root@web01 web01]# erb -P -x -T '-' httpd.conf.erb | ruby -c
-:59: syntax error, unexpected *
Include conf.modules.d/*.conf
^
-:105: syntax error, unexpected '<', expecting end-of-input
<Directory />
^
Hiera(yaml)
*.yaml
ruby -e "require 'yaml'; YAML.load_file('example.yaml')"
エラー例
[root@web01 hieradata]# ruby -e "require 'yaml'; YAML.load_file('example.yaml')"
/usr/share/ruby/psych.rb:205:in `parse': (example.yaml): could not find expected ':' while scanning a simple key at line 8 column 1 (Psych::SyntaxError)
from /usr/share/ruby/psych.rb:205:in `parse_stream'
from /usr/share/ruby/psych.rb:153:in `parse'
from /usr/share/ruby/psych.rb:129:in `load'
from /usr/share/ruby/psych.rb:299:in `block in load_file'
from /usr/share/ruby/psych.rb:299:in `open'
from /usr/share/ruby/psych.rb:299:in `load_file'
from -e:1:in `<main>'
一括チェック
findコマンド結果で、上記コマンドをループ実行して一括チェック。
*.pp
find /etc/puppet/ -name "*.pp" | while read file; do echo "--- file=$file"; puppet parser validate "$file"; done
*.erb
find /etc/puppet/modules/ -name "*.erb" | while read file; do echo "--- file=$file"; erb -P -x -T '-' "$file" | ruby -c ; done
*.yaml
find /etc/puppet/hieradata/ -name "*.yaml" | while read file; do echo "--- file=$file"; ruby -e "require 'yaml'; YAML.load_file('$file')"; done