{"id":38514,"date":"2016-07-31T13:47:41","date_gmt":"2016-07-31T08:17:41","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=38514"},"modified":"2016-08-01T17:25:48","modified_gmt":"2016-08-01T11:55:48","slug":"empower-vagrant-with-chef","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/empower-vagrant-with-chef\/","title":{"rendered":"Empower Vagrant with Chef"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-38777\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/Screen-Shot-2016-07-30-at-8.23.22-PM.png\" alt=\"Screen Shot 2016-07-30 at 8.23.22 PM\" width=\"954\" height=\"294\" \/><\/p>\n<p>Vagrant is a development friendly tool to make easy creation of development environments. In its own words it &#8220;Creates and configures lightweight, reproducible, and portable development environments&#8221;. Today we are going to learn how we can <a title=\"Chef DevOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">use chef-zero to provision guest OS<\/a> for the development environment. We are not going to explore basic of Chef and Vagrant in this blog as I am assuming you already have beginner level competency in both.<\/p>\n<p>Before you go ahead make sure you have these packages installed on your system:<br \/>\n1. Virtual Box<br \/>\n2. Vagrant<\/p>\n<p>There is no such rocket science to install these packages, and If these do not exist on your machine, please follow the official <a href=\"https:\/\/www.vagrantup.com\/docs\/installation\/\">guideline<\/a>.<\/p>\n<h1><strong>Resolve plugin dependency<\/strong><\/h1>\n<p>The vagrant use provisioner to perform tasks on guest machine and these provisioners are available as a plugin. Let&#8217;s install all required plugin and understand why we need them.<\/p>\n<p>Vagrant-Omnibus plugin is\u00a0required for Vagrant to install chef-client on guest:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">vagrant plugin install vagrant-omnibus\r\n<\/pre>\n<p>We are using vagrant-chef-zero plugin to provision local chef recipes on guest:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">vagrant plugin install vagrant-chef-zero\r\n<\/pre>\n<p>The vagrant-berkshelf plugin download and install cookbooks and dependencies on the guest machine. But make sure Berksfile need to be present in the same working directory:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">vagrant plugin install vagrant-berkshelf\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-38770\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/Screen-Shot-2016-07-30-at-8.20.12-PM.png\" alt=\"Screen Shot 2016-07-30 at 8.20.12 PM\" width=\"1104\" height=\"408\" \/><\/p>\n<h1><strong>Create Vagrantfile<\/strong><\/h1>\n<p>To quick start with vagrant execute \u201cvagrant up\u201c in your cookbook directory. It will create a Vagrantfile in your working directory with default configuration:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-38781\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/Screen-Shot-2016-07-30-at-11.20.04-AM.png\" alt=\"Screen Shot 2016-07-30 at 11.20.04 AM\" width=\"1406\" height=\"266\" \/><\/p>\n<p>I have modified default Vagrantfile with the parameters of my choice. Here I have taken centos7 as the guest OS layer and defined some other values to hostname, IPaddress, port forwarding and compute units. You are free to modify these parameters:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">### Virtual box Guest OS configuration ###### \r\n  Vagrant.configure(2) do |config|\r\n  config.vm.box = \"bento\/centos-7.1\"\r\n  config.vm.hostname = \"local-host\u201d\r\n  config.vm.network \"forwarded_port\", guest: 80, host: 80\r\n  config.vm.network \"private_network\", ip: \u201c192.168.1.100\u201d\r\n\r\n  config.vm.provider \"virtualbox\" do |v|\r\n    v.name = \"local-host\"\r\n    v.memory = 2048\r\n    v.cpus = 1\r\n  end\r\n<\/pre>\n<h1><strong>Configure Chef provisioner<\/strong><\/h1>\n<p>Let&#8217;s take an example to assign Nginx recipe to automate the installation of package and configuration. In below configuration we instruct Vagrant to perform:<br \/>\na. Bootstrap guest machine with node name &#8220;local-host&#8221;<br \/>\nb. Install chef-client in it.<br \/>\nc. Search for cookbook named Nginx in current working directory<br \/>\nd. Assign recipe to the node and execute a set of commands.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">#### Chef Configuration #####################\r\n  config.vm.provision \"chef_zero\" do |chef|\r\n    chef.node_name = \"local-host\u201d\r\n    chef.environments_path = \"..\/..\/environments\"\r\n    chef.environment = \u201clocal\u201d\r\n    chef.nodes_path = \"nodes\"\r\n    chef.roles_path = \"roles\"\r\n    chef.run_list = [ 'recipe[nginx::default]']\r\n  end<\/pre>\n<p>You can download full Vagrantfile from <a href=\"https:\/\/bitbucket.org\/rajdeeps\/blog\/src\/ab4dd4531a0986ea4aa288b5ccb8360d8e848b91\/Vagrantfile?at=master\">here<\/a>.<\/p>\n<h1><strong>Now it&#8217;s time to fire it<\/strong><\/h1>\n<p>It may require you to initialize a chef tool called berkshelf in the same directory; then you should be ready to start the vagrant provisioning:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">berks install\r\n<\/pre>\n<p>It will create a Berksfile, and this will be used by Vagrant provisioner. Look&#8217;s like we are in good position to test it out. To bring the machine up execute:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">vagrant up<\/pre>\n<p>Keep your eye on virtual box it will download centos7 box and create a virtual machine with defined configuration. Once the machine is up Vagrant will start provision on it. I have been using this to build development environment on a\u00a0fresh machine, but it can be\u00a0used to test your recipes on a\u00a0virtual machine. The combination of Chef and Vagrant is fruitful and with fewer efforts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vagrant is a development friendly tool to make easy creation of development environments. In its own words it &#8220;Creates and configures lightweight, reproducible, and portable development environments&#8221;. Today we are going to learn how we can use chef-zero to provision guest OS for the development environment. We are not going to explore basic of Chef [&hellip;]<\/p>\n","protected":false},"author":165,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[2348,1],"tags":[1853,248,1910,3870,3867,1892,3651,3868,1504,3869],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38514"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/165"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=38514"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38514\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=38514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=38514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=38514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}