class Chef::Knife::AnsibleInventory
Public Instance Methods
filter_result()
click to toggle source
limit node query results to the group_by_attribute and the hostvars
# File lib/chef/knife/ansible_inventory.rb, line 39 def filter_result filter_result = {} config[:hostvars].split(',').each do |attr| key, value = attr.split('=') filter_result[key] = str_to_attr_path(value) end filter_result.merge( config[:group_by_attribute] => str_to_attr_path(config[:group_by_attribute]), config[:host_attribute] => str_to_attr_path(config[:host_attribute]) ) end
nodes_search()
click to toggle source
get all the nodes matching the given query
# File lib/chef/knife/ansible_inventory.rb, line 52 def nodes_search nodes = [] Chef::Search::Query.new.search( :node, config[:query], filter_result: filter_result ) { |n| nodes << n } nodes end
run()
click to toggle source
# File lib/chef/knife/ansible_inventory.rb, line 62 def run validate_options nodes = nodes_search inventory = {} until nodes.empty? # TODO: currently this doesnt handle nodes that should exist in multiple groups # organize nodes into groups based on matching group_by_attribute values match_value = str_to_attr_path(config[:group_by_attribute]).inject(nodes.first, &:[]) group_name = match_value.join(',') inventory[group_name] = { 'hosts' => [], 'vars' => {} } filtered_nodes = nodes.select do |n| n[config[:group_by_attribute]] == match_value end filtered_nodes.each do |n| inventory[group_name]['hosts'] << n nodes.delete(n) end # TODO: fill out group vars with end require 'pp' pp inventory end
str_to_attr_path(str)
click to toggle source
# File lib/chef/knife/ansible_inventory.rb, line 34 def str_to_attr_path(str) str.split('.') end
validate_options()
click to toggle source
# File lib/chef/knife/ansible_inventory.rb, line 26 def validate_options missing = [] %i(group_by_attribute host_attribute hostvars query).each do |opt| missing << opt unless config[opt] end raise ArgumentError, "Missing option(s): #{missing}" unless missing.empty? end