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 = nil) ⇒ 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 = nil)
  @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



92
93
94
95
96
# File 'app/services/foreman_ansible/inventory_creator.rb', line 92

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

#ansible_params(host) ⇒ Object



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

def ansible_params(host)
  ForemanAnsible::AnsibleInfo.new(host).ansible_params
end

#ansible_settingsObject



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

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

#ansible_ssh_private_key(host) ⇒ Object



125
126
127
128
# File 'app/services/foreman_ansible/inventory_creator.rb', line 125

def ansible_ssh_private_key(host)
  ansible_private_file = host_setting(host, 'ansible_ssh_private_key_file')
  ansible_private_file unless ansible_private_file.empty?
end

#connection_params(host) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/foreman_ansible/inventory_creator.rb', line 46

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

  return {} unless @template_invocation
  ansible_settings.
    merge(remote_execution_options(host)).
    merge(ansible_extra_options(host))
end

#host_params(host) ⇒ Object



79
80
81
# File 'app/services/foreman_ansible/inventory_creator.rb', line 79

def host_params(host)
  host.host_params
end

#host_roles(host) ⇒ Object



61
62
63
# File 'app/services/foreman_ansible/inventory_creator.rb', line 61

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

#host_vars(host) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/services/foreman_ansible/inventory_creator.rb', line 37

def host_vars(host)
  {
    'foreman' => reduced_host_info(host).fetch('parameters', {}),
    'foreman_ansible_roles' => host_roles(host)
  }.merge(connection_params(host)).
    merge(host_params(host)).
    merge(ansible_params(host))
end

#hosts_varsObject



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

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

#reduced_host_info(host) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'app/services/foreman_ansible/inventory_creator.rb', line 69

def reduced_host_info(host)
  HostInfo.providers.each_with_object({}) do |provider_class, memo|
    next memo if [HostInfoProviders::HostParamsInfo, ForemanAnsible::AnsibleInfo].include? provider_class
    provider = provider_class.new(host)
    info = provider.host_info
    memo.deep_merge! info if info
    memo
  end
end

#remote_execution_options(host) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/services/foreman_ansible/inventory_creator.rb', line 98

def remote_execution_options(host)
  params = {
    'ansible_user' => @template_invocation&.job_invocation&.ssh_user || host_setting(host, 'remote_execution_ssh_user'),
    'ansible_become_method' => host_setting(host, 'remote_execution_effective_user_method'),
    'ansible_ssh_private_key_file' => ansible_ssh_private_key(host),
    'ansible_port' => host_setting(host, 'remote_execution_ssh_port'),
    'ansible_host' => AnsibleProvider.find_ip_or_hostname(host)
  }
  if @template_invocation.effective_user.present? && @template_invocation.effective_user != params['ansible_user']
    params['ansible_become_user'] = @template_invocation.effective_user
    params['ansible_become'] = true
  end
  # Backward compatibility for Ansible 1.x
  params['ansible_ssh_port'] = params['ansible_port']
  params['ansible_ssh_user'] = params['ansible_user']
  params
end

#template_inputs(template_invocation) ⇒ Object



116
117
118
119
120
121
122
123
# File 'app/services/foreman_ansible/inventory_creator.rb', line 116

def template_inputs(template_invocation)
  return {} unless template_invocation&.input_values
  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
# File 'app/services/foreman_ansible/inventory_creator.rb', line 21

def to_hash
  hosts = @hosts.map(&:name)

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