foreman_ansible_core 3.0.0 → 4.0.0

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
- SHA1:
3
- metadata.gz: d7ce49a1cf3b78d977912a6f0887b3c831b34cb2
4
- data.tar.gz: 2ec5f24efd9da511e2670fcd1706b9606e004360
2
+ SHA256:
3
+ metadata.gz: 6427ec31aba5a41a3ffa93168c761218f0aff53f97d08681c9d2cba431b50a9f
4
+ data.tar.gz: cc0a584af7801a24e3d66b679b840c968ab7851f38901abf37e5d26923d1eea9
5
5
  SHA512:
6
- metadata.gz: 3b9b755f323be085fd78f46dfad781c77e57c34629bb45238024bb9125c5d0cbced2010dc41cc51060f9f78486f11946dc18a049d49a397ea6672d126c04547a
7
- data.tar.gz: '02930ac4dc9f53b2e41dbd34225988ca9ff1a9d1693fe6bb77aa8758be610c7df4e07d61b86e2f74862ef8f3653389882514df6cf9123f6036ce00871484e4aa'
6
+ metadata.gz: 1f4db115d787db1dc78d3eaa468b2972514005aa8d068fc0b98d73f7a07e159a5646a9cff4e5f9a19ed6de1ee4043f9f04379f69e82bdceb6a1b9ab5d8257f60
7
+ data.tar.gz: bb6bb793c4ea5d2f43af3ac1c3e545f39b574f32041cd141cd06e4c115e519c3e221449a7af0b4956627acf593792b63376d54dc922bd0ff69ba13ecd76924aa
@@ -1,3 +1,5 @@
1
+ require 'shellwords'
2
+
1
3
  module ForemanAnsibleCore
2
4
  module Runner
3
5
  class AnsibleRunner < ForemanTasksCore::Runner::Parent
@@ -5,9 +7,10 @@ module ForemanAnsibleCore
5
7
 
6
8
  def initialize(input, suspended_action:)
7
9
  super input, :suspended_action => suspended_action
8
- @inventory = rebuild_inventory(input)
10
+ @inventory = rebuild_secrets(rebuild_inventory(input), input)
9
11
  @playbook = input.values.first[:input][:action_input][:script]
10
12
  @root = working_dir
13
+ @verbosity_level = input.values.first[:input][:action_input][:verbosity_level]
11
14
  end
12
15
 
13
16
  def start
@@ -35,13 +38,18 @@ module ForemanAnsibleCore
35
38
  end
36
39
  end
37
40
 
41
+ def close
42
+ super
43
+ FileUtils.remove_entry(@root) if @tmp_working_dir
44
+ end
45
+
38
46
  private
39
47
 
40
48
  def handle_event_file(event_file)
41
49
  logger.debug("[foreman_ansible] - parsing event file #{event_file}")
42
50
  begin
43
51
  event = JSON.parse(File.read(event_file))
44
- if (hostname = event['event_data']['host'])
52
+ if (hostname = event.dig('event_data', 'host'))
45
53
  handle_host_event(hostname, event)
46
54
  else
47
55
  handle_broadcast_data(event)
@@ -57,10 +65,12 @@ module ForemanAnsibleCore
57
65
  log_event("for host: #{hostname.inspect}", event)
58
66
  publish_data_for(hostname, event['stdout'] + "\n", 'stdout') if event['stdout']
59
67
  case event['event']
68
+ when 'runner_on_ok'
69
+ publish_exit_status_for(hostname, 0) if @exit_statuses[hostname].nil?
60
70
  when 'runner_on_unreachable'
61
71
  publish_exit_status_for(hostname, 1)
62
72
  when 'runner_on_failed'
63
- publish_exit_status_for(hostname, 2) if event['ignore_errors'].nil?
73
+ publish_exit_status_for(hostname, 2) if event.dig('event_data', 'ignore_errors').nil?
64
74
  end
65
75
  end
66
76
 
@@ -78,14 +88,14 @@ module ForemanAnsibleCore
78
88
  end
79
89
 
80
90
  def write_inventory
91
+ path = File.join(@root, 'inventory', 'hosts')
92
+ data_path = File.join(@root, 'data')
81
93
  inventory_script = <<~INVENTORY_SCRIPT
82
94
  #!/bin/sh
83
- cat <<-EOS
84
- #{JSON.dump(@inventory)}
85
- EOS
95
+ cat #{::Shellwords.escape data_path}
86
96
  INVENTORY_SCRIPT
87
- path = File.join(@root, 'inventory', 'hosts')
88
97
  File.write(path, inventory_script)
98
+ File.write(data_path, JSON.dump(@inventory))
89
99
  File.chmod(0o0755, path)
90
100
  end
91
101
 
@@ -95,10 +105,19 @@ module ForemanAnsibleCore
95
105
 
96
106
  def start_ansible_runner
97
107
  command = ['ansible-runner', 'run', @root, '-p', 'playbook.yml']
108
+ command << verbosity if verbose?
98
109
  initialize_command(*command)
99
110
  logger.debug("[foreman_ansible] - Running command '#{command.join(' ')}'")
100
111
  end
101
112
 
113
+ def verbosity
114
+ '-' + 'v' * @verbosity_level.to_i
115
+ end
116
+
117
+ def verbose?
118
+ @verbosity_level.to_i.positive?
119
+ end
120
+
102
121
  def prepare_directory_structure
103
122
  inner = %w[inventory project].map { |part| File.join(@root, part) }
104
123
  ([@root] + inner).each do |path|
@@ -118,7 +137,7 @@ module ForemanAnsibleCore
118
137
  def rebuild_inventory(input)
119
138
  action_inputs = input.values.map { |hash| hash[:input][:action_input] }
120
139
  hostnames = action_inputs.map { |hash| hash[:name] }
121
- inventories = action_inputs.map { |hash| JSON.parse(hash[:ansible_inventory]) }
140
+ inventories = action_inputs.map { |hash| hash[:ansible_inventory] }
122
141
  host_vars = inventories.map { |i| i['_meta']['hostvars'] }.reduce(&:merge)
123
142
 
124
143
  { '_meta' => { 'hostvars' => host_vars },
@@ -136,6 +155,21 @@ module ForemanAnsibleCore
136
155
  Dir.mktmpdir(nil, File.expand_path(dir))
137
156
  end
138
157
  end
158
+
159
+ def rebuild_secrets(inventory, input)
160
+ input.each do |host, host_input|
161
+ secrets = host_input['input']['action_input']['secrets']
162
+ per_host = secrets['per-host'][host]
163
+
164
+ new_secrets = {
165
+ 'ansible_password' => inventory['ssh_password'] || per_host['ansible_password'],
166
+ 'ansible_become_password' => inventory['effective_user_password'] || per_host['ansible_become_password']
167
+ }
168
+ inventory['_meta']['hostvars'][host].update(new_secrets)
169
+ end
170
+
171
+ inventory
172
+ end
139
173
  end
140
174
  end
141
175
  end
@@ -14,7 +14,7 @@ module ForemanAnsibleCore
14
14
 
15
15
  def initialize(inventory, playbook, options = {}, suspended_action:)
16
16
  super :suspended_action => suspended_action
17
- @inventory = inventory
17
+ @inventory = rebuild_secrets(inventory, options[:secrets])
18
18
  unknown_hosts.each do |host|
19
19
  add_to_known_hosts(host)
20
20
  end
@@ -52,7 +52,7 @@ module ForemanAnsibleCore
52
52
 
53
53
  def write_inventory
54
54
  ensure_directory(File.dirname(inventory_file))
55
- File.write(inventory_file, @inventory)
55
+ File.write(inventory_file, JSON.dump(@inventory))
56
56
  end
57
57
 
58
58
  def write_playbook
@@ -103,7 +103,7 @@ module ForemanAnsibleCore
103
103
  end
104
104
 
105
105
  def unknown_hosts
106
- JSON.parse(@inventory)['all']['hosts'].select do |host|
106
+ @inventory['all']['hosts'].select do |host|
107
107
  Net::SSH::KnownHosts.search_for(host).empty?
108
108
  end
109
109
  end
@@ -118,6 +118,20 @@ module ForemanAnsibleCore
118
118
  logger.error('[foreman_ansible] - Failed to save host key for '\
119
119
  "#{host}: #{e}")
120
120
  end
121
+
122
+ def rebuild_secrets(inventory, secrets)
123
+ inventory['all']['hosts'].each do |name|
124
+ per_host = secrets['per-host'][name]
125
+
126
+ new_secrets = {
127
+ 'ansible_password' => inventory['ssh_password'] || per_host['ansible_password'],
128
+ 'ansible_become_password' => inventory['effective_user_password'] || per_host['ansible_become_password']
129
+ }
130
+ inventory['_meta']['hostvars'][name].update(new_secrets)
131
+ end
132
+
133
+ inventory
134
+ end
121
135
  end
122
136
  end
123
137
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ForemanAnsibleCore
4
- VERSION = '3.0.0'
4
+ VERSION = '4.0.0'
5
5
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_ansible_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.0.0
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: 2019-04-18 00:00:00.000000000 Z
11
+ date: 2020-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rubocop
14
+ name: foreman_remote_execution_core
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.52'
20
- type: :development
19
+ version: '1.1'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.52'
26
+ version: '1.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: foreman-tasks-core
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.3.2
41
- - !ruby/object:Gem::Dependency
42
- name: foreman_remote_execution_core
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.1'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.1'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: net-ssh
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -106,8 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
92
  - !ruby/object:Gem::Version
107
93
  version: '0'
108
94
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.6.8
95
+ rubygems_version: 3.1.2
111
96
  signing_key:
112
97
  specification_version: 4
113
98
  summary: 'Ansible integration with Foreman (theforeman.org): core bits'