1. Ensure the apt cache is up to date
In this part you'll configure Apache. Because we want to install the latest version of Apache, it's important to ensure that your system's package index contains the latest list of what packages are available.
You could run the
apt-get update
command manually when you bring up your instance. But over time, you would need to remember to periodically update the apt
cache to get the latest updates. Chef provides the apt_update resource to automate the process.
From your
~/chef-repo
directory, add this code to a file named webserver.rb
.Editor: ~/chef-repo/webserver.rb
1 2 3 4 | apt_update 'Update the apt cache daily' do frequency 86_400 action :periodic end |
In a production environment, you might run Chef periodically to ensure your systems are kept up to date. As an example, you might run Chef multiple times in a day. However, you likely don't need to update the
apt
cache every time you run Chef. The frequency
property specifies how often to update the apt
cache (in seconds.) Here, we specify 86,400 seconds to update the cache once every 24 hours. (The _
notation is a Ruby convention that helps make numeric values more readable.)
The
:periodic
action means that the update occurs periodically. Another option would be to use the :update
action to update the apt
cache each time Chef runs.2. Install the Apache package
Now let's install the Apache package,
apache2
. Modify webserver.rb
to look like this.Editor: ~/chef-repo/webserver.rb
1 2 3 4 5 6 | apt_update 'Update the apt cache daily' do frequency 86_400 action :periodic end package 'apache2' |
We don't need to specify an action because
:install
is the default.
Now run
chef-client
to apply the recipe.Terminal: ~/chef-repo
sudo
is required because this command installs a package and therefore must be run with root privileges.
Run the recipe a second time.
Terminal: ~/chef-repo
You see that Chef does not apply any changes because there's nothing to do – it's not yet time to update the
apt
cache and the Apache package is already installed.3. Start and enable the Apache service
Now let's first enable the Apache service when the server boots and then start the service. Modify
webserver.rb
to look like this.Editor: ~/chef-repo/webserver.rb
1 2 3 4 5 6 7 8 9 10 11 | apt_update 'Update the apt cache daily' do frequency 86_400 action :periodic end package 'apache2' service 'apache2' do supports :status => true action [:enable, :start] end |
This code declares multiple actions.
Ubuntu 14.04 provides two init systems. The
supports :status => true
part tells Chef that the apache2
init script supports the status
message. This information helps Chef use the appropriate strategy to determine if the apache2
service is running. If you're interested, read this blog post for more information.
Now apply it.
Terminal: ~/chef-repo
The package is already installed, so there's nothing to do. Similarly, the service is already started and enabled. On some Linux distros, Apache is not started or enabled as it is installed. With Chef, this is easy to verify.
4. Add a home page
Let's spruce things up and add a custom home page.
You already know how to configure a
file
resource; append one that configures the default home page,/var/www/html/index.html
, to the end of webserver.rb
. The entire recipe now looks like this.Editor: ~/chef-repo/webserver.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | apt_update 'Update the apt cache daily' do frequency 86_400 action :periodic end package 'apache2' service 'apache2' do supports :status => true action [:enable, :start] end file '/var/www/html/index.html' do content ' |
Versions of Ubuntu prior to 14.04 use
/var/www/index.html
as the default home page, so adjust your recipe accordingly if your version of Ubuntu is prior to 14.04.
Now apply it.
Terminal: ~/chef-repo
hello world
- Running handlers:Running handlers completeChef Client finished, 1/5 resources updated in 02 seconds5. Confirm your web site is running
Run the
curl
command to confirm that your web page is available.Terminal: ~/chef-repo
Normally, you could also access your web server from a browser on another machine. However, the free trial virtual machine does not have a public IP address.
Summary
You saw how to work with the package and service resources. You now know how to work with four types of resources: file,apt_update, package, and service.
You also saw how to apply multiple actions. But how does Chef know what order to apply resources and actions?
Chef works in the order you specify
Let's take another quick look at our web server recipe.
Editor: ~/chef-repo/webserver.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | apt_update 'Update the apt cache daily' do frequency 86_400 action :periodic end package 'apache2' service 'apache2' do supports :status => true action [:enable, :start] end file '/var/www/html/index.html' do content ' |
The resources are applied in the order they are specified in the recipe. So here the
apt
cache is updated, the package is installed, then the service is configured, and finally the home page is set. If any resource is already in the desired state, Chef simply moves on to the next one.
The same idea applies to the action list
[:enable, :start]
for configuring the service. The service is enabled when the server boots and then it's started.
It's important to always think about how you order things. For example, the recipe wouldn't work if we tried to configure the Apache service before the package is even installed.
A recipe stops if any step fails (don't worry – Chef provides info about the error). That's why we ordered the service actions the way we did. If the service can't be enabled on boot then we don't want to start it.
Exercises
Exercise 1
Are these two recipes the same?
Editor: webserver.rb
1 2 3 4 5 | package 'apache2' service 'apache2' do supports :status => true action [:enable, :start] end |
Editor: webserver.rb
1 2 3 4 5 | service 'apache2' do supports :status => true action [:enable, :start] end package 'apache2' |
Exercise 2
Are these two recipes the same?
Editor: webserver.rb
1 2 3 4 5 | package 'apache2' service 'apache2' do supports :status => true action [:enable, :start] end |
Editor: webserver.rb
1 2 3 4 5 | package 'apache2' service 'apache2' do supports :status => true action [:start, :enable] end |
Exercise 3
Are these two recipes the same?
Editor: file.rb
1 2 3 4 5 6 | file '/etc/motd' do owner 'root' group 'root' mode '0755' action :create end |
Editor: file.rb
1 2 3 4 5 6 | file '/etc/motd' do action :create mode '0755' group 'root' owner 'root' end |
Exercise 4
Write a
service
resource that stops and then disables the apache2
service from starting when the system boots.
Hint: The documentation for the
service
resource lists the available actions.
No comments:
Post a Comment