Once
that we configure the chef server , let write a simple cookbook and see how it
is executed. Run the commands in color to create a sample cookbook
[root@manja17-I14020
.chef]# cd
~/chef-repo/cookbooks/
[root@manja17-I14020
cookbooks]# chef
generate cookbook httpd
Generating
cookbook httpd
-
Ensuring correct cookbook file content
-
Ensuring delivery configuration
-
Ensuring correct delivery build cookbook content
Your
cookbook is ready. Type `cd httpd` to enter it.
There
are several commands you can run to get started locally developing and testing
your cookbook.
Type
`delivery local --help` to see a full list.
Why
not start by writing a test? Tests for the default recipe are stored at:
test/recipes/default_test.rb
If
you'd prefer to dive right in, the default recipe can be found at:
recipes/default.rb
[root@manja17-I14020
cookbooks]# cd httpd/
[root@manja17-I14020
httpd]# ll
total
16
-rw-r--r--.
1 root root 47 Sep 3 05:47 Berksfile
-rw-r--r--.
1 root root 1133 Sep 3 05:47 chefignore
-rw-r--r--.
1 root root 568 Sep 3 05:47 metadata.rb
-rw-r--r--.
1 root root 53 Sep 3 05:47 README.md
drwxr-xr-x.
2 root root 24 Sep 3 05:47 recipes
drwxr-xr-x.
3 root root 40 Sep 3 05:47 spec
drwxr-xr-x.
3 root root 21 Sep 3 05:47 test
Add
the code to the default.rb file
Go to "~/chef-repo/cookbooks/httpd/recipes" and add
the below content as,
[root@chefdk
recipes]# cat default.rb
#
#
Cookbook Name:: httpd
#
Recipe:: default
#
#
Copyright (c) 2018 The Authors, All Rights Reserved.
package
'httpd' do
action :install
end
service
'httpd' do
action [ :enable, :start ]
end
cookbook_file
"/var/www/html/index.html" do
source "index.html"
mode "0644"
end
Also
create a file “index.html” in the httpd/files location. This will the same file
that we will copy to the chef-nodes for httpd server. This is the home page for
the httpd server.
Once
that we have written the code, upload the cookbook to the chef server using,
[root@manja17-I14020
recipes]# knife cookbook upload httpd
Uploading
httpd [0.1.0]
Uploaded
1 cookbook.
Check
the available cookbooks in server using,
[root@manja17-I14020
recipes]# knife cookbook list
httpd 0.1.0
Now add
the cookbook to the run_list using,
[root@manja17-I14020
recipes]# knife node run_list add firstnode httpd
firstnode:
run_list: recipe[httpd]
Run lists define what
cookbooks a node will use. The run list is an ordered list of all cookbooks and
recipes that the chef-client needs to pull from the Chef server to run on a
node. Run lists are also used to define roles, which are used to define patterns and attributes across
nodes.
Run the Cook Book Manually on the chef-node
using,
[root@manja17-I14022 ~]# chef-client -r
recipe[httpd]
[2018-09-03T06:19:54-04:00]
INFO: Forking chef instance to converge...
Starting
Chef Client, version 12.22.5
[2018-09-03T06:19:54-04:00]
INFO: *** Chef 12.22.5 ***
[2018-09-03T06:19:54-04:00]
INFO: Platform: x86_64-linux
[2018-09-03T06:19:54-04:00]
INFO: Chef-client pid: 29567
[2018-09-03T06:19:54-04:00]
INFO: The plugin path /etc/chef/ohai/plugins does not exist. Skipping...
[2018-09-03T06:19:57-04:00]
INFO: Setting the run_list to
[#<Chef::RunList::RunListItem:0x0000000001d9ac50 @version=nil,
@type=:recipe, @name="httpd">] from CLI options
[2018-09-03T06:19:57-04:00]
INFO: Run List is [recipe[httpd]]
[2018-09-03T06:19:57-04:00]
INFO: Run List expands to [httpd]
[2018-09-03T06:19:57-04:00]
INFO: Starting Chef Run for firstnode
[2018-09-03T06:19:57-04:00]
INFO: Running start handlers
[2018-09-03T06:19:57-04:00]
INFO: Start handlers complete.
[2018-09-03T06:19:57-04:00]
INFO: HTTP Request Returned 404 Not Found:
[2018-09-03T06:19:57-04:00]
INFO: HTTP Request Returned 404 Not Found:
[2018-09-03T06:19:57-04:00]
INFO: Error while reporting run start to Data Collector. URL:
https://chefserver.nova.com/organizations/nova/data-collector Exception: 404 --
404 "Not Found" (This is
normal if you do not have Chef Automate)
resolving
cookbooks for run list: ["httpd"]
[2018-09-03T06:19:57-04:00]
INFO: Loading cookbooks [httpd@0.1.0]
Synchronizing
Cookbooks:
[2018-09-03T06:19:57-04:00]
INFO: Storing updated cookbooks/httpd/files/index.html in the cache.
- httpd (0.1.0)
Installing
Cookbook Gems:
Compiling
Cookbooks...
[2018-09-03T06:19:57-04:00]
INFO: HTTP Request Returned 404 Not Found:
Converging
3 resources
Recipe:
httpd::default
* yum_package[httpd] action
install[2018-09-03T06:19:57-04:00] INFO: Processing yum_package[httpd] action
install (httpd::default line 7)
(up to date)
* service[httpd] action
enable[2018-09-03T06:19:59-04:00] INFO: Processing service[httpd] action enable
(httpd::default line 11)
(up to date)
* service[httpd] action
start[2018-09-03T06:19:59-04:00] INFO: Processing service[httpd] action start
(httpd::default line 11)
(up to date)
* cookbook_file[/var/www/html/index.html]
action create[2018-09-03T06:19:59-04:00] INFO: Processing
cookbook_file[/var/www/html/index.html] action create (httpd::default line 15)
[2018-09-03T06:19:59-04:00]
INFO: cookbook_file[/var/www/html/index.html] created file /var/www/html/index.html
- create new file
/var/www/html/index.html[2018-09-03T06:19:59-04:00] INFO:
cookbook_file[/var/www/html/index.html] updated file contents
/var/www/html/index.html
- update content in file
/var/www/html/index.html from none to e3b0c4
(no diff)[2018-09-03T06:19:59-04:00] INFO:
cookbook_file[/var/www/html/index.html] mode changed to 644
- change mode from '' to '0644'
- restore selinux security context
[2018-09-03T06:20:00-04:00]
INFO: Chef Run complete in 2.342676017 seconds
Running
handlers:
[2018-09-03T06:20:00-04:00]
INFO: Running report handlers
Running
handlers complete
[2018-09-03T06:20:00-04:00]
INFO: Report handlers complete
Chef
Client finished, 1/4 resources updated in 05 seconds
This
is how we will be writing a simple cookbook and automate them on the chef-nodes.
No comments :
Post a Comment