lib/bolt/inventory/group.rb in bolt-0.17.2 vs lib/bolt/inventory/group.rb in bolt-0.18.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Bolt class Inventory # Group is a specific implementation of Inventory based on nested # structured data. class Group @@ -8,22 +10,21 @@ def initialize(data) @logger = Logging.logger[self] @name = data['name'] @nodes = {} - if data['nodes'] - data['nodes'].each do |n| - n = { 'name' => n } if n.is_a? String - if @nodes.include? n['name'] - @logger.warn("Ignoring duplicate node in #{@name}: #{n}") - else - @nodes[n['name']] = n - end + data['nodes']&.each do |n| + n = { 'name' => n } if n.is_a? String + if @nodes.include? n['name'] + @logger.warn("Ignoring duplicate node in #{@name}: #{n}") + else + @nodes[n['name']] = n end end @vars = data['vars'] || {} + @facts = data['facts'] || {} @config = data['config'] || {} @groups = if data['groups'] data['groups'].map { |g| Group.new(g) } else [] @@ -87,34 +88,36 @@ nil end # The data functions below expect and return nil or a hash of the schema - # { 'config' => Hash , 'vars' => Hash, groups => Array } - # As we add more options beyond config this schema will grow + # { 'config' => Hash , 'vars' => Hash, 'facts' => Hash, groups => Array } def data_for(node_name) data_merge(group_collect(node_name), node_collect(node_name)) end def node_data(node_name) if (data = @nodes[node_name]) { 'config' => data['config'] || {}, 'vars' => data['vars'] || {}, + 'facts' => data['facts'] || {}, # groups come from group_data 'groups' => [] } end end def group_data { 'config' => @config, 'vars' => @vars, + 'facts' => @facts, 'groups' => [@name] } end def empty_data { 'config' => {}, 'vars' => {}, + 'facts' => {}, 'groups' => [] } end def data_merge(data1, data2) if data2.nil? || data1.nil? @@ -125,9 +128,10 @@ 'config' => Bolt::Util.deep_merge(data1['config'], data2['config']), # Shallow merge instead of deep merge so that vars with a hash value # are assigned a new hash, rather than merging the existing value # with the value meant to replace it 'vars' => data2['vars'].merge(data1['vars']), + 'facts' => Bolt::Util.deep_merge(data1['facts'], data2['facts']), 'groups' => data2['groups'] + data1['groups'] } end # Returns all nodes contained within the group, which includes nodes from subgroups.