Skip to content

Commit

Permalink
Merge pull request #23091 from Fryguy/add_more_ansible_runner_specs
Browse files Browse the repository at this point in the history
Add more ansible runner specs
  • Loading branch information
agrare authored Jul 29, 2024
2 parents 650ac72 + b6d0c4e commit 80ff67c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/ansible/runner/response_async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/ansible/runner/data/hello_world_vault_encrypted.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Note: The password for `encrypted_msg` is just 'vault'
#
# This playbook is just used for testing vault integration, so when provided
# with the correct password the message displayed should be:
#
# "Hello World! (NOTE: This message has been encrypted with ansible-vault)"
#
# This var was generated by running the following:
#
# $ ansible-vault encrypt_string --ask-vault-pass --stdin-name "encrypted_msg"
# New Vault password:
# Confirm New Vault password:
# Reading plaintext input from stdin. (ctrl-d to end input)
# Hello World!
# encrypted_msg: !vault |
# $ANSIBLE_VAULT;1.1;AES256
# 66383635373066393337333631383930366166656134653935663164636636623239333861643936
# 3664613065666439303135323331616666383030383839310a303461623264646233623037313363
# 63626562616166353466366232363562353461366162396262363461666439386165663565643832
# 3239396633396466630a616463393237303338633562656664653433633437383161353933303737
# 3764
# Encryption successful
#
# (NOTE: When running the above, Ctrl-D was required twice for `ansible-vault`
# to respond. Most likely due to the '!' character in the base string.)
#
- name: Hello World Sample (Vault Encrypted)
hosts: localhost
vars:
encrypted_msg: !vault |
$ANSIBLE_VAULT;1.1;AES256
66383635373066393337333631383930366166656134653935663164636636623239333861643936
3664613065666439303135323331616666383030383839310a303461623264646233623037313363
63626562616166353466366232363562353461366162396262363461666439386165663565643832
3239396633396466630a616463393237303338633562656664653433633437383161353933303737
3764
tasks:
- name: Hello Message
debug:
msg: "{{encrypted_msg}} (NOTE: This message has been encrypted with ansible-vault)"
31 changes: 31 additions & 0 deletions spec/lib/ansible/runner_execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,36 @@
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(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)"')
end
end
end

0 comments on commit 80ff67c

Please sign in to comment.