Danny.G
Danny.G

Reputation: 83

Chef 13 syntax changes

Trying to upgrade chef client from version 12 to 13 I'm facing some issues mostly related to syntax changes

node['java']['webapps'].each do |object|
        name = object[0]
        attributes = object[1]
          if attributes.attribute?('enabled')
                if attributes.enabled
                        if attributes.attribute?('x')
                                if attributes.x.attribute?('conf')
                                        attributes.x.conf.each do |conf_file|
                                                template "#{x_conf_dir}/#{conf_file}" do
                                                  source "#{conf_file}.erb"
                                                  mode '0755'
                                                  owner 'tomcat'
                                                  group 'tomcat'
                                                end
                                        end
                                end
                        end
                end
        end
end

Chef client errors out on "if attributes.enabled" which on version 12 worked fine (it should continue if the enabled=true in the array)

now i'm hitting "undefined method 'enabled'"

What is the correct syntax for checking the value in the array ?

Upvotes: 0

Views: 78

Answers (1)

coderanger
coderanger

Reputation: 54267

That has to be attributes['enabled'] now, but that code can be massively cleaned up anyway. I think this does the same thing, using the node.read method which is like Hash#dig:

node['java']['webapps'].each do |name, app_attrs|
  conf_file = app_attrs.read('enabled', 'x', 'conf')
  if conf_file
    template .. same stuff here
  end
end

Any time you see that much diagonal indentation, probably rethink your code :)

Upvotes: 1

Related Questions