How do i check the syntax of my cookbook recipes?



  • Most configuration management tools have some kind of lint (syntac) checking, some are also build into editors.

    For chef there's a command line utility called cookstyle

    I have a recipe called default.rb with know syntax errors, let see what happens when you run cookstyle against it.

    $ cookstyle default.rb
    Inspecting 1 file
    C
    
    Offenses:
    
    default.rb:7:1: C: Missing space after #.
    #file "#{ENV['HOME']}/test.txt" do
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    default.rb:9:1: C: Missing space after #.
    #end
    ^^^^
    default.rb:15:1: C: Use 2 (not 3) spaces for indentation.
       source src_url
    ^^^
    default.rb:16:3: C: Inconsistent indentation detected.
      owner 'root'
      ^^^^^^^^^^^^
    default.rb:17:3: C: Inconsistent indentation detected.
      group 'root'
      ^^^^^^^^^^^^
    default.rb:18:3: C: Inconsistent indentation detected.
      mode '0640'
      ^^^^^^^^^^^
    
    1 file inspected, 6 offenses detected
    

    You can see it provides information messages to where all the errors are. In this case it want us to add spaces and correct indentation. After correcting all errors run cookstyle again.

    $ cookstyle default.rb
    Inspecting 1 file
    .
    
    1 file inspected, no offenses detected
    

    This time it runs without any errors.


Log in to reply
 

© Lightnetics 2024