DevOps Classroom Series – 17/Apr/2020

Recipe DSL (Domain Specific Language)

  • In chef we write recipes, and recipes are .rb (ruby) files
  • To work on chef you donot need to know whole ruby, but you can use any thing in ruby.
file '/tmp/test.txt' do
  action :create

Write a chef cookbook to install tomcat server

chef generate cookbook -b tomcat
  • Find out manual commands to install tomcat on ubuntu
sudo apt-get update
sudo apt-get install tomcat8 -y
  • These steps are specific for ubuntu, so let me create a new recipe for ubuntu
chef generate recipe --help
chef generate recipe . tomcat_ubuntu


  • Every recipe will have a naming format <cookbookname>::<recipename>
    • in tomcat cookbook tomcat_ubuntu recipe name tomcat::tomcat_ubuntu
    • Writing default or directly writing cookbookname is same tomcat is equal to tomcat::default
  • Lets try to automate the below commands and write them in tomcat_ubuntu recipe
sudo apt-get update
sudo apt-get install tomcat8 -y
  • Now add the resources in tomcat_ubuntu recipe
# Cookbook:: .
# Recipe:: tomcat_ubuntu
# Copyright:: 2020, The Authors, All Rights Reserved.

apt_update 'update' do
    ignore_failure true
    action :update

apt_package 'tomcat8' do
    action :install

  • Now make changes in the run_list in the kitchen.yaml file
  • Now we want to install tomcat on centos using the same cookbook
  • Manual commands are
sudo yum install tomcat -y
sudo service tomcat start
  • Create a new recipe tomcat_centos
# Cookbook:: .
# Recipe:: tomcat_centos
# Copyright:: 2020, The Authors, All Rights Reserved.

yum_package 'tomcat' do
    action :install

service 'tomcat' do
    action :start

  • Now i want to run recipe tomcat_ubuntu when the machine is ubuntu and recipe tomcat_centos when the machine is centos
  • In chef we have a software called as ohai, which collects information about nodes and gives it to the chef server
  • Note: Before next class bootstrap two nodes one ubuntu and one chef to
  • Now in the default.rb file in recipes i have written a simple condition to call the necessary recipe
# Cookbook:: tomcat
# Recipe:: default
# Copyright:: 2020, The Authors, All Rights Reserved.

if node['platform_family'] == 'rhel' 
    include_recipe 'tomcat::tomcat_centos'
elsif node['platform_family'] == 'debian'
    include_recipe 'tomcat::tomcat_ubuntu'
  • And also i have changed kitchen.yaml file to include both platforms
  name: ec2
  aws_ssh_key_id: chef
  region: us-west-2
  availability_zone: us-west-2b
  subnet_id: subnet-89fb32ee
  instance_type: t2.micro
  image_id: 'ami-0bc06212a56393ee1'
  security_group_ids: ["sg-0fde1a94ae5592488"]

## The forwarded_port port feature lets you connect to ports on the VM guest via
## localhost on the host.
## see also:

#  network:
#    - ["forwarded_port", {guest: 80, host: 8080}]

  name: chef_zero
  # You may wish to disable always updating cookbooks in CI or other testing environments.
  # For example:
  #   always_update_cookbooks: <%= !ENV['CI'] %>
  always_update_cookbooks: true

  ## product_name and product_version specifies a specific Chef product and version to install.
  ## see the Chef documentation for more details:
  #  product_name: chef
  #  product_version: 15

  name: inspec

  ssh_key: "./chef.pem"
  connection_timeout: 10
  connection_retries: 3
  username: centos

  - name: ubuntu-18.04
      image_id: 'ami-0d1cd67c26f5fca19'
      username: ubuntu
  - name: centos-7

  - name: default
      - recipe[tomcat::default]
        - test/integration/default

Exercise: Now try this approach for install apache server.

  1. Create a cookbook called as apache
  2. Ensure your kitchen has two platforms
  3. manual steps are given below

sudo yum install httpd
sudo service httpd start -y


sudo apt-get update
sudo apt-get install apache2 -y

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin