How do I configure Chef Solo to install Nginx on a

2019-01-17 05:39发布

问题:

I'm new to Chef and the documentation (even the home page of their website) makes my head spin. I'm not even sure if I'm using it for the correct purpose.

My intent is to setup a Vagrantfile that tells Chef Solo to install some software automatically when I spin up a new box. That is one of Chef Solo's intended uses, am I correct?

I'm not really sure if that qualifies as one of "the hardest infrastructure challenges on the planet", but whatever.

My first goal is to get Chef Solo to install nginx for me. In my project, I've cloned the cookbook for nginx:

$ git clone https://github.com/opscode-cookbooks/nginx.git cookbooks/nginx

I edited my Vagrantfile to look like this:

Vagrant.configure("2") do |config|
  config.vm.box = "opscode-ubuntu-1204"
  config.vm.provision :chef_solo do |chef|
    chef.add_recipe "nginx"
  end
end

When I ran vagrant up, I got some errors that some cookbooks weren't found (build-essential, apt, etc), so I cloned them from their appropriate repos. Now, when I vagrant up, I'm getting this output:

[2013-10-01T20:31:03+00:00] INFO: Processing package[nginx] action install (nginx::package line 38)    

Error executing action `install` on resource 'package[nginx]'
Chef::Exceptions::Exec
----------------------

apt-get -q -y install nginx=1.1.19-1ubuntu0.1 returned 100, expected 0

Resource Declaration:
---------------------
# In /tmp/vagrant-chef-1/chef-solo-1/cookbooks/nginx/recipes/package.rb

 38: package node['nginx']['package_name'] do
 39:   notifies :reload, 'ohai[reload_nginx]', :immediately
 40: end
 41: 

Compiled Resource:
------------------
# Declared in /tmp/vagrant-chef-1/chef-solo-1/cookbooks/nginx/recipes/package.rb:38:in `from_file'

package("nginx") do
  action :install
  retries 0
  retry_delay 2
  package_name "nginx"
  version "1.1.19-1ubuntu0.1"
  cookbook_name :nginx
  recipe_name "package"
end

[2013-10-01T20:31:08+00:00] INFO: Running queued delayed notifications before re-raising exception
[2013-10-01T20:31:08+00:00] ERROR: Running exception handlers
[2013-10-01T20:31:08+00:00] ERROR: Exception handlers complete
[2013-10-01T20:31:08+00:00] FATAL: Stacktrace dumped to /tmp/vagrant-chef-1/chef-stacktrace.out
[2013-10-01T20:31:08+00:00] FATAL: Chef::Exceptions::Exec: package[nginx] (nginx::package line 38) had an error: Chef::Exceptions::Exec: apt-get -q -y install nginx=1.1.19-1ubuntu0.1 returned 100, expected 0

Chef never successfully completed! Any errors should be visible in the output above. Please fix your recipes so that they properly complete.

How can I fix my recipes so that they properly complete?

回答1:

To more effectively use chef I'd advise installing the following vagrant plugins:

vagrant plugin install vagrant-omnibus
vagrant plugin install vagrant-berkshelf

Berkshelf is a tool for managing cookbook dependencies. The omnibus plugin is useful to ensure you're using the latest revision of chef.

The following example demonstrates how easy it becomes to install something like nginx.

Example

├── Berksfile
└── Vagrantfile

Berkshelf

Lists the required cookbooks. Berkshelf will download them (and dependencies) from the opscode community website.

site :opscode

cookbook "nginx"

Vagrantfile

The following vagrant file will install nginx, making it available on port 8080 of the host machine:

Vagrant.configure("2") do |config|

  # Box details
  config.vm.box = "Berkshelf-CentOS-6.3-x86_64-minimal"
  config.vm.box_url = "https://dl.dropbox.com/u/31081437/Berkshelf-CentOS-6.3-x86_64-minimal.box"

  # Plugins
  config.berkshelf.enabled = true
  config.omnibus.chef_version = :latest

  # Network
  config.vm.network :forwarded_port, guest: 80, host: 8080

  # Chef solo provisioning
  config.vm.provision :chef_solo do |chef|
    chef.add_recipe "nginx"
  end

end

Notes:

  • This example uses CentOS. Should work equally well on Ubuntu.


回答2:

Hi I got into same problem and while searching stumbled upon your issue. I have solved it wi simple including APT recipe. Reason why you need it because you have to to apt-get update on the new instance before you can install packages. thats all.