Skip to content

Instantly share code, notes, and snippets.

@gsmurali
Last active January 7, 2017 18:09
Show Gist options
  • Save gsmurali/d7159fabbcc3e0535fa09d50472445f8 to your computer and use it in GitHub Desktop.
Save gsmurali/d7159fabbcc3e0535fa09d50472445f8 to your computer and use it in GitHub Desktop.
08 Linux Academy - Certified Chef Developer Basic Chef Fluency Badge- Lecture: Understanding Chef Recipes and Run Lists
#Understanding Chef Recipes and Run Lists
+ Recipes are a collection of resources, defined and written using patterns.
+ Helper code, such as loops and if statements, can be written around those resources to help customize the configurations of specific nodes. For example, if or case statements around package names.
+ Recipe is the most fundamental configuration element within the organization
+ A recipe is created using Ruby
+ must define everything that is required to configure part of a system
+ Recipe stored in a cookbook
+ May be included in a recipe(include_recipe)
+ May use the results of a search query and read the contents of a data bag
+ May have a dependency on one (or more) recipes
+ May tag a node to faciliate the creation of arbitrary groupings
+ Must be added to a run-list before it can be used by the chef-client
+ Is always executed in the same order as listed in a run-list
+ If included multiple times in a run-list, will only be executed once
+ There are "directives" that can change the order in which resources are executed
- notifies: A notification property that allows a resource to notify another resource to take action when its state changes
- subscribes: A notification property that allows a resource to listen to another resource and then take action if the state of the resource being listened to changes
#Chef Resource Ordering Execution
+ Example for notifies
service "httpd" do
end
cookbook_file "/etc/httpd/conf/httpd.conf" do
owner 'root'
group 'root'
mode '0644'
source 'httpd.conf'
notifies :restart, "service[httpd]"
end
+ Example for subscribes
service "httpd" do
subscribes :reload, "cookbook_file[/etc/httpd/conf/httpd.conf]"
end
cookbook_file "/etc/httpd/conf/httpd.conf" do
owner 'root'
group 'root'
mode '0644'
source 'httpd.conf'
end
+ Another Example
service "httpd" do
action [:enable, :start]
end
cookbook_file "/etc/httpd/conf/httpd.conf" do
owner 'root'
group 'root'
mode '0644'
source 'httpd.conf'
notifies :restart, "service[httpd]"
end
#A Brief look at run_list
+ A run-list is a list of cookbooks/recipes that are to be executed on a given node.
Example, below executes the default recipes within the cookbooks:
run_list "recipe[base]","recipe[apache]","recipe[selinux_policy]"
Example, below executes the recipes within the cookbooks:
run_list "recipe[base::recipe]","recipe[apache::recipe]","recipe[selinux_policy::recipe]","recipe[base::recipe]"
+ **Note: If for any reason a recipe is assigned to a node more than once (via roles/environments/etc.) chef-client will only execute it _one_ time**
##include_recipe
+ A recipe can include a recipe from an external cookbook
include_recipe 'cookbook_name::recipe_name'
**Note:** "include_recipe 'cookbook_name'" will, by default, translate to 'cookbook_name::default', the default recipe
+ **Important** If a recipe is being included from an external cookbook, then it is important to create a dependency on that cookbook in the metadata.rb file of the existing cookbook.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment