diff --git a/lib/ansible/runner/response_async.rb b/lib/ansible/runner/response_async.rb index 77ee5101f23..ab9c6630900 100644 --- a/lib/ansible/runner/response_async.rb +++ b/lib/ansible/runner/response_async.rb @@ -54,6 +54,25 @@ def dump } end + # Waits for the async process to complete or hit the given timeout + # + # @param timeout [Integer, ActiveSupport::Duration] Number of seconds to wait for the process to complete + # @return [Ansible::Runner::Response] Response object with all details about the Ansible run + def wait(timeout) + result = nil + # Poll once per second until complete + 1.upto(timeout) do |t| + result = response + result ? break : sleep(1) + end + # If the process is still running, then stop it + if result.nil? + stop + result = response + end + result + end + # Creates the Ansible::Runner::ResponseAsync object from hash data # # @param hash [Hash] Dumped Ansible::Runner::ResponseAsync object diff --git a/spec/lib/ansible/runner_execution_spec.rb b/spec/lib/ansible/runner_execution_spec.rb index 620d97aedaf..a68e252834c 100644 --- a/spec/lib/ansible/runner_execution_spec.rb +++ b/spec/lib/ansible/runner_execution_spec.rb @@ -17,7 +17,29 @@ it "runs a hello-world-vault-encrypted playbook" do credential = FactoryBot.create(:embedded_ansible_vault_credential, :password => "vault") playbook = File.join(__dir__, "runner/data/hello_world_vault_encrypted.yml") - response = Ansible::Runner.run(env_vars, extra_vars, playbook, :credentials => [credential.id]) + + response = Ansible::Runner.run(env_vars, extra_vars, playbook, :credentials => [credential.id]) + + expect(response.return_code).to eq(0), "ansible-runner failed with:\n#{response.stderr}" + expect(response.human_stdout).to include('"msg": "Hello World! (NOTE: This message has been encrypted with ansible-vault)"') + end + end + + describe ".run_async" do + it "runs a hello-world playbook" do + response = Ansible::Runner.run_async(env_vars, extra_vars, File.join(__dir__, "runner/data/hello_world.yml")) + response = response.wait(5.seconds) + + expect(response.return_code).to eq(0), "ansible-runner failed with:\n#{response.stderr}" + expect(response.human_stdout).to include('"msg": "Hello, world!"') + end + + it "runs a hello-world-vault-encrypted playbook" do + credential = FactoryBot.create(:embedded_ansible_vault_credential, :password => "vault") + playbook = File.join(__dir__, "runner/data/hello_world_vault_encrypted.yml") + + response = Ansible::Runner.run_async(env_vars, extra_vars, playbook, :credentials => [credential.id]) + response = response.wait(5.seconds) expect(response.return_code).to eq(0), "ansible-runner failed with:\n#{response.stderr}" expect(response.human_stdout).to include('"msg": "Hello World! (NOTE: This message has been encrypted with ansible-vault)"')