you probably want to pull out all the common resources into a new recipe like "_common.rb" or something and then
include_recipe "#{cookbook_name}::_common"
in both base.rb and backend.rb recipes, then only include one or the other recipe in your run_list for any given server. also probably want to keep the concerns together and use one cookbook with multiple recipes rather than your 'A' and 'B' cookbooks.
if you really want the 'base' recipe to be included everywhere and the 'backend' to override it then you probably want to include an attribute somewhere. you could push this attribute up into roles if you like. then it looks like you can have a single 'thing::default' recipe that has a conditional:
my_content = if node['thing']['type'] == "base"
"BASE data"
else
"BACKEND data"
end
file "/tmp/very_important_service.conf" do
content my_content
notifies :restart, 'service[ntp]'
end
for more complicated code you can use an if that wraps include_recipe so that you switch on the attribute to do two different things so default.rb might look like:
include_recipe "#{cookbook_name}::_common"
if node['thing']['type'] == "base"
include_recipe "#{cookbook_name}::_base"
else
include_recipe "#{cookbook_name}::_backend"
end
and you can set node['thing']['type'] either in a role or a role cookbook (in this case you'll want to keep one-cookbook-per-role-cookbook, so you'd have something like role_base::default and role_backend::default recipes, for now just trust me and don't do role::backend and role::base because you want to set the role attribute in attribute files and in the latter case you'll get surprised when all the attribute files in a cookbook always get parsed -- but start playing with vanilla roles first before moving on to role cookbooks, then read this again later if you start going down the role cookbook route).
On Tue Apr 1 08:06:53 2014, johnnym wrote:
Hi All,
I am new to chef so sorry if I misuse some words but I have a
question. As I tested it if a node has 2 recipes and these recipes
work on the same resource (a file or a package...or user) how can I
prevent the node from rebuilding and restarting services all the time.
In this case I make a base system with the minimal packages services
and users, but with a specific base config file. On this base system I
want to create a backend so I apply B::backend but this recipe
installs packages, adds users and modifies a config file.
If I run chef-client (below) I can see that the users, packages and
files are removing and reinstalling all the time and the services are
restarting during every chef-client run.
Can I compile the recipes into one recipe in order to allow them to
overwrite the same resource in the order of the run_list?
{
"name": "javatest03xd.origo.t-online.private",
"chef_environment": "test",
...
"run_list": [
"recipe[A::base]",
"recipe[B::backend]"
]
}
_A/base.rb_
package "nfs-common" do
action :remove
end
service 'ntp' do
action [:enable, :start]
end
file "/tmp/very_important_service.conf" do
content "BASE data"
notifies :restart, 'service[ntp]'
end
user 'extra' do
action [:remove ]
end
_B/backend.rb_
package "nfs-common" do
action :remove
end
service 'ntp' do
action [:enable, :start]
end
file "/tmp/very_important_service.conf" do
content "BACKEND data"
notifies :restart, 'service[ntp]'
end
user 'extra' do
action [:create, :lock]
end
[2014-04-01T16:59:55+02:00] INFO: Forking chef instance to converge...
[2014-04-01T16:59:55+02:00] INFO: *** Chef 11.10.4 ***
[2014-04-01T16:59:55+02:00] INFO: Chef-client pid: 7730
[2014-04-01T16:59:56+02:00] INFO: Run List is [recipe[A::base], recipe[B::backend]]
[2014-04-01T16:59:56+02:00] INFO: Run List expands to [A::base, B::backend]
[2014-04-01T16:59:56+02:00] INFO: Starting Chef Run for javatest03xd.origo.t-online.private
[2014-04-01T16:59:56+02:00] INFO: Running start handlers
[2014-04-01T16:59:56+02:00] INFO: Start handlers complete.
[2014-04-01T16:59:56+02:00] INFO: HTTP Request Returned 404 Object Not Found:
[2014-04-01T16:59:56+02:00] INFO: Loading cookbooks [A, B]
[2014-04-01T16:59:56+02:00] WARN: Cloning resource attributes for package[nfs-common] from prior resource (CHEF-3694)
[2014-04-01T16:59:56+02:00] WARN: Previous package[nfs-common]: /var/chef/cache/cookbooks/A/recipes/base.rb:1:in `from_file'
[2014-04-01T16:59:56+02:00] WARN: Current package[nfs-common]: /var/chef/cache/cookbooks/B/recipes/backend.rb:1:in `from_file'
[2014-04-01T16:59:56+02:00] WARN: Cloning resource attributes for service[ntp] from prior resource (CHEF-3694)
[2014-04-01T16:59:56+02:00] WARN: Previous service[ntp]: /var/chef/cache/cookbooks/A/recipes/base.rb:5:in `from_file'
[2014-04-01T16:59:56+02:00] WARN: Current service[ntp]: /var/chef/cache/cookbooks/B/recipes/backend.rb:5:in `from_file'
[2014-04-01T16:59:56+02:00] WARN: Cloning resource attributes for file[/tmp/very_important_service.conf] from prior resource (CHEF-3694)
[2014-04-01T16:59:56+02:00] WARN: Previous file[/tmp/very_important_service.conf]: /var/chef/cache/cookbooks/A/recipes/base.rb:9:in `from_file'
[2014-04-01T16:59:56+02:00] WARN: Current file[/tmp/very_important_service.conf]: /var/chef/cache/cookbooks/B/recipes/backend.rb:9:in `from_file'
[2014-04-01T16:59:56+02:00] WARN: Cloning resource attributes for user[extra] from prior resource (CHEF-3694)
[2014-04-01T16:59:56+02:00] WARN: Previous user[extra]: /var/chef/cache/cookbooks/A/recipes/base.rb:14:in `from_file'
[2014-04-01T16:59:56+02:00] WARN: Current user[extra]: /var/chef/cache/cookbooks/B/recipes/backend.rb:14:in `from_file'
[2014-04-01T16:59:56+02:00] INFO: Processing package[nfs-common] action remove (A::base line 1)
[2014-04-01T16:59:57+02:00] INFO: package[nfs-common] removed
[2014-04-01T16:59:57+02:00] INFO: Processing service[ntp] action enable (A::base line 5)
[2014-04-01T16:59:57+02:00] INFO: Processing service[ntp] action start (A::base line 5)
[2014-04-01T16:59:57+02:00] INFO: Processing file[/tmp/very_important_service.conf] action create (A::base line 9)
[2014-04-01T16:59:57+02:00] INFO: file[/tmp/very_important_service.conf] backed up to /var/chef/backup/tmp/very_important_service.conf.chef-20140401165957.690683
[2014-04-01T16:59:57+02:00] INFO: file[/tmp/very_important_service.conf] updated file contents /tmp/very_important_service.conf
[2014-04-01T16:59:57+02:00] INFO: file[/tmp/very_important_service.conf] not queuing delayed action restart on service[ntp] (delayed), as it's already been queued
[2014-04-01T16:59:57+02:00] INFO: Processing user[extra] action remove (A::base line 14)
[2014-04-01T16:59:57+02:00] INFO: user[extra] removed
[2014-04-01T16:59:57+02:00] INFO: Processing package[nfs-common] action install (B::backend line 1)
[2014-04-01T17:00:00+02:00] INFO: Processing service[ntp] action enable (B::backend line 5)
[2014-04-01T17:00:00+02:00] INFO: Processing service[ntp] action start (B::backend line 5)
[2014-04-01T17:00:00+02:00] INFO: Processing file[/tmp/very_important_service.conf] action create (B::backend line 9)
[2014-04-01T17:00:00+02:00] INFO: file[/tmp/very_important_service.conf] backed up to /var/chef/backup/tmp/very_important_service.conf.chef-20140401170000.881799
[2014-04-01T17:00:00+02:00] INFO: file[/tmp/very_important_service.conf] updated file contents /tmp/very_important_service.conf
[2014-04-01T17:00:00+02:00] INFO: file[/tmp/very_important_service.conf] not queuing delayed action restart on service[ntp] (delayed), as it's already been queued
[2014-04-01T17:00:00+02:00] INFO: file[/tmp/very_important_service.conf] not queuing delayed action restart on service[ntp] (delayed), as it's already been queued
[2014-04-01T17:00:00+02:00] INFO: Processing user[extra] action create (B::backend line 14)
[2014-04-01T17:00:00+02:00] INFO: user[extra] created
[2014-04-01T17:00:00+02:00] INFO: Processing user[extra] action lock (B::backend line 14)
[2014-04-01T17:00:01+02:00] INFO: file[/tmp/very_important_service.conf] sending restart action to service[ntp] (delayed)
[2014-04-01T17:00:01+02:00] INFO: Processing service[ntp] action restart (B::backend line 5)
[2014-04-01T17:00:02+02:00] INFO: service[ntp] restarted
[2014-04-01T17:00:02+02:00] INFO: Chef Run complete in 6.074158866 seconds
[2014-04-01T17:00:02+02:00] INFO: Running report handlers
[2014-04-01T17:00:02+02:00] INFO: Report handlers complete
I hope I could explain my question clearly.
Best, Balint!
Archive powered by MHonArc 2.6.16.