In the last article we wrote described the components of a cookbook & wrote a simple cookbook with a recipe consisting of a resource named httpd of type package & the action being performed was installation of the package.
I added a couple of things to this recipe & it looks like this now:
[sahil@cwork recipes]$ cat default.rb
#
# Cookbook Name:: apache
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
package_name = "httpd"
service_name = "httpd"
doc_root = "/var/www/html"
if node["platform"] == "ubuntu"
package_name = "apache2"
service_name = "apache2"
doc_root = "/var/www"
end
package package_name do
action :install
end
service service_name do
action [:start, :enable]
end
#cookbook_file "#{doc_root}/index.html" do
# source "index.html"
#mode "0644"
#end
template "#{doc_root}/index.html" do
source "index.html.erb"
mode "0644"
end
Common resource types:
Given below are some common resource types & their associated actions & attributes.
package, service, directory, file, bash, execute, cron, cookbook_file
Package:
actions:= :install, :upgrade, :remove, :purge, :reconfigure
attribute:= allow_downgrade, arch, flush_cache, options, source, version
Service:
actions:= :enable, :disable, :start, :stop, :restart, :reload
attributes:= init_command, priority, start_command, stop_command, restart_command, status_command, reload_command, supports
Directory:
actions:= :create, :delete
attribute:= path, owner, group, mode, recursive
File:
actions:= :create, :delete, create_if_missing, :touch
attribute:= path, content, owner, group, mode, backup
Bash:
actions:= :run, :nothing
attribute:= code, command, creates, cwd, user, group, umask, environment, timeout, path, return, flags
Execute:
actions:= :run, :nothing
attribute:= command, creates, cwd, user, group, umask, environment, timeout, path, return, flags
Cron:
actions:= :create, :delete,
attributes:= command, day, hour, minute, month, week, user, path, shell, home, mailto
Cookbook_file:
actions:= :create, :delete, :create_if_missing, touch
attributes:= path, content, owner, group, mode, backup, source
I added a couple of things to this recipe & it looks like this now:
[sahil@cwork recipes]$ cat default.rb
#
# Cookbook Name:: apache
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
package_name = "httpd"
service_name = "httpd"
doc_root = "/var/www/html"
if node["platform"] == "ubuntu"
package_name = "apache2"
service_name = "apache2"
doc_root = "/var/www"
end
package package_name do
action :install
end
service service_name do
action [:start, :enable]
end
#cookbook_file "#{doc_root}/index.html" do
# source "index.html"
#mode "0644"
#end
template "#{doc_root}/index.html" do
source "index.html.erb"
mode "0644"
end
That's a major overhaul from the last recipe. So, here's a breakdown of the updated cookbook.
The package_name, service_name & doc_root are variables. The intention of using variables is to make the cookbook platform independent in the sense that it can run on centos as well as ubuntu platforms without modification.
When the recipe is being applied, the if condition will be checked for the platform attribute returned by OHAI & depending on the result the value of the variables will be substituted.
If I move to the second resource 'service', the actions are comma separated & under []. This is basically the ruby syntax for an array since we are entering more than one values for the action parameter.
The next resource is 'cookbook_file'. This states that we'll be using a file that resides in the <cookbook_name>/files/default directory. So in our case, the file index.html lies in /home/sahil/chef-repo/cookbooks/apache/files/default.
[sahil@cwork default]$ pwd
/home/sahil/chef-repo/cookbooks/apache/files/default
[sahil@cwork default]$ ls
index.html
[sahil@cwork default]$ cat index.html
<html>
<body>
<h1> Hi, my name is Sahil!!</h1>
</body>
</html>
In the above recipe I've commented out this action but I had checked & executed it earlier. The recipe copies the file index.html to /var/www/html on the client node.
The next part, the template allows us to use a .erb file. ERB means extended ruby. So, we can use attributes returned by OHAI & their values will be dynamically substituted when during the chef run.
the location for the file index.html.erb is /home/sahil/chef-repo/cookbooks/apache/templates/default.
[sahil@cwork default]$ pwd
/home/sahil/chef-repo/cookbooks/apache/templates/default
[sahil@cwork default]$ ls
index.html.erb
[sahil@cwork default]$ cat index.html.erb
<html>
<body>
<h1> Hi, from server <%= node["fqdn"] %> </h1>
<p>
This server has <%= node["memory"]["total"].to_i / 1024 %> MB memory
</p>
</body>
</html>
[sahil@cwork default]$
The <%=node["attribute"]%> is the syntax we need to use while dynamically substituting attribute values.
When I uploaded & ran this cookbook & pointed the browser to my chef client node IP 192.168.44.102, It showed the following:
Common resource types:
Given below are some common resource types & their associated actions & attributes.
package, service, directory, file, bash, execute, cron, cookbook_file
Package:
actions:= :install, :upgrade, :remove, :purge, :reconfigure
attribute:= allow_downgrade, arch, flush_cache, options, source, version
Service:
actions:= :enable, :disable, :start, :stop, :restart, :reload
attributes:= init_command, priority, start_command, stop_command, restart_command, status_command, reload_command, supports
Directory:
actions:= :create, :delete
attribute:= path, owner, group, mode, recursive
File:
actions:= :create, :delete, create_if_missing, :touch
attribute:= path, content, owner, group, mode, backup
Bash:
actions:= :run, :nothing
attribute:= code, command, creates, cwd, user, group, umask, environment, timeout, path, return, flags
Execute:
actions:= :run, :nothing
attribute:= command, creates, cwd, user, group, umask, environment, timeout, path, return, flags
Cron:
actions:= :create, :delete,
attributes:= command, day, hour, minute, month, week, user, path, shell, home, mailto
Cookbook_file:
actions:= :create, :delete, :create_if_missing, touch
attributes:= path, content, owner, group, mode, backup, source
No comments:
Post a Comment