如何让 Chef 在运行其他食谱之前运行 apt-get update
现在,我的 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
为了安装添加到我的 Recipe_list 中的软件,我需要让虚拟机在安装其他软件之前发出apt-get update。
我的印象是,这是“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
Right now I have the following in my 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
In order to install the software added to my recipe_list, I need to get the VM to issue an apt-get update before installing the other software.
I was under the impression that this was one of the features of the 'apt' recipe - that it would run the update first thing.
The output when I do a vagrant provision is:
[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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以在开头包含 apt 配方:
这将运行更新命令。
You can include the apt recipe in the very beginning:
this will run the update command.
apt-get update
应该首先按照您的方式运行。不过,菜谱每 24 小时只会更新一次:apt-get update
should be running first the way you have it. However, the recipe will only update once every 24 hours:有三个资源可以在 ubuntu 系统上很好地完成此操作,特别是我在 12.04 精确 64 位上使用的资源。
当其他食谱需要时,随意运行 apt-get-update
安装 update-notifier-common 包,为我们提供更新时间戳
检查时间戳并定期更新。在本例中,86400 秒后如下。
这是这三个食谱。
There are three resources that will do this nicely on an ubuntu system, specifically in use on 12.04 precise 64 bit by me.
run apt-get-update at will when other recipes require
install update-notifier-common package that gives us timestamps on updates
check the timestamps and update periodically. In this case below after 86400 seconds.
And here's those three recipes.
看起来最新版本的 opscode apt Cookbook 允许您在编译时运行它。
只要 apt 在运行列表中的其他编译时说明书(例如 postgres)之前运行,就应该可以工作。
It looks like the latest version of the opscode apt cookbook allow you to run it at compile time.
As long as apt is run before other compiletime cookbooks (like postgres) in the run list, this should work.
此处发布的许多其他答案可能会引起有关资源克隆的警告。
根据Apt Cookbook文档,你应该可以通过设置
node['apt']['compile_time_update'] = true
来实现这一点,但是我自己在这种方法上从来没有什么运气。下面是我所做的:
这将加载原始的 apt-get update 资源并确保它运行时不会向资源集合中添加重复的条目。这将导致 apt-get update 在编译阶段的每次 Chef 运行期间执行:
显然,您还需要将 apt-get update 包含在您的元数据中.rb 文件:
A lot of the other answers posted here are likely to cause warnings about resource cloning.
According to the Apt cookbook documentation, you're supposed to be able to make this happen by setting
node['apt']['compile_time_update'] = true
, however I have never had much luck with this approach myself.Here's what I do instead:
This will load the original
apt-get update
resource and ensure that it runs without adding a duplicate entry to the resource collection. This will causeapt-get update
to execute during every Chef run during the compile phase:Obviously, you'll also want to include the
apt
cookbook in your metadata.rb file:我似乎能够通过应用以下补丁来解决该问题:
https://github.com/wil/cookbooks/commit/a470a4f68602ec3bf3374830f4990a7e19e9de81
I seem to have been able to solve the issue by applying the following patch:
https://github.com/wil/cookbooks/commit/a470a4f68602ec3bf3374830f4990a7e19e9de81
解决问题最简单、最直接的方法是应用以下补丁(h/t @ashchristopher):
https://github.com/wil/cookbooks/commit/a470a4f68602ec3bf3374830f4990a7e19e9de81
问题是
postgresql::client
配方对包资源运行安装操作在 postgresql/recipes/client.rb:39 和 44在 编译时而不是像正常那样运行时(h/ t Tim Potter),导致它们在其他任何东西运行之前由 Chef 评估(并因此安装)。我相信这是为了
database
Cookbook 的 postgres 提供程序,它依赖于postgresql
Cookbook 并依赖于在编译之前安装的pg
gem。应用上述补丁可能会破坏数据库
说明书。另一种替代解决方案是创建一个在编译时运行 apt-get update 的配方,并将其放在
run_list
之前的postgresql
中。食谱。最简单的形式可能是这样的:The simplest and most direct way to solve the problem is by applying the following patch (h/t @ashchristopher):
https://github.com/wil/cookbooks/commit/a470a4f68602ec3bf3374830f4990a7e19e9de81
The problem is that the
postgresql::client
recipe runs the install action on the package resources at postgresql/recipes/client.rb:39 and 44 at compile-time rather than run-time like normal (h/t Tim Potter), causing them to be evaluated by Chef (and thus installed) before anything else runs.I believe this is done in service of the
database
cookbook's postgres provider, which depends on thepostgresql
cookbook and relies on thepg
gem being installed before it will compile. Applying the above patch may break thedatabase
cookbook.The other alternative solution would be to create a recipe which runs
apt-get update
also at compile time and put it in yourrun_list
before thepostgresql
cookbook. In its simplest form that would probably be something like:如果不打补丁,这是解决问题的通用方法,每次运行都会更新:
可能值得考虑的是,这样的运行在每次运行时都会占用相当多的系统资源约 30 秒;您可能想要一个名为recipe::update_apt 的特殊配方,您已通过 cron 或其他事件运行该配方,即
Without patching, this is a generic approach to the problem that will update on every run:
It may be worth considering that such a run, on every run, ties up a fair bit of system resources for about 30 seconds; you may want to have a special recipe named recipe::update_apt that you have run via cron or via some other event i.e.
要在编译时运行 apt-get update,请执行以下操作:
检查 https://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time
To run apt-get update at compile time, do:
check https://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time
我也遇到了同样的情况,就我而言, apt Cookbook 在调用软件包安装的那本书之后是第二个。把它留在这里,也许有人会从中受益。检查食谱在您的运行列表、角色或其他任何地方的顺序。
I had the same situation, and in my case, apt cookbook was second after the one which called installation of package. Just leaving it here so maybe someone will benefit from it. Check the order of cookbooks in your runlist, role or wherever else.
只是一个友好的提醒,在流浪者条款中添加所有这些食谱很快就会变得难以管理。
更好的模式是创建一个厨师角色
chef/my-fancy-role.rb
然后将此角色添加到
Vagrantfile
配置部分:just a friendly reminder that adding all those recipes inside the vagrant provision can quickly become unmanageable.
A better pattern is to create a chef role
chef/my-fancy-role.rb
And then add this role to the
Vagrantfile
provisioning section:对于最新的 Chef 版本,即版本 14。
您还可以使用 https://docs.chef.io/resource_apt_update.html
apt_update
以下输出是我在本地模式(零)下为 Chef_14.5.33 运行该资源的实验:
For the recent Chef version, i.e version 14.
You could also use https://docs.chef.io/resource_apt_update.html
apt_update
The below output was my experiment running that resource for chef_14.5.33 in local mode (zero):