Beruflich Dokumente
Kultur Dokumente
----------------------------------------
vi hello.rb
---------------------------
file 'sample.txt' do
content 'hello world'
end
---------------------------
Now run the below command to execute it
$chef-apply hello.rb
It created a sample.txt resource file
Now re run same again, it will not do anything as the files has been already
created.
3. Now we will see more than a file creation using chef recipies
Like files, packages and services are also resources, and chef applies them
in an order which you specify
service 'httpd' do
action [:enable, :start]
end
file '/var/www/html/index.html' do
content '<html>
<body>
<h1>hello world</h1>
</body>
</html>'
end
-----------------------
stop the iptables service to access it from external
----------------
service 'iptables' do
action :stop
end
------------------
4. Now lets make our recipe more manageble, as the html file resides inside the
recipe which is complex for practically doing it.
-We can manage our recipes with cookbooks concept in chef, so lets see that
- create a directory cookbooks
mkdir cookbooks
cd cookbooks
*Note: the cookbooks directory mandatory as chef-client sets this as
chef_repo_path by default to search for cookbooks
- Now run below command to generate a cookbook
chef generate cookbook learn_chef_httpd
- The above command created belwo directory structure
+-- learn_chef_httpd
+-- Berksfile
+-- chefignore
+-- metadata.rb
+-- README.md
+-- recipes
+-- default.rb
- Now we will move index.html to some other file by creating templates
chef generate template learn_chef_httpd index.html
- the above command creates a templates/default/index.html.rb template
placeholder file and add below content
<html>
<body>
<h1>hello world</h1>
</body>
</html>
- Now we will update this reference template in original recipe default.rb
vi default.rb
----------------------
package 'httpd'
service 'httpd' do
action [:enable, :start]
end
template '/var/www/html/index.html' do
source 'index.html.erb'
end
service 'iptables' do
action :stop
end
--------------
- Now lets run the cookbook with above all configurations
we will use chef-client to run cookbooks instead of chef-apply
$sudo chef-client --local-mode --runlist 'recipe[learn_chef_httpd]'
- The above command runs the default recipe of the given cookbook, in case if
you want to run other recipe
$sudo chef-client --local-mode --runlist 'recipe[learn_chef_httpd:abc.rb]'
this one will run abc.rb recipe in abvoe cookbook
5. Now Lets try to do all these configuration deployments in remote nodes instead
of in same machine
The important elements of chef are three
1. WorkStation : Which is a machien where you run your cookbooks to apply on
server,and it can be any OS
2. Chef Server : is a repository where it manages your cookbooks and also the
nodes details and configs
3. Nodes : is an instance manages by chefserver, where it has chef-client
to talk to chef-server
- Setup a workstation by installing chef SDK or you can make use of earlier
instance as workstation
- Follow instruciton in below website or follow instrucitons here
https://learn.chef.io/manage-a-node/rhel/set-up-your-chef-server/
- Login hosted chef and crete an organisation with uniq name and short name
- Click on org created and left side you can see started-kit, download it to
your workstation.
- Now copy your cookbook inside the downloaded started kit dir
Run below command to upload the cookbook to chef-server
sudo knife cookbook upload learn_chef_httpd
/***********************************************************/
Writing CookBooks: (https://www.digitalocean.com/community/tutorials/how-to-create-
simple-chef-cookbooks-to-manage-infrastructure-on-ubuntu)
-------------------
What is cookbook
-Cookbooks serve as the fundamental unit of configuration and policy details
that Chef uses to bring a node into a specific state. This just means that Chef
uses cookbooks to perform work and make sure things are as they should be on the
node
What is recipe
-A recipe is the main workhorse of the cookbook. A cookbook can contain more
than one recipe, or depend on outside recipes. Recipes are used to declare
the state of different resources.
What is resource
- a resource can be anything of a ststem in a desied state
package: Used to manage packages on a node
service: Used to manage services on a node
user: Manage users on the node
group: Manage groups
template: Manage files with embedded ruby templates
cookbook_file: Transfer files from the files subdirectory in the
cookbook to a location on the node
file: Manage contents of a file on node
directory: Manage directories on node
execute: Execute a command on the node
cron: Edit an existing cron file on the node
What is Attribute
Attributes in Chef are basically settings. Think of them as simple key-value
pairs for anything you might want to use in your cookbook.
Whay is Template
Templates are similar to files, but they are not static. Template files end
with the .erb extension, meaning that they contain embedded Ruby
What is Metadata.rb
The metadata.rb file is used, not surprisingly, to manage the metadata about
a package. This includes things like the name of the package, a description, etc.
It also includes things like dependency information, where you can specify
which cookbooks this cookbook needs to operate. This will allow the Chef server to
build the run-list for the nodes correctly and ensure that all of the pieces are
transfered correctly
databag -->
hio/ohi -->
encryption of keys in ubuntu
apt-get purge ( uninstall the java)
default.rb
------------
# Cookbook Name:: leadapp
# Recipe:: default
#
# Copyright (c) 2015 The Authors, All Rights Reserved.
execute "download_db_app_artifacts" do
command "aws s3 cp s3://scmrepo/schema.sql /root/"
end
execute "download_db_app_artifacts" do
command "aws s3 cp s3://scmrepo/schema.sql /root/leadapp.war"
end
template '/root/dbscript.sql' do
source 'dbscrpt.sql.erb'
end
#execute "dbscript_exec" do
# command "mysql -u root < /root/dbscript.sql"
#end
execute "dbscript_exec" do
command "mysql -u leadapp -pleadapp < /root/schema.sql"
end
execute "tomcat_stop" do
command "/etc/init.d/tomcat6 stop"
end
#execute "testing_cmd_execution" do
# command "wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war -P
/usr/share/tomcat6/webapps/"
#end
execute "app_deploy" do
command "aws s3 cp s3://scmrepo/leadapp.war /usr/share/tomcat6/webapps/"
end
execute "tomcat_start" do
command "/etc/init.d/tomcat6 start"
end
-----------
AWS access keys
Access Key ID:AKIAJC6YIYMT35IZBSYA
Secret Access Key: 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
to downlaod a file
aws s3 cp s3://scmrepo/schema.sql .