Powered By Blogger

Friday, August 12, 2016

Chef Installation Step by Step Guide 
download chef server RPM

wget https://packages.chef.io/stable/el/7/chef-server-core-12.8.0-1.el7.x86_64.rpm
wget https://packages.chef.io/stable/el/7/chef-12.12.15-1.el7.x86_64.rpm
Extract and Install using RPM

rpm -Uvh chef-11.16.2-1.el6.x86_64.rpm
rpm -Uvh chef-12.12.15-1.el7.x86_64.rpm
Edit your hostname in


vi /etc/hosts
<IP ADDRESS> <HOSTNAME>
Configure Chef

chef-ctl-reconfigure

The above will configure chef server , generate base cookbooks, create SSL, give chef-server name wrt /etc/host entries

Now create admin and create pem
chef-server-ctl user-create admin <name> <lastname> <email> <password> -f admin.pem
chef-server-ctl org-create  <create-org> <'orgname'> --association_user admin -f <orgname>.pem
 Install git
yum install git -y
Clone base repo from git
git clone https://github.com/chef/chef-repo.git
download and install chef developer kit


wget https://packages.chef.io/stable/el/7/chefdk-0.15.16-1.el7.x86_64.rpm
rpm -Uvh chefdk-0.15.16-1.el7.x86_64.rpm
Verify Chef server
chef verify
add bin to your profile
echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profile
source ~/.bash_profile
Copy .pem files to chef repo
cp admin.pem <org>.pem  ~/chef-repo/.chef/
cp admin.pem <org>.pem  /root/.chef/
Configure your knife

[root@Chef-Server .chef]# vi knife.rb
current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "admin"
client_key               "#{current_dir}/admin.pem"
validation_client_name   "<ORG VALIDATOR>"
validation_key           "#{current_dir}/<orgkey>.pem"
chef_server_url          "https://<Chef-server>/organizations/<organisatio>"
syntax_check_cache_path  "#{ENV['HOME']}/.chef/syntaxcache"
cookbook_path            ["#{current_dir}/../cookbooks"]
wq!
cd ~/chef-repo
knife ssl fetch
knife client list
knife node list 
This will list the nodes registered with chef server 

Bootstrap Chef client
knife bootstrap NODENAME
It will run successful as no recipe / run list is added to host. 


export EDITOR=vi
knife  node edit <NODENAME>
Now add the run list to newly added client  and then run chef-client :) 


"recipe[name_of_recipe]"
##Run chef receipe from remote 
You could use knife ssh to run chef-client on all boxes that contain a certain role or recipe:

knife ssh "role:web" "sudo chef-client" -x ubuntu --sudo 

Or if you're in EC2:

knife ssh "role:web" "sudo chef-client" -x ubuntu -a ec2.public_hostname 


knife ssh name:mynode -a ipaddress  -x ubuntu -i mycredentials.pem "sudo chef-client"You could use knife ssh to run chef-client on all boxes that contain a certain role or recipe:

##Bootstrap Remote node

knife bootstrap new-host-ip -x root -P password -N node_name


#To add a role or recipe to a node
knife node run_list add node_name "recipe[cookbook::recipe]"
knife node run_list add node_name "role[role_name]"
knife node run_list add node_name "role[role_name],recipe[cookbook::recipe]"
Chef- Knife commands cheat sheet
knife commands:
knife cookbook create apache — to create cookbook
knife cookbook upload apache — to upload cookbook to chef-server
knife node run_list add NODENAME “recipe[NAME]” — to add receipe as a runlist to node
knife node run_list add NODENAME -b “recipe[NAME]” “recipe[NAMEOFUPLOADING]” — to add a recipe before partcular recipe- useful comamnd to define the preecedence of recipes in a runlist
knife -h == knife help command
knife node show NODENAME -a attribites(a-b-c-d)0- commanmd to show the attributes of the node
knife search node “os:linux” — search for node which are linux nodes
knife search node “os:linux” -a platform — search for niode which are linux nodes and shows playtform
knife search node “os:linux” -a linux.model give me the result of linux as linux.model =ubuntu as output
chef-client — command to run on node called as convergence.
knife environment list -w — to show all the enviroment
knife environment compare dev
knife environment compare dev prod — compare environments between dev and prod
knife environment compare — all -to compare all receipes in server across all environments.
knife environment delete dev -to delete env
knife environment show dev — shows environment information.
kniofe role create role_name — command to create new role
 knife role from file chef-repo/roles/rolename.rb — upload role(rolename.rb) to server
knife role list -w — list all roles in chef server
knife role delete role_name — delete the new role
knife node run_list add linuxnode “role[webserver]” — assign role(web server)to a node(linuxnode)
As similar to roles — we use base role.
create a base role — and we can use include that base role to all roles runlist. so if you need to update any roles with new runlist. All you need to change is the base role list.
 Chef- Knife commands cheat sheet
knife commands:
knife cookbook create apache — to create cookbook
knife cookbook upload apache — to upload cookbook to chef-server
knife node run_list add NODENAME “recipe[NAME]” — to add receipe as a runlist to node

knife node run_list add NODENAME -b “recipe[NAME]” “recipe[NAMEOFUPLOADING]” — to add a recipe before partcular recipe- useful comamnd to define the preecedence of recipes in a runlist
knife -h == knife help command
knife node show NODENAME -a attribites(a-b-c-d)0- commanmd to show the attributes of the node
knife search node “os:linux” — search for node which are linux nodes
knife search node “os:linux” -a platform — search for niode which are linux nodes and shows playtform
knife search node “os:linux” -a linux.model give me the result of linux as linux.model =ubuntu as output
chef-client — command to run on node called as convergence.
knife environment list -w — to show all the enviroment
knife environment compare dev
knife environment compare dev prod — compare environments between dev and prod
knife environment compare — all -to compare all receipes in server across all environments.
knife environment delete dev -to delete env
knife environment show dev — shows environment information.
kniofe role create role_name — command to create new role
 knife role from file chef-repo/roles/rolename.rb — upload role(rolename.rb) to server
knife role list -w — list all roles in chef server
knife role delete role_name — delete the new role
knife node run_list add linuxnode “role[webserver]” — assign role(web server)to a node(linuxnode)
As similar to roles — we use base role.
create a base role — and we can use include that base role to all roles runlist. so if you need to update any roles with new runlist. All you need to change is the base role list.