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
2. Create an MOTD file named hello.rb
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
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
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.
Chef Infrastructure Automation