{"id":6531,"date":"2017-03-09T17:00:56","date_gmt":"2017-03-09T09:00:56","guid":{"rendered":"http:\/\/rmohan.com\/?p=6531"},"modified":"2017-03-09T17:00:56","modified_gmt":"2017-03-09T09:00:56","slug":"chef-beginner","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6531","title":{"rendered":"chef beginner"},"content":{"rendered":"<p>INSPEC.IO<br \/>\nuser &#8216;test&#8217; do<br \/>\ncomment &#8216;test user&#8217;<br \/>\nuid &#8217;89&#8217;<br \/>\ngid &#8217;89&#8217;<br \/>\nhome &#8216;\/home\/random&#8217;<br \/>\nshell &#8216;\/bin\/bash&#8217;<br \/>\naction :create<br \/>\npassword &#8216;$1$JJsvHslasdfjVEroftprNn4JHtDi&#8217;<br \/>\nend<br \/>\nuser &#8216;test&#8217; do<br \/>\ncomment &#8216;test user&#8217;<br \/>\nuid &#8217;89&#8217;<br \/>\ngid &#8217;89&#8217;<br \/>\nhome &#8216;\/home\/test&#8217;<br \/>\nshell &#8216;\/bin\/bash&#8217;<br \/>\naction :create<br \/>\npassword &#8216;$1$\/IoJI4pW$rVC197lCpPyDdkD7RxiRG\/&#8217;<br \/>\nend<br \/>\nuser &#8216;test&#8217; do<br \/>\ncomment &#8216;test user&#8217;<br \/>\nuid &#8217;89&#8217;<br \/>\ngid &#8217;89&#8217;<br \/>\nhome &#8216;\/home\/test&#8217;<br \/>\nshell &#8216;\/bin\/bash&#8217;<br \/>\naction :modify<br \/>\npassword &#8216;$1$\/IoJI4pW$rVC197lCpPyDdkD7RxiRG\/&#8217;<br \/>\nend<\/p>\n<p>package &#8216;pqr&#8217; do<br \/>\naction :remove<br \/>\nend<\/p>\n<p>package &#8216;tree&#8217; do<br \/>\naction :install<br \/>\nend<\/p>\n<p>for p in [ &#8220;elinks&#8221;, &#8220;wget&#8221;, &#8220;lynx&#8221;, &#8220;vim&#8221;, &#8220;ant&#8221; ] do<br \/>\npackage p do<br \/>\naction [:install]<br \/>\nend<br \/>\nend<\/p>\n<p>package git<br \/>\ngroup &#8220;test1&#8221; do<br \/>\ngid 3000<br \/>\nend<br \/>\nuser &#8220;test1&#8221; do<br \/>\nuid &#8220;3000&#8221;<br \/>\nshell &#8220;\/sbin\/nologin&#8221;<br \/>\nhome &#8220;\/home\/test&#8221;<br \/>\ngid &#8220;3000&#8221;<br \/>\npassword &#8216;$1$\/IoJI4pW$rVC197lCpPyDdkD7RxiRG\/&#8217;<br \/>\nend<\/p>\n<p>file &#8216;\/etc\/motd&#8217; do<br \/>\ncontent &#8216;<\/p>\n<p>===================================<br \/>\nThis server is a property of visa<\/p>\n<p>===================================<br \/>\n&#8216;<br \/>\nmode &#8220;0644&#8221;<br \/>\nend<br \/>\nroot@ws:\/var\/chef\/cookbooks\/useradd\/recipes# \/opt\/chefdk\/embedded\/bin\/ruby -c default.rb<br \/>\nSyntax OK<br \/>\nroot@ws:\/var\/chef\/cookbooks\/useradd\/recipes#<\/p>\n<p>run on local mode<\/p>\n<p>chef-client -z | &#8211;local-mode<\/p>\n<p>chef-client &#8211;local-mode &#8211;why-run<\/p>\n<p>chef-client &#8211;local-mode<\/p>\n<p>chef-client &#8211;local -o recipe<br \/>\nchef-client -z -l info test.rb<\/p>\n<p>I understand Chef conditional execution.<\/p>\n<p>I&#8217;d like to do some conditional execution based on whether or not a database exists in Postgresql<\/p>\n<p>So here&#8217;s my example<\/p>\n<p>execute &#8220;add_db&#8221; do<br \/>\ncwd &#8220;\/tmp&#8221;<br \/>\nuser &#8220;dbuser&#8221;<br \/>\ncommand &#8220;createdb -T template_postgis mydb&#8221;<br \/>\nnot_if &#8216;psql &#8211;list|grep mydb&#8217;<br \/>\nend<br \/>\nexecute &#8220;touch \/home\/#{user}\/monkeypants&#8221; do<br \/>\nuser &#8220;monkey&#8221;<br \/>\ngroup &#8220;monkey&#8221;<br \/>\ncwd &#8220;\/home\/monkey&#8221;<br \/>\nnot_if &#8220;check_command&#8221;, :cwd =&gt; &#8220;\/home\/monkey&#8221; , :user =&gt; &#8220;monkey&#8221;, :group =&gt; &#8220;monkey&#8221;<br \/>\nend<br \/>\nlog &#8220;Welcome to Chef, #{node[&#8220;starter_name&#8221;]}!&#8221; do<br \/>\nlevel :info<br \/>\nend<\/p>\n<p>file &#8216;\/etc\/my_first_file&#8217; do<br \/>\ncontent &#8216;This is my first file creation using chef server&#8217;<br \/>\nend<\/p>\n<p>file &#8216;\/etc\/my_second_file&#8217; do<br \/>\ncontent &#8216;My Second file&#8217;<br \/>\nend<br \/>\nignore_failure true<\/p>\n<p>Full control<\/p>\n<p>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.<\/p>\n<p>service &#8220;apache&#8221; do<br \/>\naction :enable<br \/>\nignore_failure true<br \/>\nend<\/p>\n<p>how to create a cookbook<br \/>\nchef<br \/>\nmkdir cookbooks<\/p>\n<p>Usage: chef generate GENERATOR [options]<\/p>\n<p>Available generators:<br \/>\napp Generate an application repo<br \/>\ncookbook Generate a single cookbook<br \/>\nrecipe Generate a new recipe<br \/>\nattribute Generate an attributes file<br \/>\ntemplate Generate a file template<br \/>\nfile Generate a cookbook file<br \/>\nlwrp Generate a lightweight resource\/provider<br \/>\nrepo Generate a Chef code repository<br \/>\npolicyfile Generate a Policyfile for use with the install\/push commands<br \/>\ngenerator Copy ChefDK&#8217;s generator cookbook so you can customize it<br \/>\nbuild-cookbook Generate a build cookbook for use with Delivery<br \/>\nknife cookbook create nginx<br \/>\n** Creating cookbook nginx<br \/>\n** Creating README for cookbook: nginx<br \/>\n** Creating CHANGELOG for cookbook: nginx<br \/>\n** Creating metadata for cookbook: nginx<br \/>\nchef generate cookbook cookbooks\/nginx<\/p>\n<p>root@ws:~# chef generate cookbook cookbooks\/nginx<br \/>\nGenerating cookbook nginx<br \/>\n&#8211; Ensuring correct cookbook file content<br \/>\n&#8211; Committing cookbook files to git<br \/>\n&#8211; Ensuring delivery configuration<br \/>\n&#8211; Ensuring correct delivery build cookbook content<br \/>\n&#8211; Adding delivery configuration to feature branch<br \/>\n&#8211; Adding build cookbook to feature branch<br \/>\n&#8211; Merging delivery content feature branch to master<\/p>\n<p>Your cookbook is ready. Type `cd cookbooks\/nginx` to enter it.<\/p>\n<p>There are several commands you can run to get started locally developing and testing your cookbook.<br \/>\nType `delivery local &#8211;help` to see a full list.<\/p>\n<p>Why not start by writing a test? Tests for the default recipe are stored at:<\/p>\n<p>test\/recipes\/default_test.rb<\/p>\n<p>If you&#8217;d prefer to dive right in, the default recipe can be found at:<\/p>\n<p>recipes\/default.rb<\/p>\n<p>version cotrol<\/p>\n<p>Berksfile README.md chefignore metadata.rb recipes spec test<br \/>\nroot@ws:~\/cookbooks\/nginx# cat metadata.rb<br \/>\nname &#8216;nginx&#8217;<br \/>\nmaintainer &#8216;The Authors&#8217;<br \/>\nmaintainer_email &#8216;you@example.com&#8217;<br \/>\nlicense &#8216;all_rights&#8217;<br \/>\ndescription &#8216;Installs\/Configures nginx&#8217;<br \/>\nlong_description &#8216;Installs\/Configures nginx&#8217;<br \/>\nversion &#8216;0.1.0&#8217;<br \/>\ncookbooks<br \/>\n|_nginx<br \/>\n|_recipes<br \/>\n|_default<br \/>\n|_test.rb<\/p>\n<p>execute &#8216;apt-get update&#8217; do<br \/>\naction :run<br \/>\nend<\/p>\n<p>package &#8216;nginx&#8217; do<br \/>\naction :install<br \/>\nend<\/p>\n<p>service &#8216;nginx&#8217; do<br \/>\naction [ :enable, :start ]<br \/>\nend<\/p>\n<p>service &#8216;nginx&#8217; do<br \/>\nsupports status: true, restart: true, reload: true<br \/>\naction :enable<br \/>\nend<br \/>\nservice &#8216;apache&#8217; do<br \/>\nsupports :restart =&gt; true, :reload =&gt; true<br \/>\naction :enable<br \/>\nend<br \/>\nservice &#8220;nginx&#8221; do<br \/>\nsupports :restart =&gt; true, :start =&gt; true, :stop =&gt; true, :reload =&gt; true<br \/>\naction :nothing<br \/>\nend<\/p>\n<p>template &#8220;nginx&#8221; do<br \/>\npath &#8220;\/etc\/init.d\/nginx&#8221;<br \/>\nsource &#8220;nginx.erb&#8221;<br \/>\nowner &#8220;root&#8221;<br \/>\ngroup &#8220;root&#8221;<br \/>\nmode &#8220;0755&#8221;<br \/>\nnotifies :enable, &#8220;service[nginx]&#8221;<br \/>\nnotifies :start, &#8220;service[nginx]&#8221;<br \/>\nend<\/p>\n<p>cookbook_file &#8220;\/usr\/share\/nginx\/html\/index.html&#8221; do<br \/>\nsource &#8220;index.html&#8221;<br \/>\nmode &#8220;0644&#8221;<br \/>\nend<br \/>\ncd ~\/chef-repo\/cookbooks\/nginx\/files\/default<\/p>\n<p>nano index.html<\/p>\n<p>&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;title&gt;Hello there&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;h1&gt;This is a Mohan test&lt;\/h1&gt;<br \/>\n&lt;p&gt;Please Mohan work!&lt;\/p&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\nexcute &#8216;service nginx stop &#8216; do<br \/>\nnot_if &#8216;service nginx status&#8217;<br \/>\nend<\/p>\n<p>chef-client -z &#8211;runlist &#8220;recipe[nginx::install],recipe[nginx::service]&#8221;<\/p>\n<p>chef-client -z &#8211;runlist &#8220;recipe[nginx::install]&#8221;<br \/>\ninstall.rb<\/p>\n<p>execute &#8216;apt-get update&#8217; do<br \/>\nend<\/p>\n<p>package &#8216;nginx&#8217; do<br \/>\naction :install<br \/>\nend<br \/>\nservice.rb<\/p>\n<p>service &#8216;nginx&#8217; do<br \/>\naction :start<br \/>\nend<\/p>\n<p>execute &#8216;my own service start description&#8217; do<br \/>\ncommand &#8216;service nginx start&#8217;<br \/>\nnot_if &#8216;service nginx status&#8217;<br \/>\nend<br \/>\nconfig.rb<\/p>\n<p>cookbook_file &#8216;\/etc\/nginx\/nginx.conf&#8217; do<br \/>\nsource &#8216;nginx.conf&#8217;<br \/>\nmode &#8216;0644&#8217;<br \/>\naction :create<br \/>\nnotifies :restart, &#8216;service[nginx]&#8217;<br \/>\nend<br \/>\ncookbook_file &#8216;\/usr\/share\/nginx\/html\/index.html&#8217; do<br \/>\nsource &#8216;index.html&#8217;<br \/>\nmode &#8216;0644&#8217;<br \/>\naction :create<br \/>\nend<\/p>\n","protected":false},"excerpt":{"rendered":"<p>INSPEC.IO user &#8216;test&#8217; do comment &#8216;test user&#8217; uid &#8217;89&#8217; gid &#8217;89&#8217; home &#8216;\/home\/random&#8217; shell &#8216;\/bin\/bash&#8217; action :create password &#8216;$1$JJsvHslasdfjVEroftprNn4JHtDi&#8217; end user &#8216;test&#8217; do comment &#8216;test user&#8217; uid &#8217;89&#8217; gid &#8217;89&#8217; home &#8216;\/home\/test&#8217; shell &#8216;\/bin\/bash&#8217; action :create password &#8216;$1$\/IoJI4pW$rVC197lCpPyDdkD7RxiRG\/&#8217; end user &#8216;test&#8217; do comment &#8216;test user&#8217; uid &#8217;89&#8217; gid &#8217;89&#8217; home &#8216;\/home\/test&#8217; shell &#8216;\/bin\/bash&#8217; action :modify [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[81],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6531"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6531"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6531\/revisions"}],"predecessor-version":[{"id":6532,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6531\/revisions\/6532"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}