-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19383 from lfu/ansible_runner_cleanup
Cleanup after Ansible runner. (cherry picked from commit 1e1ef97) https://bugzilla.redhat.com/show_bug.cgi?id=1784103
- Loading branch information
1 parent
17a9b56
commit d1148e8
Showing
12 changed files
with
201 additions
and
1,082 deletions.
There are no files selected for viewing
72 changes: 0 additions & 72 deletions
72
app/models/manageiq/providers/embedded_ansible/automation_manager/configuration_script.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 33 additions & 33 deletions
66
app/models/manageiq/providers/embedded_ansible/automation_manager/playbook.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
class ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Playbook < ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptPayload | ||
has_many :jobs, :class_name => 'OrchestrationStack', :foreign_key => :configuration_script_base_id | ||
|
||
def run(options, userid = nil) | ||
options[:playbook_id] = id | ||
options[:userid] = userid || 'system' | ||
options[:name] = "Playbook: #{name}" | ||
miq_job = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::PlaybookRunner.create_job(options) | ||
miq_job.signal(:start) | ||
miq_job.miq_task.id | ||
end | ||
|
||
# return provider raw object | ||
def raw_create_job_template(options) | ||
job_template_klass = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScript | ||
jt_options = build_parameter_list(options) | ||
_log.info("Creating job template with options:") | ||
$log.log_hashes(jt_options) | ||
job_template_klass.raw_create_in_provider(manager, jt_options) | ||
end | ||
DEFAULT_EXECUTION_TTL = 100.minutes # automate state machine aborts after 100 retries at a minute interval | ||
|
||
def self.display_name(number = 1) | ||
n_('Playbook (Embedded Ansible)', 'Playbooks (Embedded Ansible)', number) | ||
end | ||
|
||
private | ||
def run(vars = {}) | ||
workflow = ManageIQ::Providers::AnsiblePlaybookWorkflow | ||
|
||
def build_parameter_list(options) | ||
params = { | ||
:name => options[:template_name] || "#{name}_#{SecureRandom.uuid}", | ||
:description => options[:description] || "Created on #{Time.zone.now}", | ||
:project => configuration_script_source.manager_ref, | ||
:playbook => name, | ||
:become_enabled => options[:become_enabled].present?, | ||
:verbosity => options[:verbosity].presence || 0, | ||
:limit => options[:limit], | ||
:inventory => options[:inventory] || manager.provider.default_inventory, | ||
:extra_vars => options[:extra_vars].try(:to_json) | ||
extra_vars = build_extra_vars(vars[:extra_vars]) | ||
|
||
playbook_vars = { | ||
:configuration_script_source_id => configuration_script_source_id, | ||
:playbook_relative_path => name | ||
} | ||
|
||
%i(credential vault_credential cloud_credential network_credential).each do |credential| | ||
cred_sym = "#{credential}_id".to_sym | ||
params[credential] = Authentication.find(options[cred_sym]).native_ref if options[cred_sym].present? | ||
credentials = collect_credentials(vars) | ||
|
||
kwargs = {:become_enabled => vars[:become_enabled]} | ||
kwargs[:timeout] = vars[:execution_ttl].present? ? vars[:execution_ttl].to_i.minutes : DEFAULT_EXECUTION_TTL | ||
kwargs[:verbosity] = vars[:verbosity].to_i if vars[:verbosity].present? | ||
|
||
workflow.create_job({}, extra_vars, playbook_vars, vars[:hosts], credentials, kwargs).tap do |job| | ||
job.signal(:start) | ||
end | ||
end | ||
|
||
private | ||
|
||
def build_extra_vars(external = {}) | ||
(external || {}).each_with_object({}) do |(k, v), hash| | ||
match_data = v.kind_of?(String) && /password::/.match(v) | ||
hash[k] = match_data ? ManageIQ::Password.decrypt(v.gsub(/password::/, '')) : v | ||
end | ||
end | ||
|
||
params.compact | ||
def collect_credentials(options) | ||
options.values_at( | ||
:credential, | ||
:cloud_credential, | ||
:network_credential, | ||
:vault_credential | ||
).compact | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module AnsiblePlaybookMixin | ||
extend ActiveSupport::Concern | ||
|
||
CONFIG_OPTIONS_WHITELIST = %i[ | ||
become_enabled | ||
cloud_credential_id | ||
credential_id | ||
execution_ttl | ||
extra_vars | ||
hosts | ||
network_credential_id | ||
vault_credential_id | ||
verbosity | ||
].freeze | ||
|
||
def translate_credentials!(options) | ||
%i[credential vault_credential network_credential cloud_credential].each do |cred| | ||
cred_sym = "#{cred}_id".to_sym | ||
credential_id = options.delete(cred_sym) | ||
options[cred] = Authentication.find(credential_id).native_ref if credential_id.present? | ||
end | ||
end | ||
|
||
def use_default_inventory?(hosts) | ||
hosts.blank? || hosts == 'localhost' | ||
end | ||
|
||
def hosts_array(hosts_string) | ||
return ["localhost"] if use_default_inventory?(hosts_string) | ||
|
||
hosts_string.split(',').map(&:strip).delete_blanks | ||
end | ||
|
||
def playbook_log_stdout(log_option, job) | ||
return unless %(on_error always).include?(log_option) | ||
return if log_option == 'on_error' && job.raw_status.succeeded? | ||
|
||
$log.info("Stdout from ansible job #{job.name}: #{job.raw_stdout('txt_download')}") | ||
rescue StandardError => err | ||
$log.error("Failed to get stdout from ansible job #{job.name}") | ||
$log.log_backtrace(err) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.