I know that attributes can be configured in cookbooks (attribute files and
recipes), roles, and environments.
Suppose I have a requirement on the following lines:
- Two environments: alpha and beta
- Two web servers: web-1, web-2
- Apache should run on port 80 on web-1 in alpha
- Apache should run on port 8080 on web-2 in alpha
- Apache should run on port 8081 on web-1 and web-2 in beta
I can have two approaches:
SOLUTION 1. Write a role cookbook (using application/library pattern) and
override the apache[listen_ports] attribute in the role cookbook. Again
override the apache[listen_ports] attribute in my
chef-repo/environments/alpha.rb and chef-repo/environments/beta.rb file.
But this way the configuration for Apache is scattered in multiple places
(environment files, role cookbooks and the apache community cookbook).
SOLUTION 2. Write a wrapper cookbook over the community apache cookbook. In
the attribute file of my wrapper cookbook, I write logic like this:
apache[listen_ports] = 8080
if env is alpha
if role is web1
apache[listen_ports] = 80
elseif role is web2
apache[listen_ports] = 8080
end
This way all apache settings are consolidated in one place i.e. in the
cookbook.
I have seen all the blog posts suggest the first solution. Why is the
second solution a bad idea?