su_
su_

Reputation: 55

add roles in chef via code

I wish to do

knife role create <role-name> -y

via ruby code.I donot wish to write another knife plugin.I am able to print the entire role list(see below code). But then now how do i add a simple role with no recipe in the role?

require 'rubygems'
require 'chef' 

Chef::Config[:node_name]='client_name'
Chef::Config[:client_key]='path to client cert.pem'
Chef::Config[:chef_server_url]="http://ur chef server:4000"    


puts Chef::Role.list

Thanks

Upvotes: 0

Views: 2030

Answers (1)

Tim Potter
Tim Potter

Reputation: 2457

The run list for a node is accessed via the run_list property. First you have to load the node from the Chef server though. The following code adds role[foo] to existing node n:

require 'rubygems'
require 'chef' 

Chef::Config[:node_name]='client_name'
Chef::Config[:client_key]='path to client cert.pem'
Chef::Config[:chef_server_url]="http://ur chef server:4000"    

node = Chef::Node.load('n')
node.run_list << "role[foo]"
node.save

For help figuring out how the Chef Ruby interface works I usually do "gem contents chef | xargs grep whatever" and have a good look around.

Upvotes: 2

Related Questions