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
end
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
end
apt_package 'tomcat8' do
action :install
end
- 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
end
service 'tomcat' do
action :start
end
- 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 manage.chef.io
- 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'
end
- And also i have changed kitchen.yaml file to include both platforms
---
driver:
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: https://www.vagrantup.com/docs/networking/forwarded_ports.html
# network:
# - ["forwarded_port", {guest: 80, host: 8080}]
provisioner:
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: https://docs.chef.io/config_yml_kitchen.html
# product_name: chef
# product_version: 15
verifier:
name: inspec
transport:
ssh_key: "./chef.pem"
connection_timeout: 10
connection_retries: 3
username: centos
platforms:
- name: ubuntu-18.04
driver:
image_id: 'ami-0d1cd67c26f5fca19'
transport:
username: ubuntu
- name: centos-7
suites:
- name: default
run_list:
- recipe[tomcat::default]
verifier:
inspec_tests:
- test/integration/default
attributes:
Exercise: Now try this approach for install apache server.
- Create a cookbook called as apache
- Ensure your kitchen has two platforms
- manual steps are given below
centos:
sudo yum install httpd
sudo service httpd start -y
ubuntu:
sudo apt-get update
sudo apt-get install apache2 -y
Like this:
Like Loading...