Your browser is very old. Please use a recent version of Firefox, or Google Chrome as they have the best support for modern web standards.

Top 3 reasons to use Chef over Bash!

How Chef can improve your deployments!

By John Wyatt


Chef, one of the most popular server automation tools, has always been the tool you should learn (or anything but shell scripts). But what benefit does it offer that makes it better for setting up your servers than shell scripts?

1) Chef stops when it hits an error

You want this.

Bash scripts may skip over a return value of nonzero from a command or a program. Chef stops in it's tracks when you have an error.

You want this because it can reveal critical problems. An incompatibility with new packages. Testing new versions or Ubuntu or RedHat Linux. Or, when you just break something.

Of course, if it doesn't matter, you can always ignore it by adding 'ignore_failure: true' to the resource block.

2) Templates

One of the best reasons to use Chef over shell scripts. With shell scripts you would have to write boiler plate code to tarball the different configuration files together, extract them, upload them, and set each path and you have no ability to dynamically generate the templates.

Templates makes setting config files so much easier. You can not only set config files by just copying a file in the cookbook's template directory you can dynamically generate a template by adding an .erb extension to it and adding some embedded Ruby code like below:

server_name: <%= node[:server_name] %>

or selectively leave out entire blocks with familiar if statements right in the template file:

<% if node[:set_ssl] %>

# SSL stuff

<% else %>

# Port 80 stuff

<% end %>

3) Package Management

Installing packages with shell scripts is pretty easy:

sudo apt install package.name -y

and just as easy in Chef

package 'package.name'

but the title for section 3 is package management, not installing packages.

How do you handle checking if a package is installed?

PKG_OK=$(dpkg-query -W --showformat='${Status}\n' the.package.name|grep "install ok installed")
echo Checking for somelib: $PKG_OK
if [ "" == "$PKG_OK" ]; then
  echo "No somelib. Setting up somelib."
  sudo apt-get --force-yes --yes install the.package.name
fi
( Example courtesy of Urhixidur at https://stackoverflow.com/a/10439058/1613327 )

or in Chef

package 'package.name'

Chef resources are idempotent, which means is that no matter how many times you run the above resource it will not change the state from what was set. (The exception would be execute resources because they execute shell scripts.)

You can also control the version as such:

package 'package.name' do
  version "some_package_number"
end

Or control for Linux distribution:

package 'Install Apache' do
  case node[:platform]
  when 'redhat', 'centos'
    package_name 'httpd'
  when 'ubuntu', 'debian'
    package_name 'apache2'
  end
end

-

Get started with Chef with my course at WyattTechCourses' 'Introduction to Chef' at https://wyatttechcourses.com/courses/introduction-to-chef


Tags: chef, linux, server automation

share

More Articles