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: docs/applications/configuration-management/automatically-configure-servers-with-ansible-and-playbooks/index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -279,9 +279,9 @@ This section demonstrates using a playbook to automate basic server configuratio
279
279
280
280
### Install the Stack
281
281
282
-
Create a basic server setup with Apache, PHP, and a test MySQL database to use.
282
+
Create a basic server setup with NGINX, PHP, and a test MySQL database to use.
283
283
284
-
1. The following playbook downloads the appropriate packages, turns on the Apache and MySQL services, and creates a basic database and user:
284
+
1. The following playbook downloads the appropriate packages, turns on the nginx and MySQL services, and creates a basic database and user:
Cookbooks are one of the key components in Chef. They describe the *desired state* of your nodes, and allow Chef to push out the changes needed to achieve this state. Creating a cookbook can seem like an arduous task at first, given the sheer number of options provided and areas to configure, so in this guide we will walk through the creation of one of the first things people often learn to configure: A LAMP stack.
21
+
Chef cookbooks describe the *desired state* of your nodes, and allow Chef to push out the changes needed to achieve this state. In this guide you will learn how to create a cookbook that configures A LAMP stack on a Linode.
19
22
20
23

21
24
22
-
Prior to using this guide, set up Chef with the [Setting Up a Chef Server, Workstation, and Node](/docs/applications/configuration-management/install-a-chef-server-workstation-on-ubuntu-18-04/) guide. When following that guide, **choose Ubuntu 16.04 as your Linux image for the Chef node**. This is required because the [MySQL Chef cookbook](https://supermarket.chef.io/cookbooks/mysql/) that will be used is not yet compatible with Ubuntu 18.04.
25
+
## Before You Begin
23
26
24
-
If needed, review the [Beginner's Guide to Chef](/docs/applications/configuration-management/beginners-guide-chef/).
27
+
1. Set up Chef with the [Setting Up a Chef Server, Workstation, and Node](/docs/applications/configuration-management/install-a-chef-server-workstation-on-ubuntu-18-04/) guide. When following that guide, **choose Ubuntu 16.04 as your Linux image for the Chef node you will bootstrap and manage**. This guide will use the [MySQL Chef cookbook](https://supermarket.chef.io/cookbooks/mysql/), which does not yet support Ubuntu 18.04.
25
28
26
-
The examples in this tutorial require a root user account. Readers who choose to use a limited user account will need to prefix commands with sudo where required when working on the Chef client node. If you have yet to create a limited user account, follow the steps in the [Securing Your Server](/docs/security/securing-your-server/#add-a-limited-user-account) guide.
29
+
1. Once your node is bootstrapped, you can use a Chef cookbook to secure your node. Consider using the [Users](https://supermarket.chef.io/cookbooks/users) cookbook and the [Firewall](https://supermarket.chef.io/cookbooks/firewall) cookbook for this work. While this is not required to complete this guide, it is recommended.
30
+
31
+
1. You can also review [A Beginner's Guide to Chef](/docs/applications/configuration-management/beginners-guide-chef/)to receive an overview on Chef concepts.
32
+
33
+
1. The examples in this tutorial require a user account with sudo privileges. Readers who use a limited user account will need to prefix commands with sudo when issuing commands to the Chef client node and replace `-x root` with `-x username` where `username` is your limited user account.
34
+
35
+
1. Ensure that your workstation's `/etc/hosts` file contains its own IP address and hostname and the IP address and hostname for any nodes you will interact with from the workstation. For example:
36
+
37
+
{{< file "/etc/hosts">}}
38
+
127.0.0.1 localhost
39
+
192.0.2.0 workstation
40
+
198.51.100.0 node-hostname
41
+
{{</ file >}}
27
42
28
43
## Create the Cookbook
29
44
30
-
1. From your workstation, move to your `cookbooks` directory in `chef-repo`:
45
+
1. From your workstation, move to your `chef-repo/cookbooks` directory:
31
46
32
47
cd chef-repo/cookbooks
33
48
@@ -39,27 +54,25 @@ The examples in this tutorial require a root user account. Readers who choose to
39
54
40
55
cd lamp_stack
41
56
42
-
1. List the files located in the newly-created cookbook to see that a number of directories and files have been created:
43
-
44
-
ls
57
+
If you issue the `ls` command, you should see the following files and directories:
45
58
46
-
{{< output >}}
47
-
Berksfile CHANGELOG.md chefignore LICENSE metadata.rb README.md recipes spec test
48
-
{{</ output >}}
59
+
{{< output >}}
60
+
Berksfile CHANGELOG.md chefignore LICENSE metadata.rb README.md recipes spec test
61
+
{{</ output >}}
49
62
50
-
For more information about these directories see the [Beginner's Guide to Chef](/docs/applications/configuration-management/beginners-guide-chef/).
63
+
### default.rb
51
64
52
-
## default.rb
65
+
Attributes are pieces of data that help the chef-client determine the current state of a node and any changes that have taken place on the node from one chef-client run to another. Attributes are gathered from the state of the node, cookbooks, roles and environments. Using these sources, an attribute list is created for each chef-client run and is applied to the node. If a `default.rb` file exists within a cookbook, it will be loaded first, but has the lowest attribute precedence.
53
66
54
67
The `default.rb` file in `recipes` contains the "default" recipe resources.
55
68
56
-
Because each section of the LAMP stack (Apache, MySQL, and PHP) will have its own recipe, the `default.rb` file is used to prepare your servers.
69
+
In this example, the `lamp_stack` cookbook's `default.rb` file is used to update the node's distribution software.
57
70
58
-
1. From within your`lamp_stack` directory, navigate to the `recipes` folder:
71
+
1. From within the`lamp_stack` directory, navigate to the `recipes` folder:
59
72
60
73
cd recipes
61
74
62
-
1. Open `default.rb` and add the Ruby command below, which will run system updates:
75
+
1. Open the `default.rb`file and add the following code:
Because this is the default recipe, the recipe name does not need to be defined after `lamp_stack` cookbook in the code above.
95
-
96
-
1. Access your chosen node and run the *chef-client*:
113
+
1. From your workstation, apply the configurations defined in the cookbook by running the chef-client on your node. Replace `nodename` with the name of your node:
It should output a successful Chef run. If not, review your code for any errors, usually defined in the output of the `chef-client` run.
117
+
Your output should display a successful Chef run. If not, review your code for any errors, usually defined in the output of the `chef-client` run.
101
118
102
119
## Apache
103
120
@@ -139,20 +156,26 @@ end
139
156
140
157
Because this is not the `default.rb` recipe, the recipe name, *apache*, must be appended to the recipe value.
141
158
142
-
1. From that **node**, run `chef-client`:
159
+
{{< note >}}
160
+
To view a list of all nodes managed by your, Chef server issue the following command from your workstation:
161
+
162
+
knife node list
163
+
{{</ note >}}
164
+
165
+
1. From your workstation, apply the configurations defined in the cookbook by running the chef-client on your node. Replace `nodename` with the name of your node:
If the recipe fails due to a syntax error, Chef will note it during the output.
147
170
148
171
1. After a successful `chef-client` run, check to see if Apache is running:
149
172
150
-
systemctl status apache2
151
-
152
-
It should say that `apache2` is running.
173
+
knife ssh 'name:nodename' 'systemctl status apache2' -x root
153
174
154
175
{{< note >}}
155
-
Repeat Steps 5-7 to upload the cookbook and run chef-client as needed through the rest of this guide to ensure your recipes are working properly and contain no errors. Remember to replace the recipe name in the run list code when adding a new recipe.
176
+
Repeat steps 4-7 to upload each recipe to your Chef server, as you create it. Run `chef-client` on your node, as needed, throughout the rest of this guide to ensure your recipes are working properly and contain no errors. When adding a new recipe, ensure you are using its correct name in the run list.
177
+
178
+
This is not the recommended workflow for a production environment. You might consider creating different [Chef environments](https://docs.chef.io/environments.html) for testing, staging, and production.
156
179
{{< /note >}}
157
180
158
181
### Configure Virtual Hosts
@@ -163,7 +186,7 @@ This configuration is based off of the [How to Install a LAMP Stack on Ubuntu 16
163
186
164
187
chef generate attribute ~/chef-repo/cookbooks/lamp_stack default
165
188
166
-
1. Within the new `default.rb`, create the default values of the cookbook:
189
+
1. Within the new `default.rb`, create the default values for the cookbook:
@@ -210,7 +233,7 @@ node["lamp_stack"]["sites"].each do |sitename, data|
210
233
end
211
234
{{< /file >}}
212
235
213
-
1.However, this does not create the directory itself. To do so, the `directory` resource should be used, with a `true` recursive value so all directories leading up to the `sitename` will be created. A permissions value of `0755` allows for the file owner to have full access to the directory, while group and regular users will have read and execute privileges:
236
+
1.Create the `document_root` directory. Declare a `directory` resource with a `true` recursive value so all directories leading up to the `sitename` will be created. A permissions value of `0755` allows for the file owner to have full access to the directory, while group and regular users will have read and execute privileges:
node["lamp_stack"]["sites"].each do |sitename, data|
@@ -275,7 +298,7 @@ end
275
298
276
299
The name of the template resource should be the location where the virtual host file is placed on the nodes. The `source` is the name of the template file. Mode `0644` gives the file owner read and write privileges, and everyone else read privileges. The values defined in the `variables` section are taken from the attributes file, and they are the same values that are called upon in the template.
277
300
278
-
1. The sites now need to be enabled in Apache, and the server restarted. This should *only* occur if there are changes to the virtual hosts, so the `notifies` value should be added to the `template` resource. `notifies` tells Chef when things have changed, and **only then** runs the commands:
301
+
1. The sites need to be enabled in Apache, and the server restarted. This should *only* occur if there are changes to the virtual hosts, so the `notifies` value should be added to the `template` resource. `notifies` tells Chef when things have changed, and **only then** runs the commands:
template "/etc/apache2/sites-available/#{sitename}.conf" do
@@ -294,7 +317,7 @@ end
294
317
295
318
The `notifies` command names the `:action` to be committed, then the resource, and resource name in square brackets.
296
319
297
-
1.`notifies` can also call on `execute` commands, which will run `a2ensite`and enable the sites we've made virtual hosts files for. Add the following `execute` command **above** the `template` resource code to create the `a2ensite` script:
320
+
1.`notifies` can also call on `execute` commands, which will run `a2ensite`and enable the sites that have corresponding virtual hosts files. Add the following `execute` command **above** the `template` resource code to create the `a2ensite` script:
@@ -458,7 +481,7 @@ Chef contains a feature known as *data bags*. Data bags store information, and c
458
481
knife data bag create mysql rtpass.json --secret-file ~/chef-repo/.chef/encrypted_data_bag_secret
459
482
460
483
{{< note >}}
461
-
Some knife commands require that information be edited as JSON data using a text editor. Your `knife.rb` file should contain a configuration for the text editor to use for such commands. If your `knife.rb` file does not already contain this configuration, add `knife[:editor] = "/usr/bin/vim"` to the bottom of the file to set vim as the default text editor.
484
+
Some knife commands require that information be edited as JSON data using a text editor. Your `config.rb` file should contain a configuration for the text editor to use for such commands. If your `config.rb` file does not already contain this configuration, add `knife[:editor] = "/usr/bin/vim"` to the bottom of the file to set vim as the default text editor.
462
485
{{</ note >}}
463
486
464
487
You will be asked to edit the `rtpass.json` file:
@@ -527,12 +550,6 @@ end
527
550
528
551
`mysqldefault` is the name of the MySQL service for this container. The `inital_root_password` calls to the value defined in the text above, while the action creates the database and starts the MySQL service.
529
552
530
-
{{< note >}}
531
-
When running MySQL from your nodes you will need to define the socket:
532
-
533
-
mysql -S /var/run/mysql-mysqldefault/mysqld.sock -p
534
-
{{< /note >}}
535
-
536
553
## PHP
537
554
538
555
1. Under the recipes directory, create a new `php.rb` file. The commands below install PHP and all the required packages for working with Apache and MySQL:
You have just created a LAMP Stack cookbook. Through this guide, you should have learned to use the execute, package, service, node, directory, template, cookbook_file, and mysql_service resources within a recipe, as well as download and use LWRPs, create encrypted data bags, upload/update your cookbooks to the server, and use attributes, templates, and cookbook files, giving you a strong basis in Chef and cookbook creation for future projects.
619
+
You have just created a LAMP Stack cookbook. Through this guide, you should have learned to use the execute, package, service, node, directory, template, cookbook_file, and mysql_service resources within a recipe, as well as download and use LWRPs, create encrypted data bags, upload/update your cookbooks to the server, and use attributes, templates, and cookbook files. This gives you a strong basis in Chef and cookbook creation for future projects.
0 commit comments