class ForemanAnsibleCore::PlaybookRunner
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 = {})
click to toggle source
Calls superclass method
# File lib/foreman_ansible_core/playbook_runner.rb, line 12 def initialize(inventory, playbook, options = {}) super @inventory = inventory 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/playbook_runner.rb, line 43 def close super FileUtils.remove_entry(@working_dir) if @tmp_working_dir end
kill()
click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 36 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/playbook_runner.rb, line 23 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}") end end
Private Instance Methods
add_to_known_hosts(host)
click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 108 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/playbook_runner.rb, line 72 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/playbook_runner.rb, line 68 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/playbook_runner.rb, line 96 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/playbook_runner.rb, line 81 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/playbook_runner.rb, line 87 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/playbook_runner.rb, line 60 def inventory_file File.join(@working_dir, 'foreman-inventories', id) end
playbook_file()
click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 64 def playbook_file File.join(@working_dir, "foreman-playbook-#{id}.yml") end
unknown_hosts()
click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 102 def unknown_hosts JSON.parse(@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/playbook_runner.rb, line 50 def write_inventory ensure_directory(File.dirname(inventory_file)) File.write(inventory_file, @inventory) end
write_playbook()
click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 55 def write_playbook ensure_directory(File.dirname(playbook_file)) File.write(playbook_file, @playbook) end