Class: ForemanAcd::InventoryCreator

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_acd/inventory_creator.rb

Overview

inventory creator for application instances

Instance Method Summary collapse

Constructor Details

#initialize(app_instance, foreman_hosts) ⇒ InventoryCreator

Returns a new instance of InventoryCreator.



8
9
10
11
# File 'app/services/foreman_acd/inventory_creator.rb', line 8

def initialize(app_instance, foreman_hosts)
  @app_instance = app_instance
  @foreman_hosts = foreman_hosts
end

Instance Method Details

#create_inventoryObject

TODO: this might be part of the smart proxy plugin.



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
# File 'app/services/foreman_acd/inventory_creator.rb', line 14

def create_inventory
  inventory = {}
  inventory['all'] = {}

  inventory['all'] = { 'vars' => inventory_all_vars } if @app_instance.ansible_vars_all.present?

  services = JSON.parse(@app_instance.app_definition.services)

  children = {}
  @foreman_hosts.each do |foreman_host|
    if foreman_host.host_id.nil?
      logger.warn "Ignore host #{foreman_h.hostname} because no foreman host id could be found. Is the host not provisioned yet?"
      next
    end

    service_id = foreman_host.service.to_i
    host_service = services.find { |s| s['id'] == service_id }
    ansible_group = host_service['ansibleGroup']

    children[ansible_group] = { 'hosts' => {} } unless children.key?(host_service['ansibleGroup'])

    ansible_vars = JSON.parse(foreman_host.ansibleParameters).map { |v| { v['name'] => get_param_value(foreman_host.host, v['value']) } }.reduce({}, :merge!)

    # in case there is no ansible_user defined, set "root" as default.
    ansible_vars['ansible_user'] = 'root' unless ansible_vars.key?('ansible_user')
    ansible_vars['ansible_ssh_private_key_file'] = ansible_ssh_private_key(foreman_host.host)

    children[ansible_group]['hosts'][foreman_host.host.name] = ansible_vars
  end
  inventory['all']['children'] = children
  inventory
end