Last Updated: February 25, 2016
·
6.081K
· voleoo

Vagrant + Chef solo quick start

What chef will do

  • create user
  • add user to sudo group
  • install Nginx
  • add host file for Nginx

Before start you have to install ruby and gem bundler.
How to install ruby for your OS you can find in google.

Install bundler

gem install bundler

Create directory for chef project

mkdir chef-solo-quick-start
cd chef-solo-quick-start

Create 'Gemfile' for bundler

echo 'source "https://rubygems.org"' >> Gemfile
echo '' >> Gemfile
echo 'gem "knife-solo"' >> Gemfile
echo 'gem "knife-solo_data_bag"' >> Gemfile
echo 'gem "librarian-chef"' >> Gemfile

Installing gems using bundler

bundle install

Initialize chef project

knife solo init .

Adding cookbooks to Cheffile

echo "cookbook 'chef-solo-search'" >> Cheffile
echo "cookbook 'sudo'" >> Cheffile
echo "cookbook 'users'" >> Cheffile
echo "cookbook 'nginx'" >> Cheffile

Installing cookbooks

librarian-chef install

you can skip next step

export EDITOR=vim 

Create data bag for user deployer

knife solo data bag create users deployer

insert into file

{
  "id": "deployer",
  "password"  : "here encrypted password",
  "ssh_keys"  : [
    "here ssh key"
  ],
  "groups"    : ["sudo" ,"sysadmin"],
  "shell"     : "\/bin\/bash"
}

Generate encrypted password

openssl passwd -1 "passwor_here"

Create node file

touch nodes/vagrant.json

with next content

{
  "authorization": {
    "sudo": {
      "groups": ["vagrant", "deployer", "wheel", "sysadmin"],
      "users": ["vagrant", "deployer"],
      "passwordless": "false"
    }
  },
  "users": ["deployer"],
  "nginx": {
    "version": "1.7.6",
    "default_site_enabled": false,
    "source": { "modules": [ "nginx::http_gzip_static_module" ] }
  },
  "run_list": [
    "recipe[chef-solo-search]",
    "recipe[sudo]",
    "recipe[users::sysadmins]",
    "recipe[nginx::source]"
  ]
}

Create Vagrant file

touch Vagrantfile

with next content

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "precise64"
  config.omnibus.chef_version = :latest

  config.vm.network "forwarded_port", guest: 80, host: 8080

  VAGRANT_JSON = JSON.parse(Pathname(__FILE__).dirname.join('nodes', 'vagrant.json').read)

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["site-cookbooks", "cookbooks"]
    chef.roles_path = "roles"
    chef.data_bags_path = "data_bags"
    chef.provisioning_path = "/tmp/vagrant-chef"

    chef.run_list = VAGRANT_JSON.delete('run_list')
    chef.json = VAGRANT_JSON
  end
end

Then have to install vagrant with plugin "omnibus" how to install see in google.

Set up virtual server

vagrant up

Create simple cookbook which copies the host file into the server

mkdir -p site-cookbooks/qs-nginx/files/default/
mkdir -p site-cookbooks/qs-nginx/recipes/

Create file

touch site-cookbooks/qs-nginx/files/default/quick-start

With next context

server {
  listen 80;

  server_name 127.0.0.1;
  root /home/deployer/sites/quick-start;
}

Create file

touch site-cookbooks/qs-nginx/recipes/default.rb

With next context

cookbook_file "/etc/nginx/sites-enabled/quick-start" do
  owner "root"
  group "root"
  mode "0655"
  notifies :restart, resources(:service => "nginx"), :delayed
end

Add recipe to run_list nodes/vagrant.json

"recipe[qs-nginx]"

Apply new configuration

vagrant provision

If you open http://localhost:8080/ you should see 404 error ;-)

For more information see official documentation ;-)

GitHub repo with this project