如何让Chef在运行其他配方之前运行apt-get update

ash*_*her 46 apt apt-get chef-infra vagrant

现在我的Vagrantfile中有以下内容:

config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "cookbooks"
    chef.add_recipe "apt"
    chef.add_recipe "build-essential"
    chef.add_recipe "chef-redis::source"
    chef.add_recipe "openssl"
    chef.add_recipe "git"
    chef.add_recipe "postgresql::server"
    chef.add_recipe "postgresql::client"
end
Run Code Online (Sandbox Code Playgroud)

为了安装添加到我的recipe_list的软件,我需要让VM 在安装其他软件之前发布apt-get更新.

我的印象是,这是'apt'配方的一个特点 - 它会首先运行更新.

我做流浪汉时的输出是:

[Sat, 11 Feb 2012 22:20:03 -0800] INFO: *** Chef 0.10.2 ***
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Setting the run_list to ["recipe[apt]", "recipe[build-essential]", "recipe[chef-redis::source]", "recipe[openssl]", "recipe[git]", "recipe[postgresql::server]", "recipe[postgresql::client]", "recipe[vagrant-main]"] from JSON
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Run List is [recipe[apt], recipe[build-essential], recipe[chef-redis::source], recipe[openssl], recipe[git], recipe[postgresql::server], recipe[postgresql::client], recipe[vagrant-main]]
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Run List expands to [apt, build-essential, chef-redis::source, openssl, git, postgresql::server, postgresql::client, vagrant-main]
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Starting Chef Run for lucid32
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Processing package[postgresql-client] action install (postgresql::client line 37)
[Sat, 11 Feb 2012 22:20:04 -0800] ERROR: package[postgresql-client] (postgresql::client line 37) has had an error
[Sat, 11 Feb 2012 22:20:04 -0800] ERROR: Running exception handlers
[Sat, 11 Feb 2012 22:20:04 -0800] ERROR: Exception handlers complete
[Sat, 11 Feb 2012 22:20:04 -0800] FATAL: Stacktrace dumped to /tmp/vagrant-chef-1/chef-stacktrace.out
[Sat, 11 Feb 2012 22:20:04 -0800] FATAL: Chef::Exceptions::Exec: package[postgresql-client] (postgresql::client line 37) had an error: apt-get -q -y install postgresql-client=8.4.8-0ubuntu0.10.04 returned 100, expected 0
Run Code Online (Sandbox Code Playgroud)

rta*_*oni 31

您可以在一开始就包含apt配方:

include_recipe 'apt'
Run Code Online (Sandbox Code Playgroud)

这将运行更新命令.


Gab*_*ans 21

apt-get update应该先按照你的方式运行.但是,配方只会每24小时更新一次:

execute "apt-get-update-periodic" do
  command "apt-get update"
  ignore_failure true
  only_if do
    File.exists?('/var/lib/apt/periodic/update-success-stamp') &&
    File.mtime('/var/lib/apt/periodic/update-success-stamp') < Time.now - 86400
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 当timestamp文件尚不存在时,这将不会运行`apt-get update`,它最初不会执行.在我看来,正确的逻辑将是`!File.exists?(...)|| File.mtime ...` (6认同)

jmo*_*oss 10

有三种资源可以在ubuntu系统上很好地完成,特别是在12.04精确64位上使用.

  1. 当其他食谱需要时,随意运行apt-get-update

  2. 安装update-notifier-common软件包,为我们提供更新时间戳

  3. 检查时间戳并定期更新.在这种情况下,在86400秒之后.

这是三个食谱.

execute "apt-get-update" do
  command "apt-get update"
  ignore_failure true
  action :nothing
end

package "update-notifier-common" do
  notifies :run, resources(:execute => "apt-get-update"), :immediately
end

execute "apt-get-update-periodic" do
  command "apt-get update"
  ignore_failure true
  only_if do
   File.exists?('/var/lib/apt/periodic/update-success-stamp') &&
   File.mtime('/var/lib/apt/periodic/update-success-stamp') < Time.now - 86400
  end
end
Run Code Online (Sandbox Code Playgroud)


Jak*_*ake 8

看起来最新版本的opscode apt cookbook允许您在编译时运行它.

config.vm.provision :chef_solo do |chef|
  chef.cookbooks_path = "cookbooks"
  chef.add_recipe "apt"
  chef.json = { "apt" => {"compiletime" => true} }
end
Run Code Online (Sandbox Code Playgroud)

只要apt在运行列表中的其他编译时间烹饪书(如postgres)之前运行,这应该可行.


ash*_*her 3

我似乎能够通过应用以下补丁来解决该问题:

https://github.com/wil/cookbooks/commit/a470a4f68602ec3bf3374830f4990a7e19e9de81

  • 这是一种非常奇怪的安装包的方式(即使用“action :nothing”和“run_action(:install)”。这似乎是在厨师客户端运行的编译阶段而不是执行阶段安装包。请参阅[ http://wiki.opscode.com/display/chef/Anatomy+of+a+Chef+Run]了解详细信息。将`recipe[apt]`添加到节点运行列表的前面通常应该运行`apt-get install ` 作为第一个动作。 (4认同)