-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #13812 - Remote execution provider
Ansible can be used as a provider for remote execution. The job templates can be Ansible playbooks that use host parameters, properties and ERB. Job templates, however, should have a 'hosts' section that just contains <%= @host.name %>, because a new inventory is generated per host with all the required variables. In any case, the way it should work is: Foreman sends request to proxy - including the 'hosts' it's supposed to run on. Proxy generates an inventory with the hosts and variables required Proxy runs ansible and reports to Foreman The reason why ansible_foreman_inventory cannot be used in this case is because it's less flexible than the 'search' field of REX, where one can use the 'scoped_search' syntax to figure out what hosts to run the playbook on. If we used ansible_foreman_inventory for that, we would be forced to run our playbooks on a set of hosts, hostgroups, organiztions or locations.
- Loading branch information
Showing
15 changed files
with
155 additions
and
11 deletions.
There are no files selected for viewing
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
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,24 @@ | ||
if defined? ForemanRemoteExecution | ||
module ForemanAnsible | ||
# Provider for RemoteExecution that allows to run Ansible playbooks. | ||
# Read the source of other RemoteExecution providers for more. | ||
class AnsibleProvider < RemoteExecutionProvider | ||
class << self | ||
def humanized_name | ||
'Ansible' | ||
end | ||
|
||
def host_setting(host, setting) | ||
host.params[setting.to_s] || Setting[setting] | ||
end | ||
|
||
def proxy_command_options(template_invocation, host) | ||
super(template_invocation, host).merge( | ||
'ansible_inventory' => | ||
::ForemanAnsible::InventoryCreator.new([host]).to_hash.to_json | ||
) | ||
end | ||
end | ||
end | ||
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
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,11 @@ | ||
if defined? ForemanRemoteExecution | ||
module ForemanAnsible | ||
# Dependencies related with the remote execution plugin | ||
class Engine < ::Rails::Engine | ||
config.to_prepare do | ||
RemoteExecutionProvider.register(:Ansible, | ||
ForemanAnsible::AnsibleProvider) | ||
end | ||
end | ||
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
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
40 changes: 40 additions & 0 deletions
40
lib/foreman_ansible_core/remote_execution_core/ansible_runner.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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
if defined? ::ForemanRemoteExecutionCore | ||
module ForemanAnsibleCore | ||
module RemoteExecutionCore | ||
class AnsibleRunner < ::ForemanTasksCore::Runner::CommandRunner | ||
DEFAULT_REFRESH_INTERVAL = 1 | ||
|
||
def initialize(options) | ||
super(options) | ||
@playbook_runner = ForemanAnsibleCore::PlaybookRunner.new( | ||
options['ansible_inventory'], | ||
options['script'], | ||
options | ||
) | ||
end | ||
|
||
def start | ||
@playbook_runner.start | ||
rescue => e | ||
logger.error("error while initalizing command #{e.class} #{e.message}:\n #{e.backtrace.join("\n")}") | ||
publish_exception('Error initializing command', e) | ||
end | ||
|
||
def fill_continuous_output(continuous_output) | ||
delegated_output.fetch('result', []).each do |raw_output| | ||
continuous_output.add_raw_output(raw_output) | ||
end | ||
rescue StandardError => e | ||
continuous_output.add_exception(_('Error loading data from proxy'), e) | ||
end | ||
|
||
def refresh | ||
@command_out = @playbook_runner.command_out | ||
@command_in = @playbook_runner.command_in | ||
@command_pid = @playbook_runner.command_pid | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
lib/foreman_ansible_core/remote_execution_core/settings_override.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module ForemanAnsibleCore | ||
module RemoteExecutionCore | ||
module SettingsOverride | ||
def initiate_runner | ||
return super unless input['ansible_inventory'] | ||
additional_options = { | ||
:step_id => run_step_id, | ||
:uuid => execution_plan_id | ||
} | ||
::ForemanAnsibleCore::RemoteExecutionCore::AnsibleRunner.new( | ||
input.merge(additional_options) | ||
) | ||
end | ||
end | ||
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
Empty file.
Empty file.
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,50 @@ | ||
module ForemanAnsible | ||
class AnsibleTemplateRendererTest < ActiveSupport::TestCase | ||
test 'builds an inventory using the host name' do | ||
host = FactoryGirl.build_stubbed(:host) | ||
renderer = AnsibleTemplateRenderer.new(nil, host) | ||
renderer.expects(:target_hosts).returns([host]).twice | ||
assert_equal renderer.inventory, "#{host.name} \n" | ||
end | ||
|
||
test 'builds an inventory using the host parameters' do | ||
host = FactoryGirl.build_stubbed(:host) | ||
stubbed_parameter = FactoryGirl.build_stubbed(:host_parameter) | ||
host.expects(:parameters).returns([stubbed_parameter]).at_least_once | ||
renderer = AnsibleTemplateRenderer.new(nil, host) | ||
renderer.expects(:target_hosts).returns([host]).at_least_once | ||
assert_equal(renderer.inventory, | ||
"#{host.name} "\ | ||
"#{host.parameters.first.name}="\ | ||
"#{host.parameters.first.value}\n") | ||
end | ||
|
||
context 'with hostgroup' do | ||
setup do | ||
@host = FactoryGirl.build_stubbed(:host, :with_hostgroup) | ||
end | ||
|
||
test 'builds an inventory using the host hostgroup' do | ||
renderer = AnsibleTemplateRenderer.new(nil, @host) | ||
renderer.expects(:target_hosts).returns([@host]).times(3) | ||
assert_equal(renderer.inventory, | ||
"#{@host.name} \n"\ | ||
"[#{@host.hostgroup.title}]\n"\ | ||
"#{@host.name}\n"\ | ||
"[#{@host.hostgroup.title}:vars]\n\n") | ||
end | ||
|
||
test 'builds an inventory using the host hostgroup params' do | ||
@host.hostgroup.expects(:parameters).returns('a' => 'b').at_least_once | ||
renderer = AnsibleTemplateRenderer.new(nil, @host) | ||
renderer.expects(:target_hosts).returns([@host]).at_least_once | ||
assert_equal(renderer.inventory, | ||
"#{@host.name} \n"\ | ||
"[#{@host.hostgroup.title}]\n"\ | ||
"#{@host.name}\n"\ | ||
"[#{@host.hostgroup.title}:vars]\n"\ | ||
"a=b\n\n") | ||
end | ||
end | ||
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
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
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