Check out my primer chef document for how to initially configure your chef test environment with vagrant. This document assumes you are starting from there.
Apache Tutorial Cookbook
These are my expanded notes from one of the official docs . Before we proceed, let’s take a quick look at the default chef-repo tree:
1
2
3
4
5
6
7
8
chef-repo
├── .chef
├── certificates
├── config
├── cookbooks
├── data_bags
├── environments
└── roles
When you are inside that directory and have already configured your knife config file, you can start a cookbook with:
1
knife cookbook create apache_tutorial
Let’s see how this changes our workspace:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
chef-repo
└── cookbooks
└── apache_tutorial
├── attributes
├── CHANGELOG.md
├── definitions
├── files
│ └── default
├── libraries
├── metadata.rb
├── providers
├── README.md
├── recipes
│ └── default.rb
├── resources
└── templates
└── default
Make the following modifications:
recipes/default.rb
:
1
2
3
4
5
6
7
8
9
10
11
12
package "apache2" do
action :install
end
service "apache2" do
action [ :enable, :start ]
end
cookbook_file "/var/www/index.html" do
source "index.html"
mode "0644"
end
files/default/index.html
:
1
2
3
4
5
<html>
<body>
<h1>Hello, world!</h1>
</body>
</html>
Upload that recipe to the chef server:
1
$ knife cookbook upload apache_tutorial
Run List
Now we need to add that recipe to our node’s run list
. To verify the run list of a node:
1
2
3
4
5
6
7
8
9
10
$ knife node show chefclient
Node Name: chefclient
Environment: _default
FQDN: ChefClient
IP: 10.0.2.15
Run List:
Roles:
Recipes:
Platform: ubuntu 12.04
Tags:
Let’s add our apache recipe to that node’s runlist and confirm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ knife node run_list add chefclient apache_tutorial
chefclient:
run_list: recipe[ apache_tutorial]
$ knife node show chefclient
Node Name: chefclient
Environment: _default
FQDN: ChefClient
IP: 10.0.2.15
Run List: recipe[ apache_tutorial]
Roles:
Recipes:
Platform: ubuntu 12.04
Tags:
To force our chefclient to run this recipe and check into our chef server:
1
$ knife ssh "name:chefclient" -x vagrant -P vagrant "sudo chef-client"
References