foreman_ansible 2.2.1 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf20c117b247b7bac631b02cdf847af6e29a5eb6
4
- data.tar.gz: cfc11ffafe093538f48322fbd581110e34be83a4
3
+ metadata.gz: 6ade1f70967edc1792c611054dfb7cda66cc68f3
4
+ data.tar.gz: af7d9bf908aa6ee56899d7840d16fa1d04bed82a
5
5
  SHA512:
6
- metadata.gz: 1948c441fdfe3002aee7e94abf2b0e742d0d1bd24c6a13405c72f347eb1111baf245343cd3ffb1718ae78f535b422b228964981b0b8cff2a0425c917b238f7b4
7
- data.tar.gz: a804813c0baceb26837bb63f25d24bd57d912a74184a143ddb08127afbbc8f831c41f89dfd3f0438a8534e6b32ff3da697c213748f9d83d2bc699dc4a94fa451
6
+ metadata.gz: 96a268265d406b5576b928f339684f0cff15f440d21ff8ea1fa9d0bae68fafb76624e313908568a653ebf85f56f38971d1b4b7226b817ca6e97d0dfdefd0f31d
7
+ data.tar.gz: 9d27e74f738166ce43b531ac69e7429c2725d9070cb6f302b557961ae05f3070766129f0ce24efa4aeff5ca810f5bc80f6d850d3680a505559dc1d459e027c29
@@ -6,7 +6,8 @@ module ForemanAnsible
6
6
  module Overrides
7
7
  def host_title_actions(*args)
8
8
  host = args.first
9
- if ansible_roles_present?(host)
9
+ if ansible_roles_present?(host) &&
10
+ User.current.can?(:create_job_invocations)
10
11
  button = ansible_roles_button(host)
11
12
  title_actions(button_group(button))
12
13
  end
@@ -14,10 +15,14 @@ module ForemanAnsible
14
15
  end
15
16
 
16
17
  def multiple_actions
17
- super +
18
- [[_('Play Ansible roles'),
19
- multiple_play_roles_hosts_path,
20
- false]]
18
+ actions = super
19
+ if User.current.can?(:create_job_invocations) &&
20
+ User.current.can?(:play_roles_on_host)
21
+ actions += [[_('Play Ansible roles'),
22
+ multiple_play_roles_hosts_path,
23
+ false]]
24
+ end
25
+ actions
21
26
  end
22
27
  end
23
28
 
@@ -31,9 +36,9 @@ module ForemanAnsible
31
36
  end
32
37
 
33
38
  def ansible_roles_button(host)
34
- link_to(
39
+ display_link_if_authorized(
35
40
  _('Run Ansible roles'),
36
- play_roles_host_path(:id => host.id),
41
+ hash_for_play_roles_host_path(:id => host.id),
37
42
  :id => :ansible_roles_button,
38
43
  :class => 'btn btn-default',
39
44
  :'data-no-turbolink' => true
@@ -1,3 +1,4 @@
1
+ require 'ipaddress'
1
2
  module ForemanAnsible
2
3
  # Relations to make Host::Managed 'have' ansible roles
3
4
  module HostManagedExtensions
@@ -44,6 +45,16 @@ module ForemanAnsible
44
45
  # rubocop:enable Metrics/AbcSize
45
46
  end
46
47
  # rubocop:enable Metrics/BlockLength
48
+ # Class methods we may need to override or add
49
+ module ClassMethods
50
+ def import_host(hostname, certname = nil, deprecated_proxy = nil)
51
+ host = super(hostname, certname, deprecated_proxy)
52
+ if IPAddress.valid?(hostname) && Nic::Interface.find_by(:ip => hostname)
53
+ host = Nic::Interface.find_by(:ip => hostname).host
54
+ end
55
+ host
56
+ end
57
+ end
47
58
  end
48
59
  end
49
60
 
@@ -27,6 +27,10 @@ if defined? ForemanRemoteExecution
27
27
  )
28
28
  end
29
29
 
30
+ def supports_effective_user?
31
+ true
32
+ end
33
+
30
34
  private
31
35
 
32
36
  def ansible_command?(template)
@@ -0,0 +1,19 @@
1
+ require 'ipaddress'
2
+ module ForemanAnsible
3
+ # Ensures Ansible reports from hosts where the IP was used, are assigned
4
+ # to the right hostname in Foreman
5
+ module AnsibleReportImporter
6
+ extend ActiveSupport::Concern
7
+ included do
8
+ def host
9
+ hostname = name.downcase
10
+ if AnsibleReportScanner.ansible_report?(raw['logs']) &&
11
+ IPAddress.valid?(hostname) &&
12
+ Nic::Interface.find_by(:ip => hostname)
13
+ @host = Nic::Interface.find_by(:ip => hostname).host
14
+ end
15
+ super
16
+ end
17
+ end
18
+ end
19
+ end
@@ -11,6 +11,7 @@ module ForemanAnsible
11
11
  end
12
12
 
13
13
  def ansible_report?(logs)
14
+ return false if logs.blank?
14
15
  logs.any? do |log|
15
16
  log['log'].fetch('messages', {}).
16
17
  fetch('message', '') =~ /"_ansible_parsed"/
@@ -91,12 +91,13 @@ module ForemanAnsible
91
91
 
92
92
  def remote_execution_options(host)
93
93
  params = {
94
- 'ansible_become' => @template_invocation.effective_user,
94
+ 'ansible_become_user' => @template_invocation.effective_user,
95
95
  'ansible_user' => host_setting(host, 'remote_execution_ssh_user'),
96
96
  'ansible_ssh_pass' => rex_ssh_password(host),
97
97
  'ansible_ssh_private_key_file' => ansible_or_rex_ssh_private_key(host),
98
98
  'ansible_port' => host_setting(host, 'remote_execution_ssh_port')
99
99
  }
100
+ params['ansible_become'] = true if params['ansible_become_user'].present?
100
101
  # Backward compatibility for Ansible 1.x
101
102
  params['ansible_ssh_port'] = params['ansible_port']
102
103
  params['ansible_ssh_user'] = params['ansible_user']
@@ -5,9 +5,12 @@
5
5
  display_link_if_authorized(_('Play Roles'), hash_for_play_roles_hostgroup_path(:id => hostgroup), :'data-no-turbolink' => true)
6
6
  end
7
7
 
8
- action_buttons(
9
- display_link_if_authorized(_('Nest'), hash_for_nest_hostgroup_path(:id => hostgroup)),
10
- display_link_if_authorized(_('Clone'), hash_for_clone_hostgroup_path(:id => hostgroup)),
11
- play_roles,
12
- display_delete_if_authorized(hash_for_hostgroup_path(:id => hostgroup).merge(:auth_object => hostgroup, :authorizer => authorizer), :data => { :confirm => warning_message(hostgroup) }))
8
+ actions = [
9
+ display_link_if_authorized(_('Nest'), hash_for_nest_hostgroup_path(:id => hostgroup)),
10
+ display_link_if_authorized(_('Clone'), hash_for_clone_hostgroup_path(:id => hostgroup))
11
+ ]
12
+ actions.push play_roles if User.current.can?(:create_job_invocations)
13
+ actions.push display_delete_if_authorized(hash_for_hostgroup_path(:id => hostgroup).merge(:auth_object => hostgroup, :authorizer => authorizer), :data => { :confirm => warning_message(hostgroup) })
14
+
15
+ action_buttons(*actions)
13
16
  %>
@@ -19,7 +19,6 @@ model: JobTemplate
19
19
  - hosts: all
20
20
  tasks:
21
21
  - command: |
22
- echo <%= input('action') %> host && sleep 3
23
22
  <%= case input('action')
24
23
  when 'restart'
25
24
  'shutdown -r +1'
@@ -104,6 +104,9 @@ module ForemanAnsible
104
104
  ::Api::V2::HostgroupsController.send(
105
105
  :include, ForemanAnsible::Api::V2::HostgroupsParamGroupExtensions
106
106
  )
107
+ ::ConfigReportImporter.send(
108
+ :include, ForemanAnsible::AnsibleReportImporter
109
+ )
107
110
  rescue StandardError => e
108
111
  Rails.logger.warn "Foreman Ansible: skipping engine hook (#{e})"
109
112
  end
@@ -2,5 +2,5 @@
2
2
  # This way other parts of Foreman can just call ForemanAnsible::VERSION
3
3
  # and detect what version the plugin is running.
4
4
  module ForemanAnsible
5
- VERSION = '2.2.1'.freeze
5
+ VERSION = '2.2.2'.freeze
6
6
  end
@@ -34,4 +34,15 @@ class HostManagedExtensionsTest < ActiveSupport::TestCase
34
34
  @host.inherited_ansible_roles.must_equal [@role2]
35
35
  end
36
36
  end
37
+
38
+ describe 'import facts' do
39
+ test 'when hostname looks like an IP it finds the right host' do
40
+ @host = ::FactoryBot.create(:host, :ip => '192.168.128.27')
41
+ assert_equal @host, Host::Managed.import_host(@host.ip)
42
+ end
43
+
44
+ test 'when hostname looks like an IP but there is no host with that IP' do
45
+ assert Host::Managed.import_host('192.168.128.27').new_record?
46
+ end
47
+ end
37
48
  end
@@ -0,0 +1,21 @@
1
+ require 'test_plugin_helper'
2
+ # Unit tests for AnsibleReportImporter
3
+ # This class is just meant to capture the config reports coming
4
+ # from Ansible and change anything that is required
5
+ class AnsibleReportImporterTest < ActiveSupport::TestCase
6
+ setup do
7
+ @raw = { 'host' => '192.168.121.1' }
8
+ @importer = ::ConfigReportImporter.new(@raw)
9
+ end
10
+
11
+ test 'finds host when the hostname is given as the IP' do
12
+ host = ::FactoryBot.create(:host, :ip => @raw['host'])
13
+ ForemanAnsible::AnsibleReportScanner.expects(:ansible_report?).returns(true)
14
+ assert_equal @importer.host, host
15
+ end
16
+
17
+ test 'creates new host if IP is not found' do
18
+ ForemanAnsible::AnsibleReportScanner.expects(:ansible_report?).returns(true)
19
+ assert @importer.host.new_record?
20
+ end
21
+ end
@@ -52,8 +52,9 @@ module ForemanAnsible
52
52
  @template_invocation)
53
53
  connection_params = inventory.connection_params(@host)
54
54
  assert_empty extra_options.to_a - inventory.connection_params(@host).to_a
55
+ assert_equal true, connection_params['ansible_become']
55
56
  assert_equal @template_invocation.effective_user,
56
- connection_params['ansible_become']
57
+ connection_params['ansible_become_user']
57
58
  assert_equal Setting['ansible_connection'],
58
59
  connection_params['ansible_connection']
59
60
  refute_equal Setting['remote_execution_ssh_user'],
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_ansible
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Lobato Garcia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-04 00:00:00.000000000 Z
11
+ date: 2018-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -127,6 +127,7 @@ files:
127
127
  - app/overrides/hostgroup_ansible_roles_tab.rb
128
128
  - app/overrides/hostgroup_play_roles.rb
129
129
  - app/overrides/report_output.rb
130
+ - app/services/foreman_ansible/ansible_report_importer.rb
130
131
  - app/services/foreman_ansible/ansible_report_scanner.rb
131
132
  - app/services/foreman_ansible/api_roles_importer.rb
132
133
  - app/services/foreman_ansible/fact_importer.rb
@@ -213,6 +214,7 @@ files:
213
214
  - test/unit/lib/foreman_ansible_core/playbook_runner_test.rb
214
215
  - test/unit/lib/foreman_ansible_core/roles_reader_test.rb
215
216
  - test/unit/lib/proxy_api/ansible_test.rb
217
+ - test/unit/services/ansible_report_importer_test.rb
216
218
  - test/unit/services/api_roles_importer_test.rb
217
219
  - test/unit/services/fact_importer_test.rb
218
220
  - test/unit/services/fact_parser_test.rb
@@ -270,6 +272,7 @@ test_files:
270
272
  - test/unit/services/api_roles_importer_test.rb
271
273
  - test/unit/services/structured_fact_importer_test.rb
272
274
  - test/unit/services/fact_sparser_test.rb
275
+ - test/unit/services/ansible_report_importer_test.rb
273
276
  - test/unit/actions/run_proxy_ansible_command_test.rb
274
277
  - test/unit/actions/run_ansible_job_test.rb
275
278
  - test/test_plugin_helper.rb