Class: Bolt::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/inventory.rb,
lib/bolt/inventory/group.rb

Defined Under Namespace

Classes: Group, ValidationError, WildcardError

Constant Summary collapse

ENVIRONMENT_VAR =
'BOLT_INVENTORY'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, config = nil) ⇒ Inventory

Returns a new instance of Inventory.



60
61
62
63
64
65
66
67
68
69
# File 'lib/bolt/inventory.rb', line 60

def initialize(data, config = nil)
  @logger = Logging.logger[self]
  # Config is saved to add config options to targets
  @config = config || Bolt::Config.new
  @data = data ||= {}
  @groups = Group.new(data.merge('name' => 'all'))
  @group_lookup = {}
  @target_vars = {}
  @target_facts = {}
end

Class Method Details

.default_pathsObject



39
40
41
# File 'lib/bolt/inventory.rb', line 39

def self.default_paths
  [File.expand_path(File.join('~', '.puppetlabs', 'bolt', 'inventory.yaml'))]
end

.from_config(config) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bolt/inventory.rb', line 43

def self.from_config(config)
  if ENV.include?(ENVIRONMENT_VAR)
    begin
      data = YAML.safe_load(ENV[ENVIRONMENT_VAR])
    rescue Psych::Exception
      raise Bolt::CLIError, "Could not parse inventory from $#{ENVIRONMENT_VAR}"
    end
  else
    data = Bolt::Util.read_config_file(config[:inventoryfile], default_paths, 'inventory')
  end

  inventory = new(data, config)
  inventory.validate
  inventory.collect_groups
  inventory
end

Instance Method Details

#add_facts(target, new_facts = {}) ⇒ Object



99
100
101
102
# File 'lib/bolt/inventory.rb', line 99

def add_facts(target, new_facts = {})
  @logger.warn("No facts to add") if new_facts.empty?
  set_facts(target.name, new_facts)
end

#collect_groupsObject



75
76
77
78
# File 'lib/bolt/inventory.rb', line 75

def collect_groups
  # Provide a lookup map for finding a group by name
  @group_lookup = @groups.collect_groups
end

#facts(target) ⇒ Object



104
105
106
# File 'lib/bolt/inventory.rb', line 104

def facts(target)
  @target_facts[target.name] || {}
end

#get_targets(targets) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/bolt/inventory.rb', line 80

def get_targets(targets)
  targets = expand_targets(targets)
  targets = if targets.is_a? Array
              targets.flatten.uniq(&:name)
            else
              [targets]
            end
  targets.map { |t| update_target(t) }
end

#set_var(target, key, value) ⇒ Object



90
91
92
93
# File 'lib/bolt/inventory.rb', line 90

def set_var(target, key, value)
  data = { key => value }
  set_vars_from_hash(target.name, data)
end

#validateObject



71
72
73
# File 'lib/bolt/inventory.rb', line 71

def validate
  @groups.validate
end

#vars(target) ⇒ Object



95
96
97
# File 'lib/bolt/inventory.rb', line 95

def vars(target)
  @target_vars[target.name] || {}
end