[Solved] Chef Hash not recongnized


#1

Hi,

The field attribute of log_forward is defined like this:

attribute :fields, kind_of: Hash, default: {}, required: true

And I’m trying to pass a hash value:

   log_forward 'logback-bo-batch' do
	    paths ['/var/log/urms/cgp.bo-batch.log']
	    fields  {
           'type' => 'logback',
           'component' => 'bo-batch',
           'environment' => node.chef_environment
        }
    end

At compilation it says it expects ‘}’ istead of ‘=>’ (at the first => in ‘type’ => ‘logback’)
It does not matter what I change it just does not get the hash :confused:

I read once something with chef hash and ruby hash, is there a difference?
Did I use a wrong hash and how does the hash to use look like?

Thanks for any help!! :slight_smile:


#2

This is an ambiguity in ruby syntax. The squiggly brackets are used for both Hash literals and for code blocks (aka Procs aka anonymous functions). When you call a method and do not use parentheses around the arguments, the squiggly brackets are interpreted as starting a code block section instead of a Hash. The two solutions are to either add parentheses, like fields( { 'key' => 'value' } ) or omit the squiggly brackets, like fields 'key' => 'value'

HTH


#3

Thank you very much, I did try a lot of things but not that…
:+1::+1: