Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] improve become_method: runas error handling #23328

Merged
merged 1 commit into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/ansible/playbook/play_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,13 @@ def detect_ksu_prompt(b_data):
elif self.become_method == 'runas':
# become is handled inside the WinRM connection plugin
display.warning("The Windows 'runas' become method is experimental, and may change significantly in future Ansible releases.")

if not self.become_user:
raise AnsibleError(("The 'runas' become method requires a username "
"(specify with the '--become-user' CLI arg, the 'become_user' keyword, or the 'ansible_become_user' variable)"))
if not self.become_pass:
raise AnsibleError(("The 'runas' become method requires a password "
"(specify with the '-K' CLI arg or the 'ansible_become_password' variable)"))
becomecmd = cmd

elif self.become_method == 'doas':
Expand Down
30 changes: 27 additions & 3 deletions lib/ansible/plugins/shell/powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,15 @@

} # end exec_wrapper

Function Dump-Error ($excep) {
$eo = @{failed=$true}

$eo.msg = $excep.Exception.Message
$eo.exception = $excep | Out-String
$host.SetShouldExit(1)

$eo | ConvertTo-Json -Depth 10
}

Function Run($payload) {
# NB: action popping handled inside subprocess wrapper
Expand Down Expand Up @@ -370,14 +379,25 @@
$psi.Username = $username
$psi.Password = $($password | ConvertTo-SecureString -AsPlainText -Force)

[Ansible.Shell.ProcessUtil]::GrantAccessToWindowStationAndDesktop($username)
Try {
[Ansible.Shell.ProcessUtil]::GrantAccessToWindowStationAndDesktop($username)
}
Catch {
$excep = $_
throw "Error granting windowstation/desktop access to '$username' (is the username valid?): $excep"
}

Try {
$proc.Start() | Out-Null # will always return $true for non shell-exec cases
}
Catch {
Write-Output $_.Exception.InnerException
return
$excep = $_
if ($excep.Exception.InnerException -and `
$excep.Exception.InnerException -is [System.ComponentModel.Win32Exception] -and `
$excep.Exception.InnerException.NativeErrorCode -eq 5) {
throw "Become method 'runas' become is not currently supported with the NTLM or Kerberos auth types"
}
throw "Error launching under identity '$username': $excep"
}

$payload_string = $payload | ConvertTo-Json -Depth 99 -Compress
Expand All @@ -404,6 +424,10 @@
Throw "failed, rc was $rc, stderr was $stderr, stdout was $stdout"
}
}
Catch {
$excep = $_
Dump-Error $excep
}
Finally {
Remove-Item $temp -ErrorAction SilentlyContinue
}
Expand Down