INSPEC.IO
user ‘test’ do
comment ‘test user’
uid ’89’
gid ’89’
home ‘/home/random’
shell ‘/bin/bash’
action :create
password ‘$1$JJsvHslasdfjVEroftprNn4JHtDi’
end
user ‘test’ do
comment ‘test user’
uid ’89’
gid ’89’
home ‘/home/test’
shell ‘/bin/bash’
action :create
password ‘$1$/IoJI4pW$rVC197lCpPyDdkD7RxiRG/’
end
user ‘test’ do
comment ‘test user’
uid ’89’
gid ’89’
home ‘/home/test’
shell ‘/bin/bash’
action :modify
password ‘$1$/IoJI4pW$rVC197lCpPyDdkD7RxiRG/’
end
package ‘pqr’ do
action :remove
end
package ‘tree’ do
action :install
end
for p in [ “elinks”, “wget”, “lynx”, “vim”, “ant” ] do
package p do
action [:install]
end
end
package git
group “test1” do
gid 3000
end
user “test1” do
uid “3000”
shell “/sbin/nologin”
home “/home/test”
gid “3000”
password ‘$1$/IoJI4pW$rVC197lCpPyDdkD7RxiRG/’
end
file ‘/etc/motd’ do
content ‘
===================================
This server is a property of visa
===================================
‘
mode “0644”
end
root@ws:/var/chef/cookbooks/useradd/recipes# /opt/chefdk/embedded/bin/ruby -c default.rb
Syntax OK
root@ws:/var/chef/cookbooks/useradd/recipes#
run on local mode
chef-client -z | –local-mode
chef-client –local-mode –why-run
chef-client –local-mode
chef-client –local -o recipe
chef-client -z -l info test.rb
I understand Chef conditional execution.
I’d like to do some conditional execution based on whether or not a database exists in Postgresql
So here’s my example
execute “add_db” do
cwd “/tmp”
user “dbuser”
command “createdb -T template_postgis mydb”
not_if ‘psql –list|grep mydb’
end
execute “touch /home/#{user}/monkeypants” do
user “monkey”
group “monkey”
cwd “/home/monkey”
not_if “check_command”, :cwd => “/home/monkey” , :user => “monkey”, :group => “monkey”
end
log “Welcome to Chef, #{node[“starter_name”]}!” do
level :info
end
file ‘/etc/my_first_file’ do
content ‘This is my first file creation using chef server’
end
file ‘/etc/my_second_file’ do
content ‘My Second file’
end
ignore_failure true
Full control
In case this happens to a resource you control, you have the wonderful ignore_failure attribute to modify this behavior. Adding it to e.g. a service, will enable Chef to continue a run, even if this resource is failing.
service “apache” do
action :enable
ignore_failure true
end
how to create a cookbook
chef
mkdir cookbooks
Usage: chef generate GENERATOR [options]
Available generators:
app Generate an application repo
cookbook Generate a single cookbook
recipe Generate a new recipe
attribute Generate an attributes file
template Generate a file template
file Generate a cookbook file
lwrp Generate a lightweight resource/provider
repo Generate a Chef code repository
policyfile Generate a Policyfile for use with the install/push commands
generator Copy ChefDK’s generator cookbook so you can customize it
build-cookbook Generate a build cookbook for use with Delivery
knife cookbook create nginx
** Creating cookbook nginx
** Creating README for cookbook: nginx
** Creating CHANGELOG for cookbook: nginx
** Creating metadata for cookbook: nginx
chef generate cookbook cookbooks/nginx
root@ws:~# chef generate cookbook cookbooks/nginx
Generating cookbook nginx
– Ensuring correct cookbook file content
– Committing cookbook files to git
– Ensuring delivery configuration
– Ensuring correct delivery build cookbook content
– Adding delivery configuration to feature branch
– Adding build cookbook to feature branch
– Merging delivery content feature branch to master
Your cookbook is ready. Type `cd cookbooks/nginx` 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
version cotrol
Berksfile README.md chefignore metadata.rb recipes spec test
root@ws:~/cookbooks/nginx# cat metadata.rb
name ‘nginx’
maintainer ‘The Authors’
maintainer_email ‘you@example.com’
license ‘all_rights’
description ‘Installs/Configures nginx’
long_description ‘Installs/Configures nginx’
version ‘0.1.0’
cookbooks
|_nginx
|_recipes
|_default
|_test.rb
execute ‘apt-get update’ do
action :run
end
package ‘nginx’ do
action :install
end
service ‘nginx’ do
action [ :enable, :start ]
end
service ‘nginx’ do
supports status: true, restart: true, reload: true
action :enable
end
service ‘apache’ do
supports :restart => true, :reload => true
action :enable
end
service “nginx” do
supports :restart => true, :start => true, :stop => true, :reload => true
action :nothing
end
template “nginx” do
path “/etc/init.d/nginx”
source “nginx.erb”
owner “root”
group “root”
mode “0755”
notifies :enable, “service[nginx]”
notifies :start, “service[nginx]”
end
cookbook_file “/usr/share/nginx/html/index.html” do
source “index.html”
mode “0644”
end
cd ~/chef-repo/cookbooks/nginx/files/default
nano index.html
<html>
<head>
<title>Hello there</title>
</head>
<body>
<h1>This is a Mohan test</h1>
<p>Please Mohan work!</p>
</body>
</html>
excute ‘service nginx stop ‘ do
not_if ‘service nginx status’
end
chef-client -z –runlist “recipe[nginx::install],recipe[nginx::service]”
chef-client -z –runlist “recipe[nginx::install]”
install.rb
execute ‘apt-get update’ do
end
package ‘nginx’ do
action :install
end
service.rb
service ‘nginx’ do
action :start
end
execute ‘my own service start description’ do
command ‘service nginx start’
not_if ‘service nginx status’
end
config.rb
cookbook_file ‘/etc/nginx/nginx.conf’ do
source ‘nginx.conf’
mode ‘0644’
action :create
notifies :restart, ‘service[nginx]’
end
cookbook_file ‘/usr/share/nginx/html/index.html’ do
source ‘index.html’
mode ‘0644’
action :create
end
Recent Comments