What is the significance of the default directory under chef cookbook /templates?

 

What is the significance of the default directory under chef cookbook /templates?
A cookbook is frequently designed to work across many platforms and is often required to distribute a specific template to a specific platform. This is a New in Chef Client 12.0. A cookbook can be designed to support the distribution of templates across platforms, while ensuring that the correct template ends up on each system.

 

The pattern for template specificity depends on two things: the lookup path and the source. The first pattern that matches is used:
/host-$fqdn/$source
/$platform-$platform_version/$source
/$platform/$source
/default/$source
/$source

 

Use an array with the source property to define an explicit lookup path. For example:
template ‘/test’ do
  source [“#{node.chef_environment}.erb”, ‘default.erb’]
end

 

The following example emulates the entire file specificity pattern by defining it as an explicit path:
template ‘/test’ do
  source %W{
    host-#{node[‘fqdn’]}/test.erb
    #{node[‘platform’]}-#{node[‘platform_version’]}/test.erb
    #{node[‘platform’]}/test.erb
    default/test.erb
  }
end

 

A cookbook may have a /templates directory structure like this:
/templates/
  windows-6.2
  windows-6.1
  windows-6.0
  windows
  default

 

and a resource that looks something like the following:
template ‘C:\path\to\file\text_file.txt’ do
  source ‘text_file.txt’
  mode ‘0755’
  owner ‘root’
  group ‘root’
end

 

This resource would be matched in the same order as the /templates directory structure. For a node named host-node-desktop that is running Windows 7, the second item would be the matching item and the location:
/templates
  windows-6.2/text_file.txt
  windows-6.1/text_file.txt
  windows-6.0/text_file.txt
  windows/text_file.txt

  default/text_file.txt

Reference
https://docs.chef.io/resource_template.html

Tagged : / / / / / /

Write 10 Chef Cookbooks to test your chef programming skills

chef-cookbooks-to-test-your-chef-programming-skills

  1. Write a cookbook using you can install apache http server in CentOs and Ubuntu, enable the services and start the service.

  2. Write a recipe using template resources to create a file, but only if an attribute has a specific value.

  3. Write a recipe to create a file using a string, but not if “/etc/passwd” exists?

  4. Write a cookbook to unzip a file, and then move a files from one location to another using batch, bash chef resources?

  5. Create a role and assign any two cookbooks to role and setup a 2 nodes assigned to role and conerse the role.

  6. Write a cookbook using install version of latest tomcat and deploy jenkins.war files into war.

  7. Write a 4 cookbooks php, mysql, apache and webapp respectively and map a dependency between them and install a sample web application.

  8. Write a cookbook to install git, wget, zip in RHEL and Ubuntu.

  9. Write a cookbook in which create a file using a string, but only if “/etc/passwd” exists

  10. Write a coobook to run a batch file that unzips and then moves files from one location to another.

Tagged : / / / / / / / / / /

Write 10 Chef Cookbooks to test your chef programming skills

chef-cookbooks

  1. Write a cookbook using you can install apache http server in CentOs and Ubuntu, enable the services and start the service.
  2. Write a recipe using template resources to create a file, but only if an attribute has a specific value.
    1. Write a recipe to create a file using a string, but not if “/etc/passwd” exists?
  3. Write a cookbook to unzip a file, and then move a files from one location to another using batch, bash chef resources?
  4. Create a role and assign any two cookbooks to role and setup a 2 nodes assigned to role and conerse the role.
  5. Write a cookbook using install version of latest tomcat and deploy jenkins.war files into war.
  6. Write a 4 cookbooks php, mysql, apache and webapp respectively and map a dependency between them and install a sample web application.
  7. Write a cookbook to install git, wget, zip in RHEL and Ubuntu.
  8. Write a cookbook in which create a file using a string, but only if “/etc/passwd” exists
  9. Write a coobook to run a batch file that unzips and then moves files from one location to another.
Tagged : / / / / / / / / / /

Chef – Documenting Cookbooks automatically

chef-documenting-cookbooks

Problems Area –

Our infrastructure has many cookbooks that aim to be reusable, primarily through encapsulating behaviour in LWRPs. This led to an explosion of LWRPs and sometimes the documentation didn’t keep up or did just not exist.

Solution 1: Follow Best Practices 

This command will create a README.rdoc by default, and I prefer Markdown, so I specify the -r md option.

> knife cookbook create smartmontools -r md

Solution 2: Automate using plugins

README.md which should be generated automatcially must contain information about the recipes, attributes, platform compatibility and cookbook requirements (i.e. depends, recommends, suggests etc).

Mathias Lafeldt wrote a knife plugin that generates an initial README.md from the metadata.rb file in a a cookbook.

Link –

Extension of Mathias Lafeldt was done here with regenerate README.md from the cookbook source code

Please refer these useful blogs as well which is referenced in order to write this article…

Tagged : / / / / / / / / / / / /