Dynamically determine the role that specified the recipe


#1

I have been looking through other cookbooks to figure out a way to get
the name of the role from within a recipe and haven’t found it. I
need a way to automatically remove the role from the nodes run list
without knowing what that role name is in advance. When the recipe is
run, it would figure out which role it was specified in.

I have successfully used node.role?(“myrole”) to remove a specified role
before. However, I need to dynamically determine the role that
specified the cookbook::recipe and then remove that role from the run
list.

Thanks
Randy


#2

On 4 Feb 2012, at 20:44, Van Fossan,Randy wrote:

I have been looking through other cookbooks to figure out a way to get
the name of the role from within a recipe and haven’t found it. I
need a way to automatically remove the role from the nodes run list
without knowing what that role name is in advance. When the recipe is
run, it would figure out which role it was specified in.

I have successfully used node.role?(“myrole”) to remove a specified role
before. However, I need to dynamically determine the role that
specified the cookbook::recipe and then remove that role from the run
list.

Thanks
Randy

I use this snippet in my mysql cookbook to do permissions checks:

node[ :roles ].each do |role|
next unless role =~ /-mysql-master$/
end

This loops over all roles currently assigned to the node. I’m fairly sure that it includes the fully expanded role list (i.e. roles added to the run list from inside another role.)

-ash


#3

I may be able to use that. I would still like to dynamically determine
the role that calls the recipe.

-----Original Message-----
From: Ash Berlin [mailto:ash_opscode@firemirror.com]
Sent: Saturday, February 04, 2012 4:13 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Dynamically determine the role that specified the
recipe

On 4 Feb 2012, at 20:44, Van Fossan,Randy wrote:

I have been looking through other cookbooks to figure out a way to
get
the name of the role from within a recipe and haven’t found it. I
need a way to automatically remove the role from the nodes run list
without knowing what that role name is in advance. When the recipe
is
run, it would figure out which role it was specified in.

I have successfully used node.role?(“myrole”) to remove a specified
role
before. However, I need to dynamically determine the role that
specified the cookbook::recipe and then remove that role from the run
list.

Thanks
Randy

I use this snippet in my mysql cookbook to do permissions checks:

node[ :roles ].each do |role|
next unless role =~ /-mysql-master$/
end

This loops over all roles currently assigned to the node. I’m fairly
sure that it includes the fully expanded role list (i.e. roles added to
the run list from inside another role.)

-ash