Skip to content

Instantly share code, notes, and snippets.

@pburkholder
Forked from ryancragun/map_nodes_to_orgs.md
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pburkholder/079896cae3fc5c591a46 to your computer and use it in GitHub Desktop.
Save pburkholder/079896cae3fc5c591a46 to your computer and use it in GitHub Desktop.

Mapping Nodes to Organizations

Chef 11.x

You'll need to run this on the Server in Orgmapper, eg:

/opt/opscode/bin/orgmapper
orgmapper>> eval(::File.read("/path/to/code.rb"))

This is the code file you'll want to save

require 'json'

h = Hash.new {|h,k| h[k]=[]};
sql.select(:name, :org_id).from(:nodes).each {|n| h[n[:org_id]] << n[:name] }
h.map { |id, n| { ORGS.all.find { |o| o['guid'] == id }['name'] => n }}
h.dup.each { |id, n| h[ORGS.all.find { |o| o['guid'] == id }['name']] = h.delete(id) }
File.open('/tmp/nodes.json', 'w') { |f| f.write JSON.pretty_generate(h) }

Chef 12.x

Chef 12 moved everything to pgsql, so you can just query postgres

/opt/opscode/embedded/bin/psql -U opscode_chef -h 127.0.0.1
SELECT
  n.name AS node,
  o.name AS org
FROM nodes n
INNER JOIN orgs o ON n.org_id = o.id
GROUP BY
  n.name,
  o.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment