Class: VagrantSpec::AnsibleInventory

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/vagrant_spec/ansible_inventory.rb

Overview

Generate ansible ansible_inventory file

env [Vagrant::Environment]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#lib_root, #project_root, #template_dir

Constructor Details

#initialize(env) ⇒ AnsibleInventory

Returns a new instance of AnsibleInventory.



15
16
17
18
19
20
21
# File 'lib/vagrant_spec/ansible_inventory.rb', line 15

def initialize(env)
  @env               = env
  @config            = env.vagrantfile.config
  @ansible_inventory = @config.spec.ansible_inventory
  @inventory         = {}
  @m_finder          = VagrantSpec::MachineFinder.new(env)
end

Instance Attribute Details

#ansible_inventoryObject

Returns the value of attribute ansible_inventory.



14
15
16
# File 'lib/vagrant_spec/ansible_inventory.rb', line 14

def ansible_inventory
  @ansible_inventory
end

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/vagrant_spec/ansible_inventory.rb', line 14

def config
  @config
end

#envObject

Returns the value of attribute env.



14
15
16
# File 'lib/vagrant_spec/ansible_inventory.rb', line 14

def env
  @env
end

#inventoryObject

Returns the value of attribute inventory.



14
15
16
# File 'lib/vagrant_spec/ansible_inventory.rb', line 14

def inventory
  @inventory
end

#m_finderObject

Returns the value of attribute m_finder.



14
15
16
# File 'lib/vagrant_spec/ansible_inventory.rb', line 14

def m_finder
  @m_finder
end

Instance Method Details

#generateObject



23
24
25
26
27
28
29
# File 'lib/vagrant_spec/ansible_inventory.rb', line 23

def generate
  load_ansible_inventory
  template_path = File.join(template_dir, 'vagrantspec_inventory.erb')
  template      = IO.read(template_path)
  f             = ERB.new(template, 0, '<>').result(binding)
  IO.write('vagrantspec_inventory', f)
end

#generate_machine_config(name) ⇒ Object

name [String]



61
62
63
64
65
66
# File 'lib/vagrant_spec/ansible_inventory.rb', line 61

def generate_machine_config(name)
  node = @m_finder.machine(name.to_sym)
  return nil unless node
  ssh_config = machine_ssh_config(node)
  { name => ssh_config }
end

#handle_array(group, nodes) ⇒ Object

group [String] nodes [Array<String>]



43
44
45
46
47
# File 'lib/vagrant_spec/ansible_inventory.rb', line 43

def handle_array(group, nodes)
  @inventory[group] = nodes.collect do |name|
    generate_machine_config(name)
  end
end

#handle_regexp(group, nodes) ⇒ Object

group [String] nodes [Array<String>]



51
52
53
54
55
56
57
58
# File 'lib/vagrant_spec/ansible_inventory.rb', line 51

def handle_regexp(group, nodes)
  machines = @m_finder.match_nodes(nodes)
  if machines
    @inventory[group] = machines.collect do |name|
      generate_machine_config(name.name)
    end
  end
end

#load_ansible_inventoryObject



31
32
33
34
35
36
37
38
39
# File 'lib/vagrant_spec/ansible_inventory.rb', line 31

def load_ansible_inventory
  @ansible_inventory.each do |group, nodes|
    if nodes.is_a?(Array)
      handle_array(group, nodes)
    elsif nodes.is_a?(Regexp)
      handle_regexp(group, nodes)
    end
  end
end

#machine_ssh_config(machine) ⇒ Object

machine [Vagrant::Machine]



69
70
71
72
73
74
75
76
77
78
# File 'lib/vagrant_spec/ansible_inventory.rb', line 69

def machine_ssh_config(machine)
  ssh_config = machine.ssh_info
  key = ssh_config[:private_key_path][0]
  config = {}
  config['ansible_host']                 = ssh_config[:host]
  config['ansible_port']                 = ssh_config[:port]
  config['ansible_user']                 = ssh_config[:username]
  config['ansible_ssh_private_key_file'] = key
  config
end