How to validate node role with recipe


#1

Hi Team,

I am trying to execute receipe which validate chef environment and role:
Recipe Example:

if (node.chef_environment == “dev” && node.role?(‘api’))

bash "Restarting service" do
  code <<-EOF
		#Codeblock
  EOF
end

end

if (node.chef_environment == “dev” && node.role?(‘web’))

bash "Restarting service" do
  code <<-EOF
		#Codeblock
  EOF
end

end

But while executing my code block is not working.


#2

Why wouldn’t you simply start the service in all cases? Or set an attribute, in the role, that starts the service?

I don’t think that the name of the role used is an attribute the node knows about, unless you create one specifically in the role attributes.