Skip to content

Instantly share code, notes, and snippets.

@johnskopis
Created December 13, 2013 00:30
Show Gist options
  • Save johnskopis/7938140 to your computer and use it in GitHub Desktop.
Save johnskopis/7938140 to your computer and use it in GitHub Desktop.
require 'csv'
DEFAULT_ATTRS = %w[
network.interfaces.eth0.addresses
network.interfaces.eth0.addresses
]
DEFAULT_QUERY = 'role:base'
DEFAULT_FILTER = Proc.new do |addresses|
ipa = addresses.select { |address, config| config[:family] == 'inet' }.keys
ipa.size == 1 ? ipa.first : ipa
end
def data_for_csv(query = DEFAULT_FILTER,
attrs = DEFAULT_ATTRS,
filter = DEFAULT_FILTER
)
search(:node, query).map do |node|
[node.name] +
attrs.map do |attr|
filter.call(lookup_attr(node, attr))
end
end
end
def lookup_attr(node, attr)
attr.split('.').inject(node) do |memo, meth|
memo.send meth.to_sym
end
end
puts DEFAULT_ATTRS.to_csv
csv = CSV.generate do |csv|
data_for_csv.each do |data|
csv << data
end
end
puts csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment