🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Chef Tutorials: template Resource with example


What is cookbook template (.erb)

A cookbook template is an Embedded Ruby (ERB) template that is used to dynamically generate static text files. Templates may contain Ruby expressions and statements, and are a great way to manage configuration files.

Use the template resource to add cookbook templates to recipes; place the corresponding Embedded Ruby (ERB) template file in a cookbook’s /templates directory. template resource would not work with chef-apply but can be used with chef-solo.

Use the template resource to manage the contents of a file using an Embedded Ruby (ERB) template by transferring files from a sub-directory of COOKBOOK_NAME/templates/ to a specified path located on a host that is running Chef Infra Client.

ERB Specification

Chef uses Erubis as its template language. It allows embedding pure Ruby code inside special symbols in the templates.

Example
# Content of index.html.erb
# index.html.erb must be located at cookbook/templates/index.html.erb
<h1> This is our chef ohai attributes <%=node['platform'] %></h1>
<h1> This is our Recipe attributes <%=node['devopsschool']['message'] %></h1>
<% node['learn_chef_httpd']['default'].each do |key, value| -%>
<% unless value.empty? -%>
<%= key %>=<%= value %>
<% end %>
<% end %>
<%- 4.times do %>
<%= @hi %>, <%= @world %> from <%= @from %>!
<%- end %>
# Content of attributes/default.rb
default['elasticsearch']['default']['ES_USER'] = ''
default['elasticsearch']['default']['ES_GROUP'] = ''
default['elasticsearch']['default']['ES_HEAP_SIZE'] = ''
default['elasticsearch']['default']['MAX_OPEN_FILES'] = ''
default['elasticsearch']['default']['MAX_LOCKED_MEMORY'] = 'unlimited'
default['elasticsearch']['default']['MAX_MAP_COUNT'] = ''
default['elasticsearch']['default']['LOG_DIR'] = '/var/log/elasticsearch'
default['elasticsearch']['default']['DATA_DIR'] = '/var/lib/elasticsearch'
default['elasticsearch']['default']['WORK_DIR'] = '/tmp/elasticsearch'
default['elasticsearch']['default']['CONF_DIR'] = '/etc/elasticsearch'
default['elasticsearch']['default']['CONF_FILE'] = '/etc/elasticsearch/elasticsearch.yml'
default['elasticsearch']['default']['RESTART_ON_UPGRADE'] = ''
# Content of default.rb
template "/var/www/html/index.html" do
source "index.html.erb"
owner 'root'
group 'root'
mode '0755'
variables(
hi: 'Tesing',
world: 'Welt',
from: node['fqdn']
)
end
template '/etc/sudoers' do
source 'sudoers.erb'
mode '0440'
owner 'root'
group 'root'
variables(sudoers_groups: node['authorization']['sudo']['groups'],
sudoers_users: node['authorization']['sudo']['users'])
end
Content of templates/default/sudoers.erb
------------------------------------------
#
# /etc/sudoers
#
# Generated by Chef for <%= node['fqdn'] %>
#
Defaults !lecture,tty_tickets,!fqdn
# User privilege specification
root ALL=(ALL) ALL
<% @sudoers_users.each do |user| -%>
<%= user %> ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% end -%>
# Members of the sysadmin group may gain root privileges
%sysadmin ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% @sudoers_groups.each do |group| -%>
# Members of the group '<%= group %>' may gain root privileges
<%= group %> ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% end -%>
Content of attributes/default.rb
# --------------------------------------
default['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
default['authorization']['sudo']['users'] = %w(jerry greg)

Reference

  • https://docs.chef.io/templates/
  • https://github.com/scmgalaxy/chef-repo
  • https://docs.chef.io/resources/template/
  • https://docs.aws.amazon.com/opsworks/latest/userguide/cookbooks-101-basics-attributes.html
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x