Chef DevOps Cookbook Development & Resource Management 2 — Questions and Answers
Question 1: Where are Chef custom resources defined within a cookbook?
- In the `libraries/` directory as Ruby modules
- In the `resources/` directory as `.rb` files (Correct answer)
- In the `recipes/` directory with a `resource_` prefix
- In the `providers/` directory as action handlers
Correct answer: In the `resources/` directory as `.rb` files
Custom resources are placed in the `resources/` directory, where each `.rb` file defines a reusable resource with properties and action blocks.
Question 2: In a Chef custom resource definition, what keyword is used to declare an action block?
- define_action
- action (Correct answer)
- provides
- execute
Correct answer: action
The `action` keyword in a custom resource file declares a named action block containing the recipe DSL steps to execute when that action is called.
Question 3: What is the Chef 'wrapper cookbook' pattern?
- A cookbook that archives and distributes another cookbook as a compressed artifact
- A cookbook that depends on a community cookbook and overrides its attributes to customize behavior (Correct answer)
- A cookbook that wraps multiple cookbook recipes into a single entry-point recipe
- A cookbook that provides a REST API interface for Chef resource management
Correct answer: A cookbook that depends on a community cookbook and overrides its attributes to customize behavior
The wrapper cookbook pattern creates a thin cookbook that sets attributes and calls recipes from a community cookbook, keeping customizations separate from upstream code.
Question 4: What does `include_recipe` do when called inside a Chef recipe?
- It downloads a recipe from Chef Supermarket at runtime
- It adds another recipe's resources to the current run context in order (Correct answer)
- It imports a recipe as a Ruby library function callable in the cookbook
- It creates a version pin on a dependent cookbook's named recipe
Correct answer: It adds another recipe's resources to the current run context in order
`include_recipe 'cookbook::recipe'` inserts all of the named recipe's resources into the current resource collection at compile time, in the order called.
Question 5: What is a Chef 'data bag' used for?
- A compressed archive of cookbook files for offline distribution
- A global JSON data store on the Chef Server for sharing configuration data across nodes (Correct answer)
- A local directory that caches downloaded cookbooks on the workstation
- A Chef resource for managing archive files on managed nodes
Correct answer: A global JSON data store on the Chef Server for sharing configuration data across nodes
Data bags are indexed collections of JSON objects stored on the Chef Server, accessible from any recipe to share secrets, user lists, or global configuration data.
Question 6: Which Chef resource guard executes a resource only when its condition evaluates to true?
- not_if
- guard_if
- only_if (Correct answer)
- when_true
Correct answer: only_if
`only_if` accepts a shell command string or a Ruby block; when the command exits 0 or the block returns truthy, the resource executes.
Question 7: Which Ohai-populated node attribute stores the operating system distribution name such as 'ubuntu' or 'centos'?
- node['os']
- node['platform'] (Correct answer)
- node['distribution']
- node['kernel']['os']
Correct answer: node['platform']
`node['platform']` is populated by Ohai with the distribution name and is commonly used in platform-conditional cookbook logic.
Where are Chef custom resources defined within a cookbook?