Class: Ansible::Inventory
- Inherits:
-
Object
- Object
- Ansible::Inventory
- Defined in:
- lib/ansible/inventory.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Inventory
constructor
A new instance of Inventory.
- #write_file(file) ⇒ Object
Constructor Details
#initialize ⇒ Inventory
Returns a new instance of Inventory.
73 74 75 76 |
# File 'lib/ansible/inventory.rb', line 73 def initialize @hosts = Host::Collection.new @groups = Group::Collection.new end |
Instance Attribute Details
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
71 72 73 |
# File 'lib/ansible/inventory.rb', line 71 def groups @groups end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
70 71 72 |
# File 'lib/ansible/inventory.rb', line 70 def hosts @hosts end |
Class Method Details
.read_file(file) ⇒ Object
6 7 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 |
# File 'lib/ansible/inventory.rb', line 6 def read_file(file) inventory = Inventory.new last_group = nil in_vars = false File.foreach(file) do |line| case when line =~ /^\[\S+\]$/ group_name = line[/^\[(\S+)\]$/, 1] in_vars = group_name.end_with?(':vars') if in_vars actual_group_name = group_name[0, group_name.index(':')] last_group = inventory.groups.find { |g| g.name == actual_group_name } last_group ||= inventory.groups.add(actual_group_name) else last_group = inventory.groups.add(group_name) end when line =~ /^\s*[^\[]\S+\s*(\S+=\S+\s*)*$/ host_name, *rest = line.split if in_vars && host_name.index('=') && rest.empty? k, v = host_name.split('=') last_group.vars[k] = v else vars = Hash[rest.map {|s| s.split('=')}] 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 else puts line end end inventory end |
Instance Method Details
#write_file(file) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ansible/inventory.rb', line 44 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| 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 } unless group.vars.empty? f.puts f.puts "[#{group.name}:vars]" group.vars.each {|k, v| f.puts "#{k}=#{v}" } end } end end |