These days I am working more on the chef to automate infrastructure. The Opscode’s chef is an excellent tool among Puppet, Ansible, and Saltstack. When compared to other tools chef have more learning curve but once you get the flow you love it.

The chef is a pull-based configuration management tool that said it pull configuration changes onto the node. Sometimes you want to pull specific cookbook version instead of default version mention in chef environment.

We can use the chef-client command on node machine to run once with just our desired cookbook instead of all cookbook available to that node. In this post, we will see how to accomplish with some examples. for another chef post please click here.

Run complete set of cookbooks on chef node

In order to run all the cookbooks meant to run on the machine, we can use –once to pull all updated cookbooks and run once. This is a kind of manual apply of cookbook changes whenever you require.

chef-client --once

Example:

Clipped output:

[root@linuxnix ~]# chef-client --once
Starting Chef Client, version 12.21.3
resolving cookbooks for run list: ["surendra_continuous_compliance"]
Synchronizing Cookbooks:
- surendra_file_utils (0.1.3)
- surendra_system_utils (1.0.1)
- surendra_hardening (1.5.16)
- surendra_hardening (1.6.13)
- surendra_continuous_compliance (0.3.11)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 355 resources
Recipe: surendra_continuous_compliance::default
* log[Starting continuous compliance. Run number: 13] action write

Recipe: surendra_continuous_compliance::hardening
* file[create pre-run sshd_config md5sum] action create (up to date)
Recipe: surendra_hardening::packages

Chef Client finished, 58/385 resources updated in 36 seconds

Run a specific cookbook on chef node

The below command just run only one specific cookbook and an up to date cookbook which is set in chef environment.

chef-client --once -o my_cook_book

Example:

Clipped output:

[root@linuxnix ~]# chef-client --once -o surendra_splunk
Starting Chef Client, version 12.21.3
resolving cookbooks for run list: ["surendra_splunk"]
Synchronizing Cookbooks:
- mec_nix_file_utils (0.1.3)
- surendra_functional_id (0.1.9)
- surendra_splunk (0.1.6)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 8 resources
Recipe: surendra_splunk::prereqs
* surendra_functional_id_user[splunk] action create (up to date)
* directory[/opt/splunkforwarder] action create
- change mode from '0755' to '0750'

Run a  cookbook specific version on chef node

Sometimes you may require lesser versions of a cookbook, we can use below command to specify a particular version.

chef-client --once -o "recipe[my_cook_book@my_version]"

Output:

Clipped output:

[root@linuxnix ~]# chef-client --once -o 'recipe[surendra_mqclient@1.0.0]'
Starting Chef Client, version 12.21.3
resolving cookbooks for run list: ["surendra_mqclient@1.0.0"]

Note: Make sure the version of the cookbook which you are running is less than or equal to version mentioned in chef environment. If you run a cookbook version which is v1.2.3, but the version mention in chef environment is 1.2.2 then this cookbook will not run.

Where can I set cookbook version?

All cookbooks will have the metadata.rb file in its main folder which contains the version of that specific cookbook.

How can I update cookbook version in chef environment?

Use the knife command to update chef environment by using below command.

knife environment edit chef_environment_number

Updated the file and save it. Then run chef-client –once on remote chef node to get this updated cookbook version code.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.