Class: Subspace::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/subspace/inventory.rb

Defined Under Namespace

Classes: Group, Host

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInventory

Returns a new instance of Inventory.



5
6
7
8
9
# File 'lib/subspace/inventory.rb', line 5

def initialize
  @hosts = {}
  @group_vars = {}
  @global_vars = {}
end

Instance Attribute Details

#global_varsObject

Returns the value of attribute global_vars.



4
5
6
# File 'lib/subspace/inventory.rb', line 4

def global_vars
  @global_vars
end

#group_varsObject

Returns the value of attribute group_vars.



4
5
6
# File 'lib/subspace/inventory.rb', line 4

def group_vars
  @group_vars
end

#hostsObject

Returns the value of attribute hosts.



4
5
6
# File 'lib/subspace/inventory.rb', line 4

def hosts
  @hosts
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/subspace/inventory.rb', line 4

def path
  @path
end

Class Method Details

.read(path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/subspace/inventory.rb', line 25

def self.read(path)
  inventory = new
  inventory.path = path

  yml = YAML.load(File.read(path)).to_h

  # Run through all hosts
  yml["all"]["hosts"].each do |name, vars|
    inventory.hosts[name] = Host.new(name, vars: vars || {})
  end

  # Run through all children (groups)
  # This does NOT handle sub-groups yet
  yml["all"]["children"].each do |name, group|
    next unless group["hosts"]

    # Each group defines its host membership
    group["hosts"].each do |host, vars|
      inventory.hosts[host] ||= Host.new(host, vars: vars || {})
      inventory.hosts[host].group_list.push name
    end

    if group["vars"]
      inventory.group_vars[name] = group["vars"]
    end
  end

  # Capture global variables
  inventory.global_vars = yml["all"]["vars"] || {}

  inventory
end

Instance Method Details

#find_hosts!(host_spec) ⇒ Object

Find all the hosts in the host/group or exit



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/subspace/inventory.rb', line 12

def find_hosts!(host_spec)
  if self.groups[host_spec]
    return self.groups[host_spec].host_list.map { |m| self.hosts[m] }
  elsif self.hosts[host_spec]
    return [self.hosts[host_spec]]
  else
    say "No inventory matching: '#{host_spec}' found. "
    say (["Available hosts:"] + self.hosts.keys).join("\n\t")
    say (["Available groups:"] + self.groups.keys).join("\n\t")
    exit
  end
end

#groupsObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/subspace/inventory.rb', line 112

def groups
  @groups ||= begin
    all_groups = {"all" => Group.new("all", vars: {}, host_list: hosts.keys) }
    @hosts.each do |name, host|
      host.group_list.each do |group|
        all_groups[group] ||= Group.new(group, vars: @group_vars[group])
        all_groups[group].host_list.append(name)
      end
    end
    all_groups
  end
end

#merge(inventory_json) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/subspace/inventory.rb', line 62

def merge(inventory_json)
  inventory_json["hostnames"].each_with_index do |host, i|
    if hosts[host]
      old_ip = hosts[host].vars["ansible_host"]
      new_ip = inventory_json["ip_addresses"][i]
      if old_ip != new_ip
        say "  * Host '#{host}' IP address changed! You may need to update the inventory! (#{old_ip} => #{new_ip})"
      end
      next
    end
    hosts[host] = Host.new(host)
    hosts[host].vars["ansible_host"] = inventory_json["ip_addresses"][i]
    hosts[host].vars["ansible_user"] = inventory_json["users"][i]
    hosts[host].vars["hostname"] = host
    hosts[host].group_list = inventory_json["groups"][i].split(/\s/)
  end
end

#to_ymlObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/subspace/inventory.rb', line 80

def to_yml
  all_groups = {}
  all_hosts = {}
  @hosts.each do |name, host|
    all_hosts[host.name] = host.vars.empty? ? nil : host.vars.transform_keys(&:to_s)
    host.group_list.each do |group|
      all_groups[group] ||= { "hosts" => {}}
      all_groups[group]["hosts"][host.name] = nil
    end
  end

  @group_vars.each do |group, vars|
    all_groups[group] ||= {}
    if !vars.empty?
      all_groups[group]["vars"] = vars.transform_keys(&:to_s)
    end
  end

  yml = {
    "all" => {
      "hosts" => all_hosts,
      "children" => all_groups.empty? ? nil : all_groups
    }
  }

  if !@global_vars.empty?
    yml["all"]["vars"] = @global_vars.transform_keys(&:to_s)
  end

  YAML.dump(yml)
end

#write(path = nil) ⇒ Object



58
59
60
# File 'lib/subspace/inventory.rb', line 58

def write(path=nil)
  File.write(@path || path, self.to_yml)
end