Skip to content

Commit

Permalink
Merge pull request #20282 from NickLaMuro/embedded_ansible_fix_ask_be…
Browse files Browse the repository at this point in the history
…come_pass

[Ansible::Runner] Fix --ask-become-method for machine credentials

(cherry picked from commit 7e3a476)

https://bugzilla.redhat.com/show_bug.cgi?id=1858079
  • Loading branch information
carbonin authored and simaishi committed Aug 19, 2020
1 parent 4a04ebb commit b90a4ae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
1 change: 0 additions & 1 deletion lib/ansible/runner.rb
Expand Up @@ -202,7 +202,6 @@ def run_via_cli(hosts, credentials, env_vars, extra_vars, tags: nil, ansible_run
command_line_hash = tags.present? ? {:tags => tags} : {}
if become_enabled
command_line_hash[:become] = nil
command_line_hash[:ask_become_pass] = nil
end
command_line_hash.merge!(cred_command_line)

Expand Down
4 changes: 3 additions & 1 deletion lib/ansible/runner/credential/machine_credential.rb
Expand Up @@ -7,8 +7,10 @@ def self.auth_type

def command_line
{:user => auth.userid}.delete_blanks.merge(become_args).tap do |args|
# Add `--ask-pass` flag to ansible_playbook if we have a password to provide
# Add `--ask-pass` or `--ask-become-pass` flags to ansible_playbook
# if we have a password to provide
args[:ask_pass] = nil if auth.password.present?
args[:ask_become_pass] = nil if auth.become_password.present?
end
end

Expand Down
24 changes: 19 additions & 5 deletions spec/lib/ansible/runner/credential/machine_credential_spec.rb
Expand Up @@ -32,21 +32,35 @@
describe "#command_line" do
it "is correct with all attributes" do
expected = {
:ask_pass => nil,
:become_user => "root",
:become_method => "su",
:user => "manageiq"
:ask_pass => nil,
:ask_become_pass => nil,
:become_user => "root",
:become_method => "su",
:user => "manageiq"
}
expect(cred.command_line).to eq(expected)
end

it "doesn't send :user if userid is not set" do
auth.update!(:userid => nil)

expected = {
:ask_pass => nil,
:ask_become_pass => nil,
:become_user => "root",
:become_method => "su"
}
expect(cred.command_line).to eq(expected)
end

it "doesn't set :ask_become_pass if become_password is blank" do
auth.update!(:become_password => "")

expected = {
:ask_pass => nil,
:become_user => "root",
:become_method => "su"
:become_method => "su",
:user => "manageiq"
}
expect(cred.command_line).to eq(expected)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/ansible/runner_spec.rb
Expand Up @@ -86,7 +86,7 @@
_method, dir, _json, _args = options[:params]
cmdline = File.read(File.join(dir, "env", "cmdline"))

expect(cmdline).to eq("--become --ask-become-pass")
expect(cmdline).to eq("--become")
end.and_return(result)

described_class.run(env_vars, extra_vars, playbook, :become_enabled => true)
Expand Down

0 comments on commit b90a4ae

Please sign in to comment.