Class: BarkingIguana::Compound::Ansible::InventoryWriter

Inherits:
Object
  • Object
show all
Includes:
Logging::Helper
Defined in:
lib/barking_iguana/compound/ansible/inventory_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ InventoryWriter

Returns a new instance of InventoryWriter.



13
14
15
16
# File 'lib/barking_iguana/compound/ansible/inventory_writer.rb', line 13

def initialize path = nil
  self.path = path || generate_random_filename
  self.hosts = []
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/barking_iguana/compound/ansible/inventory_writer.rb', line 7

def path
  @path
end

Instance Method Details

#add_host(host) ⇒ Object



18
19
20
21
# File 'lib/barking_iguana/compound/ansible/inventory_writer.rb', line 18

def add_host host
  hosts << host
  hosts.uniq! &:ip_address
end

#generate_random_filenameObject



23
24
25
26
27
28
29
30
# File 'lib/barking_iguana/compound/ansible/inventory_writer.rb', line 23

def generate_random_filename
  name = [
    'inventory',
    Process.pid,
    Time.now.to_i,
    rand(9_999_999_999).to_s.rjust(10, '0')
  ].join('-')
end

#to_sObject



38
39
40
41
42
# File 'lib/barking_iguana/compound/ansible/inventory_writer.rb', line 38

def to_s
  hosts.sort_by(&:name).map do |h|
    %Q(#{h.inventory_name} ansible_host=#{h.ip_address} ansible_user=#{h.ssh_username} ansible_ssh_private_key_file=#{h.ssh_key} ansible_ssh_extra_args="#{h.ssh_extra_args}")
  end.join("\n")
end

#write_fileObject



32
33
34
35
36
# File 'lib/barking_iguana/compound/ansible/inventory_writer.rb', line 32

def write_file
  File.open path, 'w' do |inventory|
    inventory.puts to_s
  end
end