Class: Ansible::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/ansible/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


86
87
88
89
# File 'lib/ansible/inventory.rb', line 86

def initialize
  @hosts = Host::Collection.new
  @groups = Group::Collection.new
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups


84
85
86
# File 'lib/ansible/inventory.rb', line 84

def groups
  @groups
end

#hostsObject (readonly)

Returns the value of attribute hosts


83
84
85
# File 'lib/ansible/inventory.rb', line 83

def hosts
  @hosts
end

Class Method Details

.read_file(file) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ansible/inventory.rb', line 8

def read_file(file)
  inventory = Inventory.new
  last_group = nil
  in_vars = false
  in_children = false
  File.foreach(file) do |line|
    case
    when line =~ /^\s*#/
      next
    # [group:vars] or [group:chidren]
    when line =~ /^\[\S+:(vars|children)\]$/
      group_name, vars_or_children = line[/^\[(\S+)\]$/, 1].split(':')
      in_vars = vars_or_children == 'vars'
      in_children = vars_or_children == 'children'
      last_group = inventory.groups[group_name] || inventory.groups.add(group_name)
    # [group]
    when line =~ /^\[[^:]+\]$/
      group_name = line[/^\[(\S+)\]$/, 1]
      last_group = inventory.groups.add(group_name)
    when line =~ /^\s*[^\[]\S+\s*(\S+=\S+\s*)*$/
      host_name, *rest = line.split
      if in_children && rest.empty?
        child_group = inventory.groups[host_name] || inventory.groups.add(host_name)
        last_group.children << child_group.name
      elsif in_vars && host_name.index('=') && rest.empty?
        k, v = host_name.split('=', 2)
        last_group.vars[k] = v
      else
        vars = ActiveSupport::HashWithIndifferentAccess[rest.map {|s| s.split('=', 2)}]
        if last_group
          host = inventory.hosts.find {|h| h.name == host_name} || Host.new(host_name, vars)
          last_group.hosts << host
        else
          inventory.hosts.add host_name, vars
        end
      end
    end
  end
  inventory
end

Instance Method Details

#write_file(file) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ansible/inventory.rb', line 50

def write_file(file)
  File.open(file, 'w') do |f|
    hosts.each {|host|
      f.puts ([host.name] + host.vars.map {|k, v| "#{k}=#{v}"}).join(' ')
    }
    groups.each {|group|
      unless group.hosts.empty?
        f.puts
        f.puts "[#{group.name}]"
        group.hosts.each {|host|
          if hosts.find {|h| h == host }
            f.puts host.name
          else
            f.puts ([host.name] + host.vars.map {|k, v| "#{k}=#{v}"}).join(' ')
          end
        }
      end
      unless group.vars.empty?
        f.puts
        f.puts "[#{group.name}:vars]"
        group.vars.each {|k, v|
          f.puts "#{k}=#{v}"
        }
      end
      unless group.children.empty?
        f.puts
        f.puts "[#{group.name}:children]"
        group.children.each {|s| f.puts s}
      end
    }
  end
end