class ForemanAnsibleCore::Runner::Playbook
Implements ForemanTasksCore::Runner::Base interface for running Ansible playbooks, used by the Foreman Ansible plugin and Ansible proxy
Attributes
command_in[R]
command_out[R]
command_pid[R]
Public Class Methods
new(inventory, playbook, options = {}, suspended_action:)
click to toggle source
Calls superclass method
# File lib/foreman_ansible_core/runner/playbook.rb, line 14 def initialize(inventory, playbook, options = {}, suspended_action:) super :suspended_action => suspended_action @inventory = rebuild_secrets(inventory, options[:secrets]) unknown_hosts.each do |host| add_to_known_hosts(host) end @playbook = playbook @options = options initialize_dirs end
Public Instance Methods
close()
click to toggle source
Calls superclass method
# File lib/foreman_ansible_core/runner/playbook.rb, line 45 def close super FileUtils.remove_entry(@working_dir) if @tmp_working_dir end
kill()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 38 def kill publish_data('== TASK ABORTED BY USER ==', 'stdout') publish_exit_status(1) ::Process.kill('SIGTERM', @command_pid) close end
start()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 25 def start write_inventory write_playbook command = CommandCreator.new(inventory_file, playbook_file, @options).command logger.debug('[foreman_ansible] - Initializing Ansible Runner') Dir.chdir(@ansible_dir) do initialize_command(*command) logger.debug("[foreman_ansible] - Running command '#{command.join(' ')}'") end end
Private Instance Methods
add_to_known_hosts(host)
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 110 def add_to_known_hosts(host) logger.warn("[foreman_ansible] - Host #{host} not found in known_hosts") Net::SSH::Transport::Session.new(host).host_keys.each do |host_key| Net::SSH::KnownHosts.add(host, host_key) end logger.warn("[foreman_ansible] - Added host key #{host} to known_hosts") rescue StandardError => e logger.error('[foreman_ansible] - Failed to save host key for '\ "#{host}: #{e}") end
ensure_directory(path)
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 74 def ensure_directory(path) if File.exist?(path) raise "#{path} expected to be a directory" unless File.directory?(path) else FileUtils.mkdir_p(path) end path end
events_dir()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 70 def events_dir File.join(@working_dir, 'events', id.to_s) end
initialize_ansible_dir(ansible_dir)
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 98 def initialize_ansible_dir(ansible_dir) raise "Ansible dir #{ansible_dir} does not exist" unless !ansible_dir.nil? && File.exist?(ansible_dir) @ansible_dir = ansible_dir end
initialize_dirs()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 83 def initialize_dirs settings = ForemanAnsibleCore.settings initialize_working_dir(settings[:working_dir]) initialize_ansible_dir(settings[:ansible_dir]) end
initialize_working_dir(working_dir)
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 89 def initialize_working_dir(working_dir) if working_dir.nil? @working_dir = Dir.mktmpdir @tmp_working_dir = true else @working_dir = File.expand_path(working_dir) end end
inventory_file()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 62 def inventory_file File.join(@working_dir, 'foreman-inventories', id) end
playbook_file()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 66 def playbook_file File.join(@working_dir, "foreman-playbook-#{id}.yml") end
rebuild_secrets(inventory, secrets)
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 121 def rebuild_secrets(inventory, secrets) inventory['all']['hosts'].each do |name| per_host = secrets['per-host'][name] new_secrets = { 'ansible_ssh_pass' => inventory['ssh_password'] || per_host['ansible_ssh_pass'], 'ansible_sudo_pass' => inventory['sudo_password'] || per_host['ansible_sudo_pass'] } inventory['_meta']['hostvars'][name].update(new_secrets) end inventory end
unknown_hosts()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 104 def unknown_hosts @inventory['all']['hosts'].select do |host| Net::SSH::KnownHosts.search_for(host).empty? end end
write_inventory()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 52 def write_inventory ensure_directory(File.dirname(inventory_file)) File.write(inventory_file, JSON.dump(@inventory)) end
write_playbook()
click to toggle source
# File lib/foreman_ansible_core/runner/playbook.rb, line 57 def write_playbook ensure_directory(File.dirname(playbook_file)) File.write(playbook_file, @playbook) end