Getting Started with Chef and Chef-solo
Chef (the systems integration framework) provides a standalone tool called chef-solo. With chef-solo you can run locally chef recipes without the need for a centralised server (Chef master). This is great for getting started with chef, testing recipes or when you just don't want a centralised Chef master.
In this article we'll go through a sample chef-solo use case.
Installing chef-solo
First, you need chef-solo which is part of the standard chef client package. For simplicity, let's assume we are using Ubuntu / Debian where you can you can install the chef package with the apt-get like this:
sudo-apt-get install chef
For other operating systems and distributions (such as SuSE), chef is not available in the default repos. Because of that, you will have to download and install chef client from the official download page. Note that there are also installation packages for Chef server and Chef DK which have more than just the chef client.
Creating a test recipe
Even though chef-solo is simple and straightforward, it still requires to a minimal configuration. It needs to know:
The filesystem path to the cookbooks
The contents of a recipe
First, the cookbook path should be specified in a configuration file, for example solo.rb. Let's assume we are working in a directory called /home/myuser/chef. Then, the configuration file should be called /home/myuser/chef/solo.rb and it should contain:
cookbook_path '/home/myuser/chef/cookbooks'
Second, let's assume we want to start with a recipe called 'test'. It should be placed inside our cookbooks having the path '/home/myuser/chef/cookbooks/test/'. The simplest possible recipe would be having one file and printing 'Hello, Chef'. To accomplish this, create a file called '/home/myuser/chef/cookbooks/test/recipes/default.rb' and place inside of it the following Ruby code:
puts "Hello, Chef"
Running the test recipe
To run the test recipe, go inside the '/home/myuser/chef/' directory and execute chef-solo specifying the path to the config file and the name of the test recipe like with these arguments:
chef-solo -c /home/myuser/chef/solo.rb -o 'recipe[test]'
Note: You can run the above recipe with a regular user because in our case we are just printing text. If you want to make system changes, you will have to run chef-solo with sudo / root privileges.
If you have followed the instructions correctly, you should see something similar to:
Starting Chef Client, version 13.8.5
[2018-04-03T10:14:14+03:00] WARN: Run List override has been provided.
[2018-04-03T10:14:14+03:00] WARN: Original Run List: []
[2018-04-03T10:14:14+03:00] WARN: Overridden Run List: [recipe[test]]
resolving cookbooks for run list: ["test"]
Synchronizing Cookbooks:
- test (0.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Hello, Chef ### Our text is shown here. ###
Converging 0 resources
[2018-04-03T10:14:14+03:00] WARN: Skipping final node save because override_runlist was given
Running handlers:
Running handlers complete
Chef Client finished, 0/0 resources updated in 05 seconds
As you could see, the above printed 'Hello, Chef' completing the recipe successfully.
Conclusion
This is how simple it is to get started with Chef with the help of chef-solo. With no configuration at all you can start writing and testing your Chef recipes.