Chef – Foodcritic

Chef – Foodcritic ”; Previous Next Writing good cookbooks without any issue is quite a difficult task. But there are ways which can help in identifying the pitfalls. Flagging in Chef Cookbook is possible. Foodcritic is one of the best way of archiving it, which tries to identify possible issues with the logic and style of cookbooks. Foodcritic Setup Step 1 − Add Foodcritic gem. vipin@laptop:~/chef-repo $ subl Gemfile source ”https://rubygems.org” gem ”foodcritic”, ”~>2.2.0” Step 2 − Install the gem. vipin@laptop:~/chef-repo $ bundle install Fetching gem metadata from https://rubygems.org/ …TRUNCATED OUTPUT… Installing foodcritic (2.2.0) Foodcritic Gem Step 1 − Run Foodcritic on the cookbook. vipin@laptop:~/chef-repo $ foodcritic ./cookbooks/<Cookbook Name> FC002: Avoid string interpolation where not required: ./cookbooks/ mysql/attributes/server.rb:220 …TRUNCATED OUTPUT… FC024: Consider adding platform equivalents: ./cookbooks/<Cookbook Name>/ recipes/server.rb:132 Step 2 − Generate a detailed report. vipin@laptop:~/chef-repo $ foodcritic -C ./cookbooks/mysql cookbooks/<cookbook Name>/attributes/server.rb FC002: Avoid string interpolation where not required […] 85| default[”<Cookbook Name>”][”conf_dir”] = “#{mysql[”basedir”]}” […] cookbooks/<Cookbook Name>/recipes/client.rb FC007: Ensure recipe dependencies are reflected in cookbook metadata 40| end 41|when “mac_os_x” 42| include_recipe ”homebrew” 43|end 44| Working Method Foodcritic defines a set of rules and checks recipe agents, each one of them. It comes with multiple rules concerning various areas: styles, connectedness, attributes, string, probability, search, services, files, metadata, and so on. Print Page Previous Next Advertisements ”;

Chef – Nodes

Chef – Nodes ”; Previous Next Knife preflight shows details about all the nodes which uses a certain cookbook before uploading it to Chef server. Getting Started In order to get started, we need to have knife-preflight gem installed. Step 1 − Define the path in the gem file. vipin@laptop:~/chef-repo $ subl Gemfile source ”https://rubygems.org” gem ”knife-preflight” Step 2 − Run bundler to install knife-preflight gem. vipin@laptop:~/chef-repo $ bundle install Fetching gem metadata from https://rubygems.org/ …TRUNCATED OUTPUT… Installing knife-preflight (0.1.6) Working Method Run knife-preflight on the given cookbook. We can run the preflight command to find out which nodes and roles have the given cookbook in their expanded run lists. vipin@laptop:~/chef-repo $ knife preflight ntp Searching for nodes containing ntp OR ntp::default in their expanded run_list… 2 Nodes found www-staging.example.com cms-staging.example.com Searching for roles containing ntp OR ntp::default in their expanded run_list… 3 Roles found your_cms_role your_www_role your_app_role Found 6 nodes and 3 roles using the specified search criteria There are multiple ways for a cookbook to get executed on the node. You can assign the cookbook directly to a node by adding it to the node”s run list. You can add a cookbook to the role and add the role to the node”s run list. You can add the role to the run list of another role and add that other role to the node”s run list. A cookbook can be a dependency of another used cookbook. No matter how a cookbook ends up in a node”s run list, the knife preflight command will catch it as Chef stores all expanded lists of roles and recipes in node attributes. The knife preflight command issues a search for exactly those node attributes. Print Page Previous Next Advertisements ”;

Chef – Chef-Client as Daemon

Chef – Chef-Client as Daemon ”; Previous Next Running Chef-Client as daemon helps in knowing the state of all the nodes at any point of time. This help in running the Chef-Client at any point of time. Pre-requisites The node should be registered with Chef server and it should be running Chef-Client without any error. Chef-Client in Daemon Mode Start Chef-Client in daemon mode, running every 30 minutes. user@server:~$ sudo chef-client -i 1800 In the above code, – i enables to run the Chef-Client in daemon mode on the required node and 1800 seconds define that the Chef-Client daemon should run in every 30 minutes. Validating Daemon Run Validate that the Chef-Client is running as a daemon. user@server:~$ ps auxw | grep chef-client The above command will grep the running daemon process of Chef-Client. Other Ways Instead of running Chef-Client as a daemon, we can run the same as a cron job. user@server:~$ subl /etc/cron.d/chef_client PATH=/usr/local/bin:/usr/bin:/bin # m h dom mon dow user command */15 * * * * root chef-client -l warn | grep -v ”retrying [1234]/5 in” The above cron job will run after every 15 minutes. Print Page Previous Next Advertisements ”;

Chef – Roles

Chef – Roles ”; Previous Next Roles in Chef are a logical way of grouping nodes. Typical cases are to have roles for web servers, database servers, and so on. One can set custom run list for all the nodes and override attribute value within roles. Create a Role vipin@laptop:~/chef-repo $ subl roles/web_servers.rb name “web_servers” description “This role contains nodes, which act as web servers” run_list “recipe[ntp]” default_attributes ”ntp” => { ”ntpdate” => { ”disable” => true } } Once we have the role created, we need to upload to the Chef server. Upload Role to Chef Server vipin@laptop:~/chef-repo $ knife role from file web_servers.rb Now, we need to assign a role to a node called server. Assign a Role to Node vipin@laptop:~/chef-repo $ knife node edit server “run_list”: [ “role[web_servers]” ] Saving updated run_list on node server Run the Chef-Client user@server:~$ sudo chef-client …TRUNCATED OUTPUT… [2013-07-25T13:28:24+00:00] INFO: Run List is [role[web_servers]] [2013-07-25T13:28:24+00:00] INFO: Run List expands to [ntp] …TRUNCATED OUTPUT… How It Works Define a role in a Ruby file inside the roles folder of Chef repository. A role consists of a name and a description attribute. A role consists of role-specific run list and role-specific attribute settings. Every node that has a role in its run list will have the role’s run list exacted into its own. All the recipes in the role’s run list will be executed on the node. The role will be uploaded to Chef server using the knife role from file command. The role will be added to the node run list. Running Chef client on a node having the role in its run list will execute all the recipes listed in the role. Print Page Previous Next Advertisements ”;

Dynamically Configuring Recipes

Chef – Dynamically Configuring Recipes ”; Previous Next Attributes are the key components for dynamically configuring cookbooks. Attributes enable the authors to make the cookbook configurable. By overriding default values set in cookbooks, the user can inject their own values. Step 1 − Create a default file for cookbook attributes and add a default attribute to it. vipin@laptop:~/chef-repo $ subl cookbooks/my_cookbook/attributes/default.rb default[”my_cookbook”][”message”] = ”hello world!” Step 2 − Define the attribute inside the recipe. vipin@laptop:~/chef-repo $ subl cookbooks/<Cookbook Name>/recipes/default.rb message = node[”my_cookbook”][”message”] Chef::Log.info(“** Saying what I was told to say: #{message}”) Step 3 − Uploading the modified cookbook. vipin@laptop:~/chef-repo $ knife cookbook upload my_cookbook Uploading my_cookbook [0.1.0] Step 4 − Running Chef-Client of the defined node. user@server:~$ sudo chef-client …TRUNCATED OUTPUT… [2013-01-13T20:48:21+00:00] INFO: ** Saying what I was told to say: hello world! …TRUNCATED OUTPUT… Working Method Chef loads all attributes from the attribute file before it executes them. The attributes are stored with the node object. One can access all the attributes stored with the node object within recipes and retrieve their current values. Chef has a restricted structure starting from the default being the lowest, then comes normal (which is aliased with the set) and then overrides. The attribute level set in the recipe has precedence over the same level set in an attribute file. Overriding Attribute at the Node and Environment Level Attribute defined in roles or environment have the highest precedence. Step 1 − Create a role. vipin@laptop:~/chef-repo $ subl roles/german_hosts.rb name “german_hosts” description “This Role contains hosts, which should print out their messages in German” run_list “recipe[my_cookbook]” default_attributes “my_cookbook” => { “message” => “Hallo Welt!” } Step 2 − Upload the role to Chef server. vipin@laptop:~/chef-repo $ knife role from file german_hosts.rb Updated Role german_hosts! Step 3 − Assign the role to a node. vipin@laptop:~/chef-repo $ knife node edit server “run_list”: [ “role[german_hosts]” ] Saving updated run_list on node server Step 4 − Run the Chef-Client. user@server:~$ sudo chef-client …TRUNCATED OUTPUT… [2013-01-13T20:49:49+00:00] INFO: ** Saying what I was told to say: Hallo Welt! …TRUNCATED OUTPUT… Print Page Previous Next Advertisements ”;

Chef – Plain Ruby with Chef DSL

Chef – Plain Ruby with Chef DSL ”; Previous Next In Chef, if one needs to create simple recipes one can use resources available in Chef, such as templates, remote_file, and services. However as the recipes become elaborate, one needs advanced techniques, such as conditional statements to execute parts of the recipe on condition. This is the power of mixing plain Ruby with Chef Domain Specific Language (DSL). How to Use It? Start Chef Shell on any of the node in the client mode to be able to access the Chef server. user@server:~$ sudo chef-shell –client loading configuration: /etc/chef/client.rb Session type: client …TRUNCATED OUTPUT… run `help” for help, `exit” or ^D to quit. Ohai2u user@server! Chef> Basic Conditions with Chef DSL Sort nodes by name using plain Ruby. chef > nodes.sort! {|a,b| a.name <=> b.name } => [node[alice],node[server]] Loop through the nodes, printing their operating system. chef > nodes.each do |n| chef > puts n[”os”] chef ?> end linux windows => [node[server], node[alice]] Install multiple Ruby gems using an array, a loop, and string expansion to construct the gem names. chef > %w{ec2 essentials}.each do |gem| chef > gem_package “knife-#{gem}” chef ?> end => [“ec2”, “essentials”] Working Method Chef recipes are Ruby files, which gets evaluated in the context of Chef run. They can contain plain Ruby code such as if statement and loops as well as Chef DSL elements such as resources. Inside the recipe, one can simply declare Ruby variables and assign values to it. Print Page Previous Next Advertisements ”;

Chef – Useful Resources

Chef – Useful Resources ”; Previous Next The following resources contain additional information on Chef. Please use them to get more in-depth knowledge on this. Useful Links on Chef Chef Website− Official Website of Chef Chef Wiki − Wikipedia Reference for Chef Useful Books on Chef To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;

Chef – Solo Setup

Chef – Solo Setup ”; Previous Next Chef-Solo is an open source tool that runs locally and allows to provision guest machines using Chef cookbooks without the complication of any Chef client and server configuration. It helps to execute cookbooks on a self-created server. Before running Chef-Solo on the local machine, one needs to install the following two files on the local machine. Solo.rb − This file tells Chef about where to find cookbooks, roles, and data bags. Node.json − This file sets the run list and any node-specific attribute, if required. solo.rb Configuration Following are the steps to configure solo.rb. Step 1 − Create a solo.rb file inside the chef repo. 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” Step 2 − Add the file to git repo. $ git add solo.rb Step 3 − Create a node.json file inside the chef repo with the following content. { “run_list”: [ “recipe[ntp]” ] } Step 4 − Get the ntp cookbook inside the chef repo using knife. vipin@laptop:~/chef-repo $ knife cookbook site install ntp Installing ntp to /Users/mma/work/chef-repo/cookbooks …TRUNCATED OUTPUT… Cookbook ntp version 1.3.0 successfully installed Step 5 − Add the node.json file to Git. $ git add node.json Step 6 − Commit and push the files to git repo. vipin@laptop:~/chef-repo $ git commit -m “initial setup for Chef Solo” vipin@laptop:~/chef-repo $ git push Counting objects: 4, done. Delta compression using up to 4 threads. …TRUNCATED OUTPUT… To [email protected]:mmarschall/chef-repo.git b930647..5bcfab6 master -> master Running the Cookbook on the Node Step 1 − Login to the node where one wants to provision the Chef-Solo. Step 2 − Clone the Chef repo on the machine. $ git clone $URL_PATH Step 3 − cd to the chef repo. $ cd chef-repo Finally, run the Chef-Solo to converge the node − $ sudo chef-solo -c solo.rb -j node.json [2017-20-08T22:54:13+01:00] INFO: *** Chef 11.0.0 *** [2017-20-08T22:54:13+01:00] INFO: Setting the run_list to [“recipe[ntp]”] from JSON …TRUNCATED OUTPUT… [2012-12-08T22:54:16+01:00] INFO: Chef Run complete in 2.388374 seconds [2012-12-08T22:54:16+01:00] INFO: Running report handlers solo.rb configures Chef-Solo to look for its cookbooks, roles, and data bags inside the current directory: the Chef repository. Chef-Solo takes its node configuration from a JSON file. In our example, we called it node.json. If you”re going to manage multiple servers, you”ll need a separate file for each node. Then, Chef-Solo just executes a Chef run based on the configuration data found in solo.rb and node.json. Print Page Previous Next Advertisements ”;

Chef – Home

Chef Tutorial PDF Version Quick Guide Resources Job Search Discussion Chef is a configuration management technology developed by Opscode to manage infrastructure on physical or virtual machines. It is an open source developed using Ruby, which helps in managing complex infrastructure on the fly. This tutorial provides a basic understanding of the infrastructure and fundamental concepts of managing an infrastructure using Chef. Audience This tutorial has been prepared for those who want to understand the features and functionality of Chef and how Chef can help in reducing the complexity of managing an infrastructure. After completing this tutorial one would have moderate level understanding of Chef and its key building blocks. It will also give a fair idea on how to configure Chef in a preconfigured infrastructure and how to use it. Prerequisites We assume anyone who wants to learn Chef should have an understanding of system administration, infrastructure and network protocol communication. To automate the infrastructure provisioning, one should have a command over basic Ruby script writing and the underlying system where one wants to use Chef. Print Page Previous Next Advertisements ”;

Chef – Client Setup

Chef – Client Setup ”; Previous Next In order to make Chef node communicate with Chef server, you need to set up Chef client on the node. Chef Client This is one of the key components of Chef node, which retrieves the cookbooks from the Chef server and executes them on the node. It is also known as the Chef provisioner. Here, we will use Vagrant to manage VM. Vagrant can also be configured with the provisioner such as Shell script, Chef and Puppet to get VM into a desired state. In our case, we will use Vagrant to manage VMs using VirtualBox and Chef client as a provisioner. Step 1 − Download and install VirtualBox from https://www.virtualbox.org/wiki/downlod Step 2 − Download and install Vagrant at http://downloads.vagrantup.com Step 3 − Install Vagrant Omnibus plugin to enable Vagrant to install Chef client on the VM. $ vagrant plugin install vagrant-omnibus Creating and Booting Virtual Step 1 − We can download the required Vagrant box from the Opscode vagrant repo. Download the opscode-ubuntu-12.04 box from the following URL https://opscode-vmbento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box Step 2 − Once you have the Vagrant file, download the path you need to edit the Vagrant file. vipin@laptop:~/chef-repo $ subl Vagrantfile Vagrant.configure(“2”) do |config| config.vm.box = “opscode-ubuntu-12.04” config.vm.box_url = https://opscode-vm-bento.s3.amazonaws.com/ vagrant/opscode_ubuntu-12.04_provisionerless.box config.omnibus.chef_version = :latest config.vm.provision :chef_client do |chef| chef.provisioning_path = “/etc/chef” chef.chef_server_url = “https://api.opscode.com/ organizations/<YOUR_ORG>” chef.validation_key_path = “/.chef/<YOUR_ORG>-validator.pem” chef.validation_client_name = “<YOUR_ORG>-validator” chef.node_name = “server” end end In the above program, you need to update the <YOUR_ORG> name with the correct or required organization name. Step 3 − Next step after the configuration is, to get the vagrant box up. For this, you need to move to the location where Vagrant box is located and run the following command. $ vagrant up Step 4 − Once the machine is up, you can login to the machine using the following command. $ vagrant ssh In the above command, vagrantfile is written in a Ruby Domain Specific Language (DSL) for configuring the vagrant virtual machine. In the vagrant file, we have the config object. Vagrant will use this config object to configure the VM. Vagrant.configure(“2”) do |config| ……. End Inside the config block, you will tell vagrant which VM image to use, in order to boot the node. config.vm.box = “opscode-ubuntu-12.04” config.vm.box_url = https://opscode-vm-bento.s3.amazonaws.com/ vagrant/opscode_ubuntu-12.04_provisionerless.box In the next step, you will tell Vagrant to download the omnibus plugin. config.omnibus.chef_version = :latest After selecting the VM box to boot, configure how to provision the box using Chef. config.vm.provision :chef_client do |chef| ….. End Inside this, you need to set up instruction on how to hook up the virtual node to the Chef server. You need to tell Vagrant where you need to store all the Chef stuff on the node. chef.provisioning_path = “/etc/chef” Print Page Previous Next Advertisements ”;