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
Copy file name to clipboardExpand all lines: content/recipes_yaml.md
+40-29Lines changed: 40 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,24 +17,6 @@ YAML recipes simplify defining Chef resources for basic use cases. While they ha
17
17
18
18
For most production environments, use a hybrid approach: YAML recipes for simple static configurations and Ruby recipes for complex logic. This approach balances simplicity and functionality.
19
19
20
-
## Use cases
21
-
22
-
Use YAML recipes for:
23
-
24
-
- Simple, static configurations
25
-
- Declarative resource management
26
-
- Teams preferring YAML over Ruby
27
-
- Basic infrastructure as code
28
-
29
-
Avoid YAML recipes when you need:
30
-
31
-
- Dynamic node attribute access
32
-
- Conditional logic
33
-
- Resource notifications
34
-
- Complex data transformations
35
-
- Integration with Ruby libraries
36
-
- Advanced Chef DSL features
37
-
38
20
## Create a YAML recipe
39
21
40
22
To create a YAML recipe, follow these steps:
@@ -75,32 +57,36 @@ To create a YAML recipe, follow these steps:
75
57
In this example:
76
58
77
59
- the [`package` resource]({{< relref "/resources/package/" >}}) uses the `install` action and the `version` property to install Nginx 1.18.0.
78
-
- the [`service` resource]({{< relref "/resources/service/" >}}) uses the `enable` and `start` actions to enable and start up Nginx.
60
+
- the [`service` resource]({{< relref "/resources/service/" >}}) uses the `enable` and `start` actions to enable and start Nginx.
79
61
80
62
## Examples
81
63
82
64
### Basic file management
83
65
66
+
Use the [`directory` resource]({{< relref "/resources/directory">}}) to create the `/opt/app_name` directory and apply owner and group permissions to the directory. Use the [`file` resource]({{< relref "/resources/">}}) to create the `/opt/app_name/config.txt` file, add text to the file, and apply file owner and group permissions to the file.
67
+
84
68
```yaml
85
69
---
86
70
resources:
87
71
- type: "directory"
88
-
name: "/opt/myapp"
89
-
owner: "myapp"
90
-
group: "myapp"
72
+
name: "/opt/app_name"
73
+
owner: "app_name"
74
+
group: "app_name"
91
75
mode: "0755"
92
76
recursive: true
93
77
94
78
- type: "file"
95
-
name: "/opt/myapp/config.txt"
79
+
name: "/opt/app_name/config.txt"
96
80
content: "This is a configuration file"
97
-
owner: "myapp"
98
-
group: "myapp"
81
+
owner: "app_name"
82
+
group: "app_name"
99
83
mode: "0644"
100
84
```
101
85
102
86
### Package and service management
103
87
88
+
Use the [`package` resource]({{< relref "/resources/package">}}) to install Nginx and curl. Then use the [`service` resource]({{< relref "/resources/service">}}) to enable and start Nginx.
89
+
104
90
```yaml
105
91
---
106
92
resources:
@@ -119,6 +105,8 @@ resources:
119
105
120
106
### User management
121
107
108
+
Use the [`group` resource]({{< relref "/resources/group">}}) to create a group called "developers" and the [`user` resource]({{< relref "/resources/">}}) to create a user, give them properties, and assign them to the developers group.
109
+
122
110
```yaml
123
111
---
124
112
resources:
@@ -137,16 +125,39 @@ resources:
137
125
138
126
### Template with static variables
139
127
128
+
Use the [`template` resource]({{< relref "/resources/template">}}) create the `/etc/app_name/config.yml` file using the `config.yml.erb` template.
129
+
140
130
```yaml
141
131
---
142
132
resources:
143
133
- type: "template"
144
-
name: "/etc/myapp/config.yml"
134
+
name: "/etc/app_name/config.yml"
145
135
source: "config.yml.erb"
146
136
owner: "root"
147
137
group: "root"
148
138
mode: "0644"
149
-
# Note: Variables must be static, can't use node attributes
139
+
```
140
+
141
+
### Guards
142
+
143
+
Some common resource functionality is also supported, as long as the value of a property can be expressed as one of the four primitive types (string, integer, boolean, array). That means it's possible to use [`only_if` or `not_if` guards]({{< relref "/resource_common#guards" >}}) as long as they shell out to Bash or PowerShell and aren't passed a Ruby block.
Chef Infra Client returns this error if multiple recipes have the same filename but a different file extension. For example, `default.yaml` and `default.yml`.
273
+
Chef Infra Client returns this error if two recipes have the same filename with different file extensions. For example, `default.yaml` and `default.yml`.
263
274
264
275
```text
265
276
Chef::Exceptions::AmbiguousYAMLFile: Found both default.yml and default.yaml in cookbook, update the cookbook to remove one
0 commit comments