{"id":392,"date":"2011-10-07T22:22:43","date_gmt":"2011-10-07T22:22:43","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=392"},"modified":"2011-10-07T22:35:26","modified_gmt":"2011-10-07T22:35:26","slug":"simple-chef-example","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/10\/simple-chef-example\/","title":{"rendered":"Simple Chef Example"},"content":{"rendered":"<p>Chef is a tool for the automated deployment of servers, workstations and services. The idea is pretty neat in that once you&#8217;ve built your scripts you can tell Chef &#8220;I want a new Redhat server running apache, it needs to have Ruby etc. and my web app installed&#8221; and Chef will go away and figure everything out and do it.<\/p>\n<p>I&#8217;m just learning to use Chef and I wanted to put together a simple example. Getting Chef installed on Ubuntu 10.04 was harder than I expected! The install process here has a LOT of redundant steps. At some point I&#8217;ll try this again on a clean server and correct this.<\/p>\n<p>So here&#8217;s the process to run an example simple Chef &#8220;recipe&#8221;. It uses chef-solo, which is a serverless version of Chef.<\/p>\n<p>1. Install Chef. On Ubuntu I did the following. I had some issues during install, and some of the following packages may not be required, but this is exactly what I did:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install chef ruby-dev build-essential ruby-gems1.9.1 libxml2-dev libxslt1-dev ruby1.9.1-dev\r\nsudo apt-get install libopenssl-ruby1.9.1\r\nsudo apt-get install ruby-gems\r\nsudo apt-get install libmixlib-log-ruby\r\nsudo gem install net-shh net-ssh-multi fog highline # THIS PROBABLY ISN'T REQUIRED\r\nsudo gem install chef                               # THIS PROBABLY ISN'T REQUIRED\r\nsudo gem install knife-ec2\r\nsudo gem install mixlib-authentication\r\nsudo apt-get install libhighline-ruby1.9.1\r\nsudo apt-get install ruby-full\r\nsudo gem install json\r\nsudo gem install highline\r\n<\/pre>\n<p>2. Create a chef-solo config file and place it in ~\/solo.rb  (note, you must use the full path to your home directory here):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nfile_cache_path &quot;\/home\/YOURUSERNAME\/chef-solo&quot;\r\ncookbook_path &quot;\/home\/YOURUSERNAME\/chef-solo\/cookbooks&quot;\r\n<\/pre>\n<p>This just tells Chef where to store cached files and where your &#8220;cookbook&#8221; lives.<\/p>\n<p>You need to create these directories for Chef:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmkdir chef-solo\r\nmkdir chef-solo\/cookbooks\r\n<\/pre>\n<p>3. Use knife to create a new &#8220;cookbook&#8221; a &#8220;cookbook&#8221; is where your &#8220;recipes&#8221; (install scripts) go. For some reason, knife isn&#8217;t anywhere on the path&#8230;<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/var\/lib\/gems\/1.9.1\/gems\/chef-0.10.4\/bin\/knife cookbook create MYCOOKBOOK -c ~\/solo.rb\r\n<\/pre>\n<p>4. Create you recipe file, create a new file called:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n~\/chef-solo\/cookbooks\/MYCOOKBOOK\/recipes\/myrecipe.rb\r\n<\/pre>\n<p>With the following contents, edit YOURUSERNAME and YOURUSERGROUP accordingly:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nif platform?(&quot;redhat&quot;, &quot;centos&quot;, &quot;fedora&quot;)\r\n  # code for only redhat based systems.\r\n  file &quot;\/tmp\/rpm_based&quot; do\r\n    owner &quot;YOURUSERNAME&quot;\r\n    group &quot;YOURUSERGROUP&quot;\r\n    mode &quot;0755&quot;\r\n    action :create\r\n  end\r\nend\r\n\r\nif platform?(&quot;ubuntu&quot;, &quot;debian&quot;)\r\n  # code for debian based systems\r\n  file &quot;\/tmp\/deb_based&quot; do\r\n    owner &quot;YOURUSERNAME&quot;\r\n    group &quot;YOURUSERGROUP&quot;\r\n    mode &quot;0755&quot;\r\n    action :create\r\n  end\r\nend\r\n<\/pre>\n<p>5. Create a JSON run specification. Create a new file called ~\/this.json with the following contents:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;run_list&quot;: &#x5B;\r\n    &quot;recipe&#x5B;MYCOOKBOOK::myrecipe]&quot;\r\n  ]\r\n}\r\n<\/pre>\n<p>6. Run the recipe!<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nchef-solo -c ~\/solo.rb MYCOOKBOOK -j .\/this.json\r\n<\/pre>\n<p>This very simple example recipe just checks if the server is running a debian based distribution or a redhat one and writes a file to \/tmp accordingly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chef is a tool for the automated deployment of servers, workstations and services. The idea is pretty neat in that once you&#8217;ve built your scripts you can tell Chef &#8220;I want a new Redhat server running apache, it needs to have Ruby etc. and my web app installed&#8221; and Chef will go away and figure [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-392","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-6k","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=392"}],"version-history":[{"count":8,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/392\/revisions"}],"predecessor-version":[{"id":399,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/392\/revisions\/399"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}