You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
2
16
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.
4
18
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 Rubyrecipes for complex logic provides the best balance of simplicity and functionality.
6
20
7
21
## Basic Structure
8
22
9
23
YAML recipes must follow a specific structure:
10
24
11
-
````yaml
25
+
```yaml
12
26
---
13
27
resources:
14
28
- type: "resource_type"
@@ -19,27 +33,27 @@ resources:
19
33
name: "another_resource_name"
20
34
property1: value1
21
35
property2: value2
22
-
````
36
+
```
23
37
24
38
## File Naming and Location
25
39
26
40
YAML recipes can be placed in the same locations as Ruby recipes:
27
41
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)
31
45
32
46
### File Extension Support
33
47
34
48
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.
35
49
36
50
## Required Structure and Restrictions
37
51
38
-
### 1. Top-Level Resources Hash
52
+
### Top-Level Resources Hash
39
53
40
54
Every YAML recipe **must** contain a top-level `resources` key that contains an array of resource declarations:
41
55
42
-
````yaml
56
+
```yaml
43
57
# ✅ CORRECT
44
58
---
45
59
resources:
@@ -59,17 +73,17 @@ files:
59
73
- type: "file"
60
74
name: "/tmp/hello.txt"
61
75
content: "Hello World"
62
-
````
76
+
```
63
77
64
-
### 2. Resource Declaration Format
78
+
### Resource Declaration Format
65
79
66
80
Each resource in the array must have:
67
81
68
82
- **`type`**: The Chef resource type (string)
69
83
- **`name`**: The resource name/identifier (string)
70
84
- **Additional properties**: Resource-specific properties as key-value pairs
71
85
72
-
````yaml
86
+
```yaml
73
87
resources:
74
88
- type: "package"
75
89
name: "nginx"
@@ -78,13 +92,13 @@ resources:
78
92
- type: "service"
79
93
name: "nginx"
80
94
action: ["enable", "start"]
81
-
````
95
+
```
82
96
83
-
### 3. Single Document Limitation
97
+
### Single Document Limitation
84
98
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:
86
100
87
-
````yaml
101
+
```yaml
88
102
# ❌ INCORRECT - Multiple documents not supported
89
103
---
90
104
resources:
@@ -94,15 +108,15 @@ resources:
94
108
resources:
95
109
- type: "file"
96
110
name: "/tmp/file2.txt"
97
-
````
111
+
```
98
112
99
-
## Major Limitations
113
+
## YAML recipe limitations
100
114
101
-
### 1. No Ruby Code Blocks
115
+
### No Ruby Code Blocks
102
116
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:
104
118
105
-
````ruby
119
+
```ruby
106
120
# ❌ Cannot be expressed in YAML - Ruby blocks not supported
107
121
template "/etc/nginx/nginx.conf" do
108
122
source "nginx.conf.erb"
@@ -112,60 +126,60 @@ template "/etc/nginx/nginx.conf" do
112
126
notifies :restart, "service[nginx]", :delayed
113
127
only_if { node['platform'] == 'ubuntu' }
114
128
end
115
-
````
129
+
```
116
130
117
-
### 2. No Conditional Logic
131
+
### No Conditional Logic
118
132
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:
Chef::Exceptions::AmbiguousYAMLFile: Found both default.yml and default.yaml in cookbook, update the cookbook to remove one
308
322
```
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