Skip to content

Commit 3bf33e5

Browse files
committed
Quick fixes
Signed-off-by: Ian Maddaus <[email protected]>
1 parent b74bb85 commit 3bf33e5

File tree

2 files changed

+69
-61
lines changed

2 files changed

+69
-61
lines changed

content/debug.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ aliases = ["/debug.html"]
99
title = "Debug Recipes, Client Runs"
1010
identifier = "chef_infra/cookbook_reference/recipes/debug.md Debug Recipes, Client Runs"
1111
parent = "chef_infra/cookbook_reference/recipes"
12-
weight = 20
12+
weight = 30
1313
+++
1414

1515
Elements of good approaches to building cookbooks and recipes that are

content/recipes_in_yaml.md renamed to content/recipes_yaml.md

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
# Chef YAML Recipes: Complete Guide
1+
+++
2+
title = "About YAML Recipes"
3+
draft = false
4+
gh_repo = "chef-web-docs"
5+
6+
[menu]
7+
[menu.infra]
8+
title = " YAML Recipes"
9+
identifier = "chef_infra/cookbook_reference/recipes/YAML Recipes"
10+
parent = "chef_infra/cookbook_reference/recipes"
11+
weight = 20
12+
+++
13+
14+
YAML recipes provide an alternative way to define Chef Infra resources using YAML syntax instead of Ruby.
15+
This feature was introduced to make Chef Infra recipes more accessible to users who are more comfortable with declarative YAML syntax than Ruby code.
216

3-
## Overview
17+
YAML recipes provide a simplified way to define Chef resources for basic use cases. While they have significant limitations compared to Ruby recipes, they can be valuable for teams that prefer declarative YAML syntax and don't require advanced Chef DSL features. For complex scenarios involving dynamic logic, node attributes, or resource relationships, Ruby recipes remain the preferred approach.
418

5-
Chef YAML recipes provide an alternative way to define Chef resources using YAML syntax instead of Ruby. This feature was introduced to make Chef recipes more accessible to users who are more comfortable with declarative YAML syntax than Ruby code.
19+
For most production environments, a hybrid approach using both YAML recipes for simple static configurations and Ruby recipes for complex logic provides the best balance of simplicity and functionality.
620

721
## Basic Structure
822

923
YAML recipes must follow a specific structure:
1024

11-
````yaml
25+
```yaml
1226
---
1327
resources:
1428
- type: "resource_type"
@@ -19,27 +33,27 @@ resources:
1933
name: "another_resource_name"
2034
property1: value1
2135
property2: value2
22-
````
36+
```
2337
2438
## File Naming and Location
2539
2640
YAML recipes can be placed in the same locations as Ruby recipes:
2741
28-
- **Standard recipe location**: `cookbooks/mycookbook/recipes/default.yml` or `cookbooks/mycookbook/recipes/default.yaml`
29-
- **Named recipes**: `cookbooks/mycookbook/recipes/web.yml` or `cookbooks/mycookbook/recipes/database.yaml`
30-
- **Root-level recipe alias**: `cookbooks/mycookbook/recipe.yml` or `cookbooks/mycookbook/recipe.yaml` (acts as default recipe)
42+
- **Standard recipe location**: `cookbooks/cookbook_name/recipes/default.yml` or `cookbooks/cookbook_name/recipes/default.yaml`
43+
- **Named recipes**: `cookbooks/cookbook_name/recipes/web.yml` or `cookbooks/cookbook_name/recipes/database.yaml`
44+
- **Root-level recipe alias**: `cookbooks/cookbook_name/recipe.yml` or `cookbooks/cookbook_name/recipe.yaml` (acts as default recipe)
3145

3246
### File Extension Support
3347

3448
Both `.yml` and `.yaml` extensions are supported. However, if both `default.yml` and `default.yaml` exist in the same cookbook, Chef will raise an `AmbiguousYAMLFile` error requiring you to remove one of them.
3549

3650
## Required Structure and Restrictions
3751

38-
### 1. Top-Level Resources Hash
52+
### Top-Level Resources Hash
3953

4054
Every YAML recipe **must** contain a top-level `resources` key that contains an array of resource declarations:
4155

42-
````yaml
56+
```yaml
4357
# ✅ CORRECT
4458
---
4559
resources:
@@ -59,17 +73,17 @@ files:
5973
- type: "file"
6074
name: "/tmp/hello.txt"
6175
content: "Hello World"
62-
````
76+
```
6377

64-
### 2. Resource Declaration Format
78+
### Resource Declaration Format
6579

6680
Each resource in the array must have:
6781

6882
- **`type`**: The Chef resource type (string)
6983
- **`name`**: The resource name/identifier (string)
7084
- **Additional properties**: Resource-specific properties as key-value pairs
7185

72-
````yaml
86+
```yaml
7387
resources:
7488
- type: "package"
7589
name: "nginx"
@@ -78,13 +92,13 @@ resources:
7892
- type: "service"
7993
name: "nginx"
8094
action: ["enable", "start"]
81-
````
95+
```
8296

83-
### 3. Single Document Limitation
97+
### Single Document Limitation
8498

85-
YAML recipes support only **one YAML document** per file. Multiple documents separated by `---` are not allowed:
99+
YAML recipes support only **one YAML document** in each file. Multiple documents separated by `---` aren't allowed:
86100

87-
````yaml
101+
```yaml
88102
# ❌ INCORRECT - Multiple documents not supported
89103
---
90104
resources:
@@ -94,15 +108,15 @@ resources:
94108
resources:
95109
- type: "file"
96110
name: "/tmp/file2.txt"
97-
````
111+
```
98112

99-
## Major Limitations
113+
## YAML recipe limitations
100114

101-
### 1. No Ruby Code Blocks
115+
### No Ruby Code Blocks
102116

103-
YAML recipes cannot contain Ruby code blocks, which significantly limits their functionality compared to Ruby recipes:
117+
YAML recipes can't contain Ruby code blocks, which significantly limits their functionality compared to Ruby recipes:
104118

105-
````ruby
119+
```ruby
106120
# ❌ Cannot be expressed in YAML - Ruby blocks not supported
107121
template "/etc/nginx/nginx.conf" do
108122
source "nginx.conf.erb"
@@ -112,60 +126,60 @@ template "/etc/nginx/nginx.conf" do
112126
notifies :restart, "service[nginx]", :delayed
113127
only_if { node['platform'] == 'ubuntu' }
114128
end
115-
````
129+
```
116130

117-
### 2. No Conditional Logic
131+
### No Conditional Logic
118132

119-
YAML recipes cannot include conditional logic like `if`, `unless`, `only_if`, or `not_if` with Ruby expressions:
133+
YAML recipes can't include conditional logic like `if`, `unless`, `only_if`, or `not_if` with Ruby expressions:
120134

121-
````yaml
135+
```yaml
122136
# ❌ Cannot include complex conditionals
123137
resources:
124138
- type: "package"
125139
name: "nginx"
126140
# Cannot do: only_if { node['platform'] == 'ubuntu' }
127-
````
141+
```
128142

129-
### 3. No Node Attribute Access
143+
### No Node Attribute Access
130144

131-
YAML recipes cannot directly access node attributes or perform Ruby evaluations:
145+
YAML recipes can't directly access node attributes or perform Ruby evaluations:
132146

133-
````yaml
147+
```yaml
134148
# ❌ Cannot access node attributes dynamically
135149
resources:
136150
- type: "user"
137151
name: "webapp"
138152
# Cannot do: home "/home/#{node['webapp']['user']}"
139153
home: "/home/webapp" # Must be static
140-
````
154+
```
141155

142-
### 4. No Resource Notifications
156+
### No Resource Notifications
143157

144-
Complex resource relationships and notifications cannot be expressed:
158+
Complex resource relationships and notifications can't be expressed:
145159

146-
````yaml
160+
```yaml
147161
# ❌ Cannot express notifications between resources
148162
resources:
149163
- type: "template"
150164
name: "/etc/nginx/nginx.conf"
151165
source: "nginx.conf.erb"
152166
# Cannot do: notifies :restart, "service[nginx]", :delayed
153-
````
167+
```
154168

155-
### 5. No Include/Require Functionality
169+
### No Include/Require Functionality
156170

157-
YAML recipes cannot include other recipes or libraries:
171+
YAML recipes can't include other recipes or libraries:
158172

159-
````yaml
173+
```yaml
160174
# ❌ Cannot include other recipes
161175
# include_recipe "cookbook::other_recipe"
162-
````
176+
```
163177

164178
## Examples
165179

166180
### Basic File Management
167181

168-
````yaml
182+
```yaml
169183
---
170184
resources:
171185
- type: "directory"
@@ -174,54 +188,54 @@ resources:
174188
group: "myapp"
175189
mode: "0755"
176190
recursive: true
177-
191+
178192
- type: "file"
179193
name: "/opt/myapp/config.txt"
180194
content: "This is a configuration file"
181195
owner: "myapp"
182196
group: "myapp"
183197
mode: "0644"
184-
````
198+
```
185199

186200
### Package and Service Management
187201

188-
````yaml
202+
```yaml
189203
---
190204
resources:
191205
- type: "package"
192206
name: "nginx"
193207
action: "install"
194-
208+
195209
- type: "package"
196210
name: "curl"
197211
action: "install"
198-
212+
199213
- type: "service"
200214
name: "nginx"
201215
action: ["enable", "start"]
202-
````
216+
```
203217

204218
### User Management
205219

206-
````yaml
220+
```yaml
207221
---
208222
resources:
209223
- type: "group"
210224
name: "developers"
211225
gid: 3000
212-
226+
213227
- type: "user"
214228
name: "alice"
215229
uid: 2001
216230
gid: 3000
217231
home: "/home/alice"
218232
shell: "/bin/bash"
219233
action: "create"
220-
````
234+
```
221235

222236
### Template with Static Variables
223237

224-
````yaml
238+
```yaml
225239
---
226240
resources:
227241
- type: "template"
@@ -230,8 +244,8 @@ resources:
230244
owner: "root"
231245
group: "root"
232246
mode: "0644"
233-
# Note: Variables must be static, cannot use node attributes
234-
````
247+
# Note: Variables must be static, can't use node attributes
248+
```
235249

236250
## Working with YAML Recipes
237251

@@ -243,7 +257,7 @@ Chef provides a `knife yaml convert` command to convert YAML recipes to Ruby:
243257
knife yaml convert recipes/default.yml recipes/default.rb
244258
```
245259

246-
**Note**: Converting from Ruby to YAML is not supported due to the limitations of YAML format.
260+
**Note**: Converting from Ruby to YAML isn't supported due to the limitations of YAML format.
247261

248262
### File Processing
249263

@@ -285,7 +299,7 @@ Avoid YAML recipes when you need:
285299
3. Consider hybrid approach: YAML for simple resources, Ruby for complex ones
286300
4. Use `knife yaml convert` to understand Ruby equivalents
287301

288-
## Error Handling
302+
## Troubleshooting
289303

290304
Common errors when working with YAML recipes:
291305

@@ -306,9 +320,3 @@ ArgumentError: YAML recipe 'recipes/default.yml' contains multiple documents, on
306320
```text
307321
Chef::Exceptions::AmbiguousYAMLFile: Found both default.yml and default.yaml in cookbook, update the cookbook to remove one
308322
```
309-
310-
## Conclusion
311-
312-
YAML recipes provide a simplified way to define Chef resources for basic use cases. While they have significant limitations compared to Ruby recipes, they can be valuable for teams that prefer declarative YAML syntax and don't require advanced Chef DSL features. For complex scenarios involving dynamic logic, node attributes, or resource relationships, Ruby recipes remain the preferred approach.
313-
314-
For most production environments, a hybrid approach using both YAML recipes for simple static configurations and Ruby recipes for complex logic provides the best balance of simplicity and functionality.

0 commit comments

Comments
 (0)