Platform agnostic roles and/or cookbooks


#1

Hello all.

I would like to bootstrap a server with a base role and not care if it’s
rhel/centos or ubuntu/debian.
I’m experimenting and using a base role to run the cookbooks users, sudo, &
ntpd.
But ubuntu also needs the apt cookbook to run ‘apt-get update’ first. My
bootstrap for rhel/centos is failing because it can’t run the apt cookbook.

Is there something in the yum & apt cookbooks (or any cookbook for that
matter) that essentially says “only run this if you’re platform X, else
skip this cookbook”?

I’m fairly new and it seems like there should be a simple way to solve this.
Thanks!


#2

Cooper,

You could write your own recipe in your own custom “my-base” cookbook.

In your “my-base” cookbook’s default recipe, you can write:

if platform?(“ubuntu”)
include_recipe "apt"
end

In your “my-base” cookbook’s metadata, be sure to write:

depends “apt”

That last line in the metadata doesn’t actually add “recipe[apt]” to the
run-list. It just makes sure that the apt cookbook gets downloaded to the
chef-client, so that it can be run by your recipe, if you want to run it.

Then you can remove “recipe[apt]” from your base role’s run-list. Instead,
you can add “recipe[my-base]”, which includes the apt recipe only on ubuntu.

Cheers,
Jay

On Sat, May 12, 2012 at 5:31 PM, Cooper Simmons cooper.simmons@gmail.comwrote:

Hello all.

I would like to bootstrap a server with a base role and not care if it’s
rhel/centos or ubuntu/debian.
I’m experimenting and using a base role to run the cookbooks users, sudo,
& ntpd.
But ubuntu also needs the apt cookbook to run ‘apt-get update’ first. My
bootstrap for rhel/centos is failing because it can’t run the apt cookbook.

Is there something in the yum & apt cookbooks (or any cookbook for that
matter) that essentially says “only run this if you’re platform X, else
skip this cookbook”?

I’m fairly new and it seems like there should be a simple way to solve
this.
Thanks!


#3

On May 12, 2012, at 6:17 PM, Jay Feldblum wrote:

You could write your own recipe in your own custom “my-base” cookbook.

In your “my-base” cookbook’s default recipe, you can write:

if platform?(“ubuntu”)
include_recipe "apt"
end

In your “my-base” cookbook’s metadata, be sure to write:

depends “apt”

However, that would mean that all the rhel/centos boxes would suddenly become dependent on the apt cookbook, even though they don’t use apt. The apt recipe might not be added to the run list, but the dependency would still be there.

Personally, I would create OS-specific roles & cookbooks for the stuff that needs to be os-specific (like depending on apt), and then include all the generic stuff that doesn’t need to care what particular platform you’re running on.


Brad Knowles brad@shub-internet.org
LinkedIn Profile: http://tinyurl.com/y8kpxu