Class: ForemanAnsible::InventoryCreator

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

Overview

Service to list an inventory to be passed to the ansible-playbook binary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hosts, template_invocation) ⇒ InventoryCreator

Returns a new instance of InventoryCreator.



9
10
11
12
# File 'app/services/foreman_ansible/inventory_creator.rb', line 9

def initialize(hosts, template_invocation)
  @hosts = hosts
  @template_invocation = template_invocation
end

Instance Attribute Details

#hostsObject (readonly)

Returns the value of attribute hosts.



7
8
9
# File 'app/services/foreman_ansible/inventory_creator.rb', line 7

def hosts
  @hosts
end

Instance Method Details

#ansible_extra_options(host) ⇒ Object



86
87
88
89
90
# File 'app/services/foreman_ansible/inventory_creator.rb', line 86

def ansible_extra_options(host)
  host.host_params.select do |key, _|
    /ansible_/.match(key) || Setting[key]
  end
end

#ansible_or_rex_ssh_private_key(host) ⇒ Object



120
121
122
123
124
125
126
127
# File 'app/services/foreman_ansible/inventory_creator.rb', line 120

def ansible_or_rex_ssh_private_key(host)
  ansible_private_file = host_setting(host, 'ansible_ssh_private_key_file')
  if !ansible_private_file.empty?
    ansible_private_file
  else
    ForemanRemoteExecutionCore.settings[:ssh_identity_key_file]
  end
end

#ansible_settingsObject



77
78
79
80
81
82
83
84
# File 'app/services/foreman_ansible/inventory_creator.rb', line 77

def ansible_settings
  Hash[
    %w[connection ssh_private_key_file
       winrm_server_cert_validation].map do |setting|
      ["ansible_#{setting}", Setting["ansible_#{setting}"]]
    end
  ]
end

#connection_params(host) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/foreman_ansible/inventory_creator.rb', line 51

def connection_params(host)
  # Preference order is:
  # 1st option: host parameters.
  #   - If they're set to 'ansible_whatever' we use that over anything else
  # 2nd option: REX options.
  #   - both settings, ssh password, effective_user can be used
  # 3rd option:
  #   - other settings
  params = ansible_settings.
           merge(remote_execution_options(host)).
           merge(ansible_extra_options(host))
  params
end

#host_attributes(host) ⇒ Object



69
70
71
# File 'app/services/foreman_ansible/inventory_creator.rb', line 69

def host_attributes(host)
  render_rabl(host, 'api/v2/hosts/main')
end

#host_params(host) ⇒ Object



73
74
75
# File 'app/services/foreman_ansible/inventory_creator.rb', line 73

def host_params(host)
  host.host_params
end

#host_roles(host) ⇒ Object



65
66
67
# File 'app/services/foreman_ansible/inventory_creator.rb', line 65

def host_roles(host)
  host.all_ansible_roles.map(&:name)
end

#host_vars(host) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/foreman_ansible/inventory_creator.rb', line 39

def host_vars(host)
  result = {
    'foreman' => host_attributes(host),
    'foreman_params' => host_params(host),
    'foreman_ansible_roles' => host_roles(host)
  }.merge(connection_params(host))
  if Setting['top_level_ansible_vars']
    result = result.merge(host_params(host))
  end
  result
end

#hosts_varsObject



31
32
33
34
35
36
37
# File 'app/services/foreman_ansible/inventory_creator.rb', line 31

def hosts_vars
  hosts.reduce({}) do |hash, host|
    hash.update(
      RemoteExecutionProvider.find_ip_or_hostname(host) => host_vars(host)
    )
  end
end

#remote_execution_options(host) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/services/foreman_ansible/inventory_creator.rb', line 92

def remote_execution_options(host)
  params = {
    'ansible_become_user' => @template_invocation.effective_user,
    'ansible_user' => host_setting(host, 'remote_execution_ssh_user'),
    'ansible_ssh_pass' => rex_ssh_password(host),
    'ansible_ssh_private_key_file' => ansible_or_rex_ssh_private_key(host),
    'ansible_port' => host_setting(host, 'remote_execution_ssh_port')
  }
  params['ansible_become'] = true if params['ansible_become_user'].present?
  # Backward compatibility for Ansible 1.x
  params['ansible_ssh_port'] = params['ansible_port']
  params['ansible_ssh_user'] = params['ansible_user']
  params
end

#rex_ssh_password(host) ⇒ Object



115
116
117
118
# File 'app/services/foreman_ansible/inventory_creator.rb', line 115

def rex_ssh_password(host)
  @template_invocation.job_invocation.password ||
    host_setting(host, 'remote_execution_ssh_password')
end

#template_inputs(template_invocation) ⇒ Object



107
108
109
110
111
112
113
# File 'app/services/foreman_ansible/inventory_creator.rb', line 107

def template_inputs(template_invocation)
  input_values = template_invocation.input_values
  result = input_values.each_with_object({}) do |input, vars_hash|
    vars_hash[input.template_input.name] = input.value
  end
  result
end

#to_hashObject

It returns a hash in a format that Ansible understands. See docs.ansible.com/ansible/developing_inventory.html for more details. For now, we don’t group the hosts based on different paramters (use github.com/theforeman/foreman_ansible_inventory for more advanced cases). Therefore we have only the ‘all’ group with all hosts.



21
22
23
24
25
26
27
28
29
# File 'app/services/foreman_ansible/inventory_creator.rb', line 21

def to_hash
  hosts = @hosts.map do |h|
    RemoteExecutionProvider.find_ip_or_hostname(h)
  end

  { 'all' => { 'hosts' => hosts,
               'vars'  => template_inputs(@template_invocation) },
    '_meta' => { 'hostvars' => hosts_vars } }
end