Skip to content

Instantly share code, notes, and snippets.

@ctdk
Created June 22, 2015 20:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctdk/16623c2453640448b6d6 to your computer and use it in GitHub Desktop.
Save ctdk/16623c2453640448b6d6 to your computer and use it in GitHub Desktop.
A ruby script to create a whole bunch of chef nodes.
#!/usr/bin/env ruby
require 'chef-api'
require 'fauxhai'
include ChefAPI::Resource
ChefAPI.configure do |config|
config.endpoint = 'http://HOSTNAME:4545'
config.client = 'admin'
config.key = '~/etc/goiardi/admin.pem'
end
datacenters = [ { :city => "Vagrantheim", :cluster => "VAG", :name => "VAG", :region => "US-West" }, { :city => "San Jose", :cluster => "SJC", :name => "SJC", :region => "US-California" }, { :city => "McMurdo", :cluster => "MCM", :name => "MCM", :region => "Antarctica" } ]
run_lists = [ "recipe[datacenter::vagrant]", "role[foo]", "recipe[scout],recipe[bnerk]", "role[foo],recipe[scout]" ]
pkeys = [ "debian", "ubuntu", "centos" ]
platforms = {
"debian" => [
"6.0.5", "7.5", "7.8"
],
"ubuntu" => [
"12.04", "14.04", "10.04"
],
"centos" => [
"5.9", "6.6", "7.0"
]
}
(100..10000).each do |x|
p = rand(3)
v = rand(3)
auto = Fauxhai.mock(platform: pkeys[p], version: platforms[pkeys[p]][v]) do |node|
node['hostname'] = "server#{x}.example.com"
end
n = Node.new
n.name = "server#{x}.example.com"
n.automatic = auto.data
ds = rand(3)
rl = rand(4)
n.default['datacenter'] = datacenters[ds]
n.run_list = run_lists[rl].split(/,/)
n.save
Client.create(:name => n.name)
end
@ranjib
Copy link

ranjib commented Jul 3, 2015

without chef-api

# configure 
Chef::Config.from_file('/path/to/knife.rb')
# or
Chef::Config[:node_name] = 'foo'
Chef::Config[:client_key] = '/path/to/key.pem'
Chef::Config[:chef_server_url] = 'https://foo.bar.com'


# create node object
n = Chef::Node.new
n.name('aha')
n.chef_environment("test-environment")
n.run_list << 'role[foo]'

# inject fauxhai data
n.consume_external_attrs(Fauxhai.mock(platform: 'ubuntu', version: '14.04').data,  {})

# save the node 
n.save

note.. unless you need to populate the node in chef server, you wont need save and configure related code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment