Pages

Friday, September 14, 2018

Working with Chef-Solo

Chef-solo is a command that executes chef-client in a way that does not require the chef-server communication. That is when ever we have written a cookbook we usually upload them first to the chef-server, then use the chef-client command to execute. The chef-client download any changes ( cookbook also ) to the chef-node and then use them to perform any changes.

What if we want to run our cookbooks locally rather then downloading from chef-server. This is where chef-solo comes into use. Chef-solo uses chef-client “chef local mode” to run the cookbooks. While running cookbooks with chef-solo, the authentication, authorization etc functionalities will not be available to the chef-client.

How to use the chef-solo
Chef-solo requires 2 files to be available in order to run cookbooks ,
Solo.rb – File tells chef about the location of the cookbooks, roles and data bags
Node.json – file sets the run list and any node specific attributes if necessary

Lets create the files in chef-repo ,

[root@chefdk chef-repo]# cat solo.rb
current_dir       = File.expand_path(File.dirname(__FILE__))
file_cache_path   "#{current_dir}"
cookbook_path     "#{current_dir}/cookbooks"
role_path         "#{current_dir}/roles"
data_bag_path     "#{current_dir}/data_bags"
# chef-solo

[root@chefdk chef-repo]# cat node.json
{
    "run_list": [ "recipe[simple]" ]
}

From the above code , we can see the solo.rb talks about the location of cookbooks, roles and databags. The node.json file talks about the cookbooks that we want to execute locally.

Now upload the code to the git repository with all the chef-repo. Now from the chef-client node ( machine where we want to run the chef-client ) clone the git repo. From inside the chef-repo, we will run the chef-solo command as  below.

[root@chefclient chef-repo]# sudo chef-solo -c solo.rb -j node.json
[2018-09-07T04:36:13-04:00] INFO: Forking chef instance to converge...
Starting Chef Client, version 12.22.5
[2018-09-07T04:36:13-04:00] INFO: *** Chef 12.22.5 ***
[2018-09-07T04:36:13-04:00] INFO: Platform: x86_64-linux
[2018-09-07T04:36:13-04:00] INFO: Chef-client pid: 16351
[2018-09-07T04:36:13-04:00] INFO: The plugin path /etc/chef/ohai/plugins does not exist. Skipping...
[2018-09-07T04:36:16-04:00] INFO: HTTP Request Returned 404 Not Found: Object not found: chefzero://localhost:8889/nodes/chefclient.nova.com
[2018-09-07T04:36:16-04:00] INFO: Setting the run_list to ["recipe[simple]"] from CLI options
[2018-09-07T04:36:16-04:00] INFO: Run List is [recipe[simple]]
[2018-09-07T04:36:16-04:00] INFO: Run List expands to [simple]
[2018-09-07T04:36:16-04:00] INFO: Starting Chef Run for chefclient.nova.com
[2018-09-07T04:36:16-04:00] INFO: Running start handlers
[2018-09-07T04:36:16-04:00] INFO: Start handlers complete.
[2018-09-07T04:36:16-04:00] INFO: HTTP Request Returned 404 Not Found: Object not found:
resolving cookbooks for run list: ["simple"]
[2018-09-07T04:36:16-04:00] INFO: Loading cookbooks [simple@0.1.0]
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/README.md from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/example/README.md from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/example/attributes/default.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/example/metadata.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/example/recipes/default.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/Berksfile from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/README.md from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/chefignore from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/files/index.html from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/metadata.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/recipes/default.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/spec/spec_helper.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/spec/unit/recipes/default_spec.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/test/recipes/default_test.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/nginx/CHANGELOG.md from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/nginx/README.md from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/nginx/metadata.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/nginx/recipes/default.rb from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/.gitignore from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/httpd/.kitchen.yml from the cache; its cookbook is no longer needed on this client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/simple/spec/spec_helper.rb from the cache; its is no longer in the cookbook manifest.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/simple/spec/unit/recipes/default_spec.rb from the cache; its is no longer in the cookbook manifest.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/simple/test/recipes/default_test.rb from the cache; its is no longer in the cookbook manifest.
Synchronizing Cookbooks:
  - simple (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
[2018-09-07T04:36:16-04:00] INFO: HTTP Request Returned 404 Not Found: Object not found:
Converging 1 resources
Recipe: simple::default
  * file[/tmp/x.txt] action create[2018-09-07T04:36:16-04:00] INFO: Processing file[/tmp/x.txt] action create (simple::default line 7)
 (up to date)
[2018-09-07T04:36:16-04:00] INFO: Chef Run complete in 0.265017699 seconds
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/simple/Berksfile from the cache; it is no longer needed by chef-client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/simple/.gitignore from the cache; it is no longer needed by chef-client.
[2018-09-07T04:36:16-04:00] INFO: Removing cookbooks/simple/.kitchen.yml from the cache; it is no longer needed by chef-client.

Running handlers:
[2018-09-07T04:36:16-04:00] INFO: Running report handlers
Running handlers complete
[2018-09-07T04:36:16-04:00] INFO: Report handlers complete

Chef Client finished, 0/1 resources updated in 03 seconds

No comments :

Post a Comment