LIANNE LARDIZABAL


Software Engineer, BJJ Practioner, Artist
Computer Science @ University of British Columbia
Vancouver, BC

Configuring a resource with Chef infrastructure automation

In Chef, a resource determines the desired state of a system and a recipe describes an ordered set of state configurations. We can write our own “recipe,” the name of a Ruby script denoted by Chef, to declare the state of a resource. Chef takes idempotent measures when converging a system to a particular state described as test and repair. First, it evaluates the recipes on the node and searches for resources to add to its list of resources. After evaluating the recipes, it iterates over the list of resources and takes the actions necessary to set the resources to the recipe-declared state. In this example, we write a simple recipe to determine the state of an MOTD file and use chef-client to apply these changes to our system.

1. Set up a directory for the file to live in

mkdir ~/chef-repo

2. Create an MOTD file named hello.rb

file '/tmp/motd' do
  content 'hello world'
end

Our recipe states that this file must exist and what it contains. However, it does not indicate how this file is created. This feature offers flexibility for development on different platforms.

3. Apply the changes to the system

chef-client --local-mode hello.rb

The file is created and the system is up-to-date. What happens when we make changes to our recipe?

4. Update the contents of the MOTD file

file '/tmp/motd' do
  content 'hello chef'
end

When we run chef-client again, Chef compares the current state of the system and the contents of the recipe. Since the state changed, Chef recognizes the differences and applies the changes to the system.

Reference:
Chef Infrastructure Automation