diff --git a/app/models/miq_action.rb b/app/models/miq_action.rb index 46eeb5683e2..11aaec40b6b 100644 --- a/app/models/miq_action.rb +++ b/app/models/miq_action.rb @@ -222,6 +222,12 @@ def action_audit(_action, rec, inputs) :message => "Policy #{msg}: policy: [#{inputs[:policy].description}], event: [#{inputs[:event].description}]") end + def action_run_ansible_playbook(action, rec, _inputs) + service_template = ServiceTemplate.find(action.options[:service_template_id]) + options = { :dialog_hosts => target_hosts(action, rec) } + service_template.provision_request(target_user(rec), options) + end + def action_snmp_trap(action, rec, inputs) # Validate SNMP Version snmp_version = action.options[:snmp_version] @@ -1018,4 +1024,26 @@ def invoke_or_queue( MiqQueue.put(args) end end + + def target_hosts(action, rec) + if action.options[:use_event_target] + ipaddress(rec) + elsif action.options[:use_localhost] + 'localhost' + else + action.options[:hosts] + end + end + + def ipaddress(record) + record.ipaddresses[0] if record.respond_to?(:ipaddresses) + end + + def target_user(record) + record.respond_to?(:tenant_identity) ? record.tenant_identity : default_user + end + + def default_user + User.super_admin.tap { |u| u.current_group = Tenant.root_tenant.default_miq_group } + end end diff --git a/spec/models/miq_action_spec.rb b/spec/models/miq_action_spec.rb index ec6b48aa329..0ea10a15c7b 100644 --- a/spec/models/miq_action_spec.rb +++ b/spec/models/miq_action_spec.rb @@ -387,4 +387,62 @@ def stub_csv(data) action.action_email(action, nil, inputs) end end + + context 'run_ansible_playbook' do + let(:tenant) { FactoryGirl.create(:tenant) } + let(:group) { FactoryGirl.create(:miq_group, :tenant => tenant) } + let(:user) { FactoryGirl.create(:user, :userid => "test", :miq_groups => [group]) } + let(:vm) { FactoryGirl.create(:vm_vmware, :evm_owner => user, :miq_group => group, :hardware => hardware) } + let(:action) { FactoryGirl.create(:miq_action, :name => "run_ansible_playbook", :options => action_options) } + let(:stap) { FactoryGirl.create(:service_template_ansible_playbook) } + let(:ip1) { "1.1.1.94" } + let(:ip2) { "1.1.1.96" } + let(:hardware) do + FactoryGirl.create(:hardware).tap do |h| + h.ipaddresses << ip1 + h.ipaddresses << ip2 + end + end + + shared_examples_for "#workflow check" do + it "run playbook" do + miq_request = instance_double(MiqRequest) + allow(vm).to receive(:tenant_identity).and_return(user) + expect(ServiceTemplate).to receive(:find).with(stap.id).and_return(stap) + expect(stap).to receive(:provision_request).with(user, options).and_return(miq_request) + + action.action_run_ansible_playbook(action, vm, {}) + end + end + + context "use event target" do + let(:action_options) do + { :service_template_id => stap.id, + :use_event_target => true } + end + let(:options) { {:dialog_hosts => ip1 } } + + it_behaves_like "#workflow check" + end + + context "use localhost" do + let(:action_options) do + { :service_template_id => stap.id, + :use_localhost => true } + end + let(:options) { {:dialog_hosts => 'localhost' } } + + it_behaves_like "#workflow check" + end + + context "use hosts" do + let(:action_options) do + { :service_template_id => stap.id, + :hosts => "ip1, ip2" } + end + let(:options) { {:dialog_hosts => 'ip1, ip2' } } + + it_behaves_like "#workflow check" + end + end end