YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: IT Automation with Chef

IT Automation with Chef

Anuchit [email protected]

Page 2: IT Automation with Chef

Chef Server*hosted*

Node*chef-client*

Workstation*chef*

Git

Page 3: IT Automation with Chef

Tools

● Chef client tools for admin workstation● Chef server● Node with bootstrap

Page 4: IT Automation with Chef

Chef client

Install from http://www.getchef.com/chef/install/ choose match with your operating system and arch.

Page 5: IT Automation with Chef

Chef client

* for Linux and Mac user can use shell script installer from curl :)

Page 6: IT Automation with Chef

Chef server

You can use on premises chef server or use hosted chef. choose your version at http://www.getchef.com/chef/choose-your-version/

Page 7: IT Automation with Chef

Chef server

Page 8: IT Automation with Chef

Chef server

Page 9: IT Automation with Chef

Starter kit

Download Starter Kit on Administration page, then extract to your home directory.

Page 10: IT Automation with Chef

Starter kit

Page 11: IT Automation with Chef

Git repository

Chef need repository for your cookbook. Change to your chef-repo and using git to init your repository.

git initgit add .git commit -m “add starter kit”

* Ref : Customized Git Configuration

Page 12: IT Automation with Chef

Try knife

Change to your Chef repo directory and use knife command line eg: knife client list

knife client list

Page 13: IT Automation with Chef

Node bootstrap

To add node to Chef server, use knife bootstrap to your node.knife bootstrap fqdn/ip --sudo -x username -P passwd -N "nodename"

knife bootstrap 192.168.2.138 --sudo -x username -P passwd -N "nodename"

Page 14: IT Automation with Chef

Node bootstrap

Page 15: IT Automation with Chef

Node bootstrap

Page 16: IT Automation with Chef

Cookbook

Now write your first cookbook call apache, use knife to create new cookbook.

knife cookbook create apache

Page 17: IT Automation with Chef

Cookbook : Apache

Edit cookbooks/apache/recipes/default.rb to create your recipe, with following structure

# install apache...

# start the apache service make sure the service starts...

# write our home page...

Page 18: IT Automation with Chef

Cookbook : Apache# install apachepackage "apache2" do

action :installend

# start the apache service make sure the service startsservice "apache2" do

action [ :enable, :start]end

# write our home pagecookbook_file "/var/www/index.html" do

source "index.html"mode "0644"

end

Page 19: IT Automation with Chef

Cookbook : Apache

At the last part in cookbook_file you must have index.html in cookbooks/apache/files/default/index.html write your own content.

<html> <title>Hello World from Chef</title><body> <h1>Hello World from Chef</h1></body></html>

Page 20: IT Automation with Chef

Cookbook upload

Each part call resources, which are step to install apache, enable service and create default html file. Now you are ready to publish your cookbook.

knife cookbook upload apache

Page 21: IT Automation with Chef

Cookbook

Ubuntu need you to update APT cache before install any package the you shuld create apt cookbook to update APT cache first

knife cookbook create apt

Page 22: IT Automation with Chef

Cookbook : apt

Use the execute command to update APT cache

execute "apt-get update" docommand "apt-get update"

end

Page 23: IT Automation with Chef

Cookbook upload

Now you are ready to publish your apt cookbook.

knife cookbook upload apt

Page 24: IT Automation with Chef

Cookbook

Page 25: IT Automation with Chef

Run list

After upload cookbook to Chef server, you must create a run list for the node to apply recipes.

Page 26: IT Automation with Chef

Run list

Page 27: IT Automation with Chef

Run list

Page 28: IT Automation with Chef

Chef Client

Now back to your node, run command sudo chef-client to apply run list.

sudo chef-client

* Run remote by knife: knife ssh ‘name:*’ ‘sudo chef-client’ -x username -P password

Page 29: IT Automation with Chef

Check the result

Open your browser and browse to your node with an ip address or fqdn.

Page 30: IT Automation with Chef

Check the report

Goto Chef server to check your report; success, failure, aborted.

Page 31: IT Automation with Chef

Reports

Page 32: IT Automation with Chef

Reports

Page 33: IT Automation with Chef

Make more dynamic

Your first recipe support only Ubuntu node, then make support another distributions you may add following items for more dynamically

● Attributes● Templates● Metadata

Page 34: IT Automation with Chef

Attributes

Add cookbooks/apache/attributes/default.rb as a default values for your recipes.

case node["platform_family"]when "debian" default["package_name"] = "apache2" default["service_name"] = "apache2" default["document_root"] = "/var/www"when "rhel" default["package_name"] = "httpd" default["service_name"] = "httpd" default["document_root"] = "/var/www/html"end

Page 35: IT Automation with Chef

Recipes# install apachepackage node["package_name"] do

action :installend

# start the apache service# make sure the service startsservice node["service_name"] do

action [ :enable, :start]end

# write our home pagetemplate "#{node["document_root"]}/index.html" do

source "index.html.erb"mode "0644"

end

Page 36: IT Automation with Chef

Templates

Add template file for your index.html in cookbooks/apache/templates/default/index.html.erb

<html><title>Hello World</title><body>

<h1>Hello World from <%= node["fqdn"] %>!</h1></body></html>

Page 37: IT Automation with Chef

Metadata

Edit metadata file in cookbooks/apache/metadata.rb

name 'apache'maintainer 'Anuchit Chalothorn'maintainer_email '[email protected]'license 'All rights reserved'description 'Installs/Configures apache'long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))version '0.1.0'

Page 38: IT Automation with Chef

Rules

Roles allow you to encapsulate run lists and attribute required for a server to be. eg:

● Web Server● Database Server● etc

Page 39: IT Automation with Chef

Rules

Create role for your web server by adding a webserver.rb in roles directory edit with following content.

name "webserver"description "Web Server"run_list "recipe[apache]"

default_attributes({"company" => "RedLineSoft"

})

Page 40: IT Automation with Chef

Rules

Create role for your web server by adding a base-ubuntu.rb in roles directory edit with following content.

name "base-ubuntu"description "Base Ubuntu"run_list "recipe[apt]"

Page 41: IT Automation with Chef

Rules

Update your cookbook version in metadata and add new role with following knife command

knife role create from file webserver.rb

knife role create from file base-ubuntu.rb

knife cookbook upload apache

Page 42: IT Automation with Chef

Rules

Goto Chef server and add new roles to your node instead of using cookbook then use chef-client to apply recipes

Page 43: IT Automation with Chef

Rules

Page 44: IT Automation with Chef

Community Cookbook

Chef also have a community cookbooks. You can find an interest cookbook at http://community.opscode.com/cookbooks

Page 45: IT Automation with Chef

Community Cookbook

Page 46: IT Automation with Chef

Community Cookbook

You can use community cookbook from Chef community by using knife.

knife cookbook site download mysql 4.1.2

You'll get an archive file mysql-4.1.2.tar.gz in your chef-repo

* Easy way use; knife cookbook site install mysql

Page 47: IT Automation with Chef

Using Community Cookbook

Now you get the archive cookbook from community already then extract an archive to cookbooks directory

tar zxvf mysql-4.1.2.tar.gz -C cookbooks/

Page 48: IT Automation with Chef

Using Community Cookbook

Check dependency in metadata.rb, if you don’t have please download them, for homebrew, windows is dependency for OSX and Windows if you not use it, just comment it.

depends 'openssl', '~> 1.1'depends 'build-essential', '~> 1.4'

#depends 'homebrew'#depends 'windows'

Page 49: IT Automation with Chef

Using Community Cookbook

Read the recipe files, mysql cookbook has mysql::client, mysql::server and mysql::ruby so you can specify which recipe you will use.

Page 50: IT Automation with Chef

Using Community Cookbook

Download dependency cookbook for mysql

knife cookbook site download openssl 1.1.0knife cookbook site download build-essential 1.4.4tar zxvf openssl-1.1.0.tar.gz -C cookbooks/tar zxvf build-essential-1.4.4.tar.gz -C cookbooks/

knife cookbook upload build-essential openssl mysql

Page 51: IT Automation with Chef

Using Community Cookbook

Create new role webserver-mysql to install webserver and mysql in this role.

name "webserver-mysql"description "Webserver and MySQL Database Server"run_list "recipe[apache]","recipe[mysql::client]","recipe[mysql::server]"

knife role from file webserver-mysql.rb

Page 52: IT Automation with Chef

Using Community Cookbook

Goto Chef server add role to node then apply webserver-mysql role in node

Page 53: IT Automation with Chef

Using Community Cookbook

Page 54: IT Automation with Chef

Using Community Cookbook

Goto node and run chef-client to apply role.

sudo chef-client

Page 55: IT Automation with Chef

Further Resources

● http://www.opscode.com● http://community.opscode.com● http://docs.opscode.com● http://learnchef.com● http://youtube.com/user/Opscode

Page 56: IT Automation with Chef

Related Documents