foreman_ansible_core 3.0.1 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a2a8033b0b49e4265b56fa56a0376ca02cde2f293a57c618039c60087014381
4
- data.tar.gz: 98166355302fab2ab9d5624699cd6cfe6d880d6161b304b689f6410a1006c0f1
3
+ metadata.gz: 6d8f767c115d90811e6d131c931a3b26759a678b3cebce797735735c99deffb2
4
+ data.tar.gz: 7d51f480e657bcc0aaa6bbcfe8d24af0b18772468d384e09fdcacb4b3c13a705
5
5
  SHA512:
6
- metadata.gz: 4ec94ef02708b8dd9403e630abf2f3f82a65503124dbcef17ee4f3ca9e78e663d9dd8b2ce89ea43d37641ec13c5b81cd2c96d25dbf7bf3b13b8fbad5d189631e
7
- data.tar.gz: 199d21b8a88c256ece9fccc356f580f46d2334fbd87b7310f39043ebdce7560e37d10338ef73b34f4c7af24ed8940b556314faeba74c5206e8fa453a0ce7542c
6
+ metadata.gz: fd37c7c252342f558ae43153e0faa5f44c0a568ef5ecdad2a397ef210dc9708aa16627be056cd0b0c4d262aebce7ca95f5685e1f07dda52586530439bf1439c6
7
+ data.tar.gz: 0d57f0807a3324a7db4494ef9bc81149d3626b8beac521365be7b21146ee2c32a1f2d026d28af658db731621e78333f2eae6cd5dd8badf2b7e518f57aab78d4b
@@ -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
@@ -41,7 +44,7 @@ module ForemanAnsibleCore
41
44
  logger.debug("[foreman_ansible] - parsing event file #{event_file}")
42
45
  begin
43
46
  event = JSON.parse(File.read(event_file))
44
- if (hostname = event['event_data']['host'])
47
+ if (hostname = event.dig('event_data', 'host'))
45
48
  handle_host_event(hostname, event)
46
49
  else
47
50
  handle_broadcast_data(event)
@@ -62,17 +65,23 @@ module ForemanAnsibleCore
62
65
  when 'runner_on_unreachable'
63
66
  publish_exit_status_for(hostname, 1)
64
67
  when 'runner_on_failed'
65
- publish_exit_status_for(hostname, 2) if event['ignore_errors'].nil?
68
+ publish_exit_status_for(hostname, 2) if event.dig('event_data', 'ignore_errors').nil?
66
69
  end
67
70
  end
68
71
 
69
72
  def handle_broadcast_data(event)
70
73
  log_event("broadcast", event)
71
74
  if event['event'] == 'playbook_on_stats'
75
+ failures = event.dig('event_data', 'failures') || {}
72
76
  header, *rows = event['stdout'].strip.lines.map(&:chomp)
73
77
  @outputs.keys.select { |key| key.is_a? String }.each do |host|
74
78
  line = rows.find { |row| row =~ /#{host}/ }
75
79
  publish_data_for(host, [header, line].join("\n"), 'stdout')
80
+
81
+ # If the task has been rescued, it won't consider a failure
82
+ if @exit_statuses[host].to_i != 0 && failures[host].to_i <= 0
83
+ publish_exit_status_for(host, 0)
84
+ end
76
85
  end
77
86
  else
78
87
  broadcast_data(event['stdout'] + "\n", 'stdout')
@@ -80,14 +89,14 @@ module ForemanAnsibleCore
80
89
  end
81
90
 
82
91
  def write_inventory
92
+ path = File.join(@root, 'inventory', 'hosts')
93
+ data_path = File.join(@root, 'data')
83
94
  inventory_script = <<~INVENTORY_SCRIPT
84
95
  #!/bin/sh
85
- cat <<-EOS
86
- #{JSON.dump(@inventory)}
87
- EOS
96
+ cat #{::Shellwords.escape data_path}
88
97
  INVENTORY_SCRIPT
89
- path = File.join(@root, 'inventory', 'hosts')
90
98
  File.write(path, inventory_script)
99
+ File.write(data_path, JSON.dump(@inventory))
91
100
  File.chmod(0o0755, path)
92
101
  end
93
102
 
@@ -97,10 +106,19 @@ module ForemanAnsibleCore
97
106
 
98
107
  def start_ansible_runner
99
108
  command = ['ansible-runner', 'run', @root, '-p', 'playbook.yml']
109
+ command << verbosity if verbose?
100
110
  initialize_command(*command)
101
111
  logger.debug("[foreman_ansible] - Running command '#{command.join(' ')}'")
102
112
  end
103
113
 
114
+ def verbosity
115
+ '-' + 'v' * @verbosity_level.to_i
116
+ end
117
+
118
+ def verbose?
119
+ @verbosity_level.to_i.positive?
120
+ end
121
+
104
122
  def prepare_directory_structure
105
123
  inner = %w[inventory project].map { |part| File.join(@root, part) }
106
124
  ([@root] + inner).each do |path|
@@ -120,7 +138,7 @@ module ForemanAnsibleCore
120
138
  def rebuild_inventory(input)
121
139
  action_inputs = input.values.map { |hash| hash[:input][:action_input] }
122
140
  hostnames = action_inputs.map { |hash| hash[:name] }
123
- inventories = action_inputs.map { |hash| JSON.parse(hash[:ansible_inventory]) }
141
+ inventories = action_inputs.map { |hash| hash[:ansible_inventory] }
124
142
  host_vars = inventories.map { |i| i['_meta']['hostvars'] }.reduce(&:merge)
125
143
 
126
144
  { '_meta' => { 'hostvars' => host_vars },
@@ -138,6 +156,21 @@ module ForemanAnsibleCore
138
156
  Dir.mktmpdir(nil, File.expand_path(dir))
139
157
  end
140
158
  end
159
+
160
+ def rebuild_secrets(inventory, input)
161
+ input.each do |host, host_input|
162
+ secrets = host_input['input']['action_input']['secrets']
163
+ per_host = secrets['per-host'][host]
164
+
165
+ new_secrets = {
166
+ 'ansible_ssh_pass' => inventory['ssh_password'] || per_host['ansible_ssh_pass'],
167
+ 'ansible_sudo_pass' => inventory['sudo_password'] || per_host['ansible_sudo_pass']
168
+ }
169
+ inventory['_meta']['hostvars'][host].update(new_secrets)
170
+ end
171
+
172
+ inventory
173
+ end
141
174
  end
142
175
  end
143
176
  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_ssh_pass' => inventory['ssh_password'] || per_host['ansible_ssh_pass'],
128
+ 'ansible_sudo_pass' => inventory['sudo_password'] || per_host['ansible_sudo_pass']
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.1'
4
+ VERSION = '3.0.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_ansible_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Lobato Garcia
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-06 00:00:00.000000000 Z
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -91,7 +91,7 @@ homepage: https://github.com/theforeman/foreman_ansible
91
91
  licenses:
92
92
  - GPL-3.0
93
93
  metadata: {}
94
- post_install_message:
94
+ post_install_message:
95
95
  rdoc_options: []
96
96
  require_paths:
97
97
  - lib
@@ -106,8 +106,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  - !ruby/object:Gem::Version
107
107
  version: '0'
108
108
  requirements: []
109
- rubygems_version: 3.0.3
110
- signing_key:
109
+ rubygems_version: 3.1.2
110
+ signing_key:
111
111
  specification_version: 4
112
112
  summary: 'Ansible integration with Foreman (theforeman.org): core bits'
113
113
  test_files: []