没有名为`template' chef的资源或方法

时间:2014-02-08 23:02:14

标签: chef chef-solo

实际上尝试用厨师独奏写wirst cookbook,我很期待cookbook wordpress来初始化我自己的网站。

但是几个小时后我找不到这个bug的解决方案:

No resource or method named `template' for `Chef::Recipe "create_configs"'

片段:

template "#{project_config_dir}/database.php" do
  source "database.php.erb"
  mode 0440
  owner "root"
  group node['apache']['group']
  variables(
    'dev' => {
      'type' => app_db_config['type'],
      'hostname' => app_db_config['hostname'],
      'port' => app_db_config['port'],
      'username' => app_db_config['user'],
      'password' => app_db_config['password'],
      'database' => app_db_config['dababase'],
    } 
  )
  action :create
end

使用Vagrant文​​件:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  config.vm.network :private_network, ip: "192.168.33.11", :bridge => 'en1: Wi-Fi (AirPort)'

  config.vm.synced_folder "/Users/Tom/Documents/vagrant/destination_server/public", "/home/vagrant/public"

  config.vm.provider :virtualbox do |vb|
    # Don't boot with headless mode
     vb.gui = false

   # Use VBoxManage to customize the VM. For example to change memory:
   vb.customize ["modifyvm", :id, "--memory", "1024"]
 end

  config.vm.provision :shell, :inline => "/etc/init.d/networking restart"

  config.vm.provision :shell, :inline => "sudo apt-get update -y"
  config.vm.provision :shell, :inline => "sudo apt-get install ruby1.9.3 -y"
  config.vm.provision :shell, :inline => "sudo apt-get install rubygems -y"

  config.vm.provision :shell, :inline => "cd /home/vagrant && sudo ./postinstall.sh"

  config.vm.provision "chef_solo" do |chef|
    chef.add_recipe "apt"
    chef.add_recipe "bazaar"

    chef.json = {
      "mysql" => {
        "server_debian_password" => "root",
        "server_root_password" => "root",
        "server_repl_password" => "root"
      },
      "run_list" =>["recipe[mysql::server]"]  
    }

    chef.add_recipe "site"


  end
end

有趣的想法是资源"目录"工作正常,但与使用相同的问题"执行"。

使用了cookbook wordpress并添加了所有需要的烹饪书,并且使用这两种资源进行了精心设计。

我有11.10版的厨师。 使用Vagrant创建VirtualMachine

有什么想法吗?需要帮助/线索该怎么做:/

4 个答案:

答案 0 :(得分:3)

虽然Mark的回答帮助您解决了问题,但是遇到Chef发送错误消息的原始问题 -

No resource or method named `template' for `Chef::Recipe "create_configs"'

实际上是由包含引发template的内容的NoMethodError资源引起的。

所以:

template "#{project_config_dir}/database.php" do
  # some code here
  something_(like_a_node_attribute)_that_raises_no_method_error
end

如果我不得不猜测,我会假设您没有在运行列表中包含apache食谱,因为您很可能已将所有app_db_config属性设置在某处

 group node['apache']['group']
  variables(
    'dev' => {
      'type' => app_db_config['type'],
      'hostname' => app_db_config['hostname'],
      'port' => app_db_config['port'],
      'username' => app_db_config['user'],
      'password' => app_db_config['password'],
      'database' => app_db_config['dababase'],
    } 
  )

答案 1 :(得分:1)

目前还不清楚错误是什么,但是你的流浪文件似乎不必要地复杂化了。

我建议您使用omnibus插件管理chef和Berkshelf插件的安装来管理cookbook。举个例子,我建议:

建议编辑

Berksfile

Berkshelf需要此文件并列出您的cookbook依赖项。这些将自动从社区站点下载。在这里,我假设自定义“站点”食谱位于 cookbooks 子目录下:

site :opscode

cookbook "apt"
cookbook "bazaar"
cookbook "mysql"
cookbook "site", :path "cookbooks/site"

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Box   
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

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

  # Network
  config.vm.network :private_network, ip: "192.168.33.11", :bridge => 'en1: Wi-Fi (AirPort)'

  # Storage
  config.vm.synced_folder "/Users/Tom/Documents/vagrant/destination_server/public", "/home/vagrant/public"

  # Virtualbox
  config.vm.provider :virtualbox do |vb|
    # Don't boot with headless mode
    vb.gui = false

    # Use VBoxManage to customize the VM. For example to change memory:
    vb.customize ["modifyvm", :id, "--memory", "1024"]
  end

  # Chef solo provisioning
  config.vm.provision "chef_solo" do |chef|
    chef.add_recipe "apt"
    chef.add_recipe "bazaar"
    chef.add_recipe "mysql::server"
    chef.add_recipe "site"

    chef.json = {
      "mysql" => {
        "server_debian_password" => "root",
        "server_root_password" => "root",
        "server_repl_password" => "root"
      }
    }
  end    
end

备注:

  • shell配置程序由一个omnibus变量替换,指示应安装哪个版本的chef
  • add_recipe 方法将为您构建运行列表。

答案 2 :(得分:1)

我有同样的问题。我用这个article作为教程。毕竟我想创建自己的食谱。我将其命名为 ruby​​ 并保存在 site_cookbooks 中。我用简单的代码添加了默认配方:

execute "install_ruby_through_rvm" do
   command "rvm install #{default['ruby']['install_version']}"
   action :run
end

我尝试过Mark O'Connor提出的解决方案,但每次只得到一个结果:

Relevant File Content:
----------------------
/tmp/vagrant-chef/chef-solo-1/cookbooks/ruby2/recipes/default.rb:

3:  # Recipe:: default
4:  #
5:  # Copyright 2014, YOUR_COMPANY_NAME
6:  #
7:  # All rights reserved - Do Not Redistribute
8:  #
9:  
10>> execute "install_ruby_through_rvm" do
11:    command "rvm install #{default['ruby']['install_version']}"
12:    action :run
13:  end 14:  


[2014-02-10T21:50:01+00:00] ERROR: Running exception handlers
[2014-02-10T21:50:01+00:00] ERROR: Exception handlers complete
[2014-02-10T21:50:01+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2014-02-10T21:50:01+00:00] ERROR: No resource or method named `execute' for `Chef::Recipe "default"'

我不明白什么是错的?

<强>已更新

我只是个傻瓜:-)问题出在属性哈希中!正确的食谱:

execute "install_ruby_through_rvm" do
   command "rvm install #{node['ruby']['install_version']}"
   action :run
end

答案 3 :(得分:0)

你肯定会得到那个错误,例如你写“mod”而不是“mode”就像发生在我身上一样