class ForemanAnsibleCore::PlaybookRunner

Implements ForemanTasksCore::Runner::Base interface for running Ansible playbooks

Public Class Methods

new(inventory, playbook) click to toggle source
Calls superclass method
# File lib/foreman_ansible_core/playbook_runner.rb, line 9
def initialize(inventory, playbook)
  super
  @inventory = inventory
  @playbook = playbook
  initialize_dirs
end

Public Instance Methods

close() click to toggle source
Calls superclass method
# File lib/foreman_ansible_core/playbook_runner.rb, line 33
def close
  super
  FileUtils.remove_entry(@working_dir) if @tmp_working_dir
end
command() click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 25
def command
  command = [{ 'JSON_INVENTORY_FILE' => inventory_file }]
  command << 'ansible-playbook'
  command.concat(['-i', json_inventory_script])
  command << playbook_file
  command
end
json_inventory_script() click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 38
def json_inventory_script
  File.expand_path('../../bin/json_inventory.sh', File.dirname(__FILE__))
end
start() click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 16
def start
  write_inventory
  write_playbook
  logger.debug('initalizing runner')
  Dir.chdir(@ansible_dir) do
    initialize_command(*command)
  end
end

Private Instance Methods

ensure_directory(path) click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 66
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 62
def events_dir
  File.join(@working_dir, 'events', id.to_s)
end
initialize_dirs() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/foreman_ansible_core/playbook_runner.rb, line 76
def initialize_dirs
  settings = ForemanAnsibleCore.settings
  @ansible_dir = settings[:ansible_dir]
  unless File.exist?(@ansible_dir)
    raise "Ansible dir #{@ansible_dir} does not exist"
  end
  if ForemanAnsibleCore.settings[:working_dir]
    @working_dir = File.expand_path(settings[:working_dir])
  else
    @working_dir = Dir.mktmpdir
    @tmp_working_dir = true
  end
end
inventory_file() click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 54
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 58
def playbook_file
  File.join(@working_dir, "foreman-playbook-#{id}.yml")
end
write_inventory() click to toggle source
# File lib/foreman_ansible_core/playbook_runner.rb, line 44
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 49
def write_playbook
  ensure_directory(File.dirname(playbook_file))
  File.write(playbook_file, @playbook)
end