Table of contents
1.
Introduction
2.
ActionMethodInResource
2.1.
Examples
3.
AllowedActionsFromInitialize
3.1.
Examples
4.
ChefGemNokogiri
4.1.
Examples
5.
ClassEvalActionClass
5.1.
Examples
6.
ConditionalUsingTest
6.1.
Examples
7.
CronDFileOrTemplate
7.1.
Examples
8.
CronManageResource
8.1.
Examples
9.
CustomResourceWithAttributes
9.1.
Examples
10.
DatabagHelpers
10.1.
Examples
11.
DeclareActionClass
11.1.
Examples
12.
DefaultActionFromInitialize
12.1.
Examples
13.
DefinesChefSpecMatchers
13.1.
Examples
14.
Definitions
15.
DependsOnChefVaultCookbook
15.1.
Examples
16.
DependsOnChocolateyCookbooks
16.1.
Examples
17.
DependsOnKernelModuleCookbook
17.1.
Examples
18.
DependsOnLocaleCookbook
18.1.
Examples
19.
DependsOnOpensslCookbook
19.1.
Examples
20.
Frequently Asked Questions
20.1.
What is Cookstyle?
20.2.
Which command is used for Autocorrecting Cookstyle Warnings ?
20.3.
What is RuboCop?
21.
Conclusion
Last Updated: Aug 13, 2025

CookStyle Cops - Chef/Modernize Section

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Cookstyle is a code linting tool that aids in the improvement of your Chef Infra cookbooks by identifying and automatically fixing any errors in style, grammar, and logic. Cookstyle was created to make it possible to upgrade the RuboCop engine while maintaining backward compatibility with the configuration and avoiding the need to update all of the recipes. Any new cops in the file that are enabled or disabled must have comments explaining the justification for the change.

This blog explains the details of CookStyle Cops - Chef/Modernize Section along with the details of its various departments and examples.

Without further ado, let's get started.

Chef Image

ActionMethodInResource

  • Department : ActionMethodInResource
     
  • Full name of the cop: Chef/Modernize/ActionMethodInResource
     

It is enabled by default and supports autocorrection. 
Instead of constructing actions with methods, use the action:my_action blocks of the custom resource language.

Examples

Incorrect:

def action_create
 # :create action code here
end

 

Correct:

action:create do
 # :create action code here
end

AllowedActionsFromInitialize

  • Department : AllowedActionsFromInitialize
     
  • Full name of the cop: Chef/Modernize/AllowedActionsFromInitialize
     

It is enabled by default and supports autocorrection. 
Instead of utilising the @actions or @allowed actions variables in the resource's initialise method, the allowed_actions can now be defined using the allowed actions helper. In general, we advise against developing HWRPs, but if you must, you should make the most of the resource DSL.

Examples

Incorrect:

def initialize(*args)
  super
  @actions = [ :create, :add ]
end


# also bad
def initialize(*args)
  super
  @allowed_actions = [ :create, :add ]
end

 

Correct:

allowed_actions [ :create, :add ]

ChefGemNokogiri

  • Department : ChefGemNokogiri
     
  • Full name of the cop: Chef/Modernize/ChefGemNokogiri
     

It is enabled by default and supports autocorrection. 
Chef Infra Client 12+ includes the nokogiri gem out of the box; it does not require installation to utilise.

Examples

Incorrect:

chef_gem 'nokogiri'

ClassEvalActionClass

  • Department : ClassEvalActionClass
     
  • Full name of the cop: Chef/Modernize/ClassEvalActionClass
     

It is enabled by default and supports autocorrection. 
It is no longer essential to call the class eval method on the action class block in Chef Infra Client 12.9 and later.

Examples

Incorrect:

action_class.class_eval do
  foo
end

 

Correct:

action_class do
  foo
end

ConditionalUsingTest

  • Department : ConditionalUsingTest
     
  • Full name of the cop: Chef/Modernize/ConditionalUsingTests
     

It is enabled by default and supports autocorrection. 
Use ::File.exist? ('/foo/bar') rather than the slower, more shell out 'test -f /foo/bar'

Examples

Incorrect:

only_if 'test -f /bin/foo'

 

Correct:

only_if { ::File.exist?('bin/foo') }

CronDFileOrTemplate

  • Department : CronDFileOrTemplate
     
  • Full name of the cop: Chef/Modernize/CronDFileOrTemplate
     

It is enabled by default but does not support autocorrection. 
Instead of manually creating the file with the template, file, or cookbook_file resources, use the cron_d resource that is included with Chef Infra Client 14.4+.

Examples

Incorrect:

template '/etc/cron.d/backup' do
  source 'cron_backup_job.erb'
  owner 'root'
  group 'root'
  mode '644'
end

cookbook_file '/etc/cron.d/backup' do
  owner 'root'
  group 'root'
  mode '644'
end

file '/etc/cron.d/backup' do
  content '*/30 * * * * backup /usr/local/bin/backup_script.sh'
  owner 'root'
  group 'root'
  mode '644'
end

file '/etc/cron.d/blogs' do
  action :delete
end

file "/etc/cron.d/#{job_name}" do
  action :delete
end

file File.join('/etc/cron.d', job) do
  action :delete
end

file 'delete old cron job' do
  path '/etc/cron.d/backup'
  action :delete
end

file 'delete old cron job' do
  path "/etc/cron.d/#{job}"
  action :delete
end

file 'delete old cron job' do
  path ::File.join('/etc/cron.d', job)
  action :delete
end


Correct:

cron_d 'backup' do
  minute '1'
  hour '1'
  mailto 'sysadmins@example.com'
  command '/usr/local/bin/backup_script.sh'
end
cron_d 'blogs' do
  action :delete
end

CronManageResource

  • Department : CronManageResource
     
  • Full name of the cop: Chef/Modernize/CronManageResource
     

It is enabled by default and supports autocorrection. The target chef version is 14.4+.
In the cron cookbook's 6.1 release and later included in Chef Infra Client 14.4, the cron_manage resource was changed to cron_access. Use the updated resource name.

Examples

Incorrect:

cron_manage 'mike'

 

Correct:

cron_access 'mike'

CustomResourceWithAttributes

  • Department : CustomResourceWithAttributes
     
  • Full name of the cop: Chef/Modernize/CustomResourceWithAttributes
     

It is enabled by default and supports autocorrection.
You defined attributes in HWRPs and LWRPs, but custom resources altered the name of the concept to properties in order to avoid confusion with chef recipe attributes. Despite the fact that they are aliased, they should be referred to as properties when constructing a custom resource.

Examples

Incorrect:

attribute :something, String
action :create do
  # some action code because we're in a custom resource
end

 

Correct:

property :something, String
action :create do
  # some action code because we're in a custom resource
end

DatabagHelpers

  • Department : DatabagHelpers
     
  • Full name of the cop: Chef/Modernize/DatabagHelpers
     

It is enabled by default and supports autocorrection.
Instead of using Chef::DataBagItem.load or Chef::EncryptedDataBagItem.load, use the data_bag_item helper.

Examples

Incorrect:

plain_text_data = Chef::DataBagItem.load('foo', 'bar')
encrypted_data = Chef::EncryptedDataBagItem.load('foo2', 'bar2')

 

Correct:

plain_text_data = data_bag_item('foo', 'bar')
encrypted_data = data_bag_item('foo2', 'bar2')

DeclareActionClass

  • Department : DeclareActionClass
     
  • Full name of the cop: Chef/Modernize/DeclareActionClass
     

It is enabled by default and supports autocorrection. The target Chef version is 12.9+.
The action_class can be used in place of declare_action_class with Chef Infra Client 12.9 and later.

Examples

Incorrect:

declare_action_class do
  foo
end


Correct:

action_class do
  foo
end

DefaultActionFromInitialize

  • Department : DefaultActionFromInitialize
     
  • Full name of the cop: Chef/Modernize/DefaultActionFromInitialize
     

It is enabled by default and supports autocorrection.
The @action variable in the resource provider initialise method can now be replaced with the default action helper to specify default_actions. In general, we advise against developing HWRPs, but if you must, you should make the most of the resource DSL.

Examples

Incorrect:

def initialize(*args)
  super
  @action = :create
end


Correct:

default_action :create

DefinesChefSpecMatchers

  • Department : DefinesChefSpecMatchers
     
  • Full name of the cop: Chef/Modernize/DefinesChefSpecMatchers
     

It is enabled by default and supports autocorrection.
Later versions( ChefSpec 7.1+) of ChefSpec automatically create ChefSpec matchers. Cookbooks can now have their matchers deleted.

Examples

Incorrect:

if defined?(ChefSpec)
  def create_yum_repository(resource_name)
    ChefSpec::Matchers::ResourceMatcher.new(:yum_repository, :create, resource_name)
  end
end

Definitions

  • Department : Definitions
     
  • Full name of the cop: Chef/Modernize/Definitions
     

It is enabled by default but does not support auto correction.
Custom Resources were introduced in 2016 with Chef Infra Client 12.5 as a mechanism to create reusable resource code that could be distributed in cookbooks. In addition to unit testing using ChefSpec, input validation, actions, common attributes like not_if/only_if, and resource reporting, custom resources have several advantages over historical definitions.

DependsOnChefVaultCookbook

  • Department : DependsOnChefVaultCookbook
     
  • Full name of the cop: Chef/Modernize/DependsOnChefVaultCookbook
     

It is enabled by default and supports autocorrection. The target chef version in 16.0.
It is independent of the Chef Infra Client 16.0-outdated Chef Vault cookbook. The Chef Infra Client now includes the Chef Vault Gem and Helpers.

Examples

Incorrect:

depends 'chef-vault'

DependsOnChocolateyCookbooks

  • Department : DependsOnChocolateyCookbooks
     
  • Full name of the cop: Chef/Modernize/DependsOnChocolateyCookbooks
     

It is enabled by default and supports autocorrection. The target chef version in 14.3+.
It is independent of the outdated Chef Infrastructure Client 14.3 cookbooks chocolatey_source and chocolatey_config. The Chef Infra Client now includes the chocolatey_source and chocolatey_config resources.

Examples

Incorrect:

depends 'chocolatey_source'
depends 'chocolatey_config'

DependsOnKernelModuleCookbook

  • Department : DependsOnKernelModuleCookbook
     
  • Full name of the cop: Chef/Modernize/DependsOnKernelModuleCookbook
     

It is enabled by default and supports autocorrection. The target chef version in 14.3+.
It is independent of the kernel_module cookbook, which Chef Infra Client 14.3 rendered obsolete. The Chef Infra Client now contains the kernel_module resource.

Examples

Incorrect:

depends 'kernel_module'

DependsOnLocaleCookbook

  • Department :DependsOnLocaleCookbook
  • Full name of the cop: Chef/Modernize/DependsOnLocaleCookbook

It is enabled by default and supports autocorrection. The target chef version in 14.5+.

It is independent of the local cookbook, which Chef Infra Client 14.5 rendered obsolete. The Chef Infra Client now contains the locale resource.

Examples

Incorrect:

depends 'locale'

DependsOnOpensslCookbook

  • Department :DependsOnOpensslCookbook
     
  • Full name of the cop: Chef/Modernize/DependsOnOpensslCookbook
     

It is enabled by default and supports autocorrection. The target chef version in 14.4+.
It is independent on the openssl cookbook, which Chef Infra Client 14.4 rendered obsolete. Now, Chef Infra Client directly includes all openssl_* resources.

Examples

Incorrect:

depends 'openssl'

Frequently Asked Questions

What is Cookstyle?

Cookstyle is a code linting tool that aids in the improvement of your Chef Infra cookbooks by identifying and automatically fixing any errors in style, grammar, and logic. 

Which command is used for Autocorrecting Cookstyle Warnings ?

The cookstyle -a command is used for Autocorrecting Cookstyle Warnings.

What is RuboCop?

RuboCop is a powerful and adaptable Ruby code style checker and formatter that can find and, in many situations, correct issues in your code.

Conclusion

In this article, we have extensively discussed the details of CookStyle Cops - Chef/Modernize Section along with the details of its various departments and examples.

We hope that this blog has helped you enhance your knowledge regarding CookStyle Cops - Chef/Modernize Section, and if you would like to learn more, you can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!!

Thank You image
Live masterclass