Repository: ansible/ansible

Issue:
ansible hangs waiting for/to send doas password
##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
TASK [setup]

##### ANSIBLE VERSION
jforman@stonecrab:/tank/git/ansible-configs$ /tmp/ansible/bin/ansible --version
ansible 2.2.0.0
  config file = /tank/git/ansible-configs/ansible.cfg
  configured module search path = Default w/o overrides

##### OS / ENVIRONMENT
Origin OS: Ubuntu Desktop 16.10
Destination OS: OpenBSD 6.0

##### SUMMARY
Suddenly (I can't remember when I last had issues with this) any Ansible operation on the OpenBSD machine hangs at the 'task setup' state.

##### STEPS TO REPRODUCE
Works as expected:
/tmp/ansible/bin/ansible -vvvv --become-method doas -i hosts -k $openbsdhost -m setup

Hangs:
/tmp/ansible/bin/ansible --ask-become-pass -b -vvvv --become-method doas -i hosts -k $openbsdhost -m setup
Using /tank/git/ansible-configs/ansible.cfg as config file
SSH password: <entered password>
DOAS password[defaults to SSH password]:  <enter>
.......
<$openbsdhost> SSH: EXEC sshpass -d12 ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/home/jforman/.ssh/id_rsa"' -o User=ansibler -o ConnectTimeout=10 -o ControlPath=/home/jforman/.ansible/cp/%h-%p-%r -tt $openbsdhost '/bin/sh -c '"'"'doas  -u root  echo BECOME-SUCCESS-jehbbhnejigevhrwtomnvrghnfnumhpj && doas  -u root  env ANSIBLE=true /usr/local/bin/python /home/ansibler/.ansible/tmp/ansible-tmp-1480716111.08-113929165791759/setup.py; rm -rf "/home/ansibler/.ansible/tmp/ansible-tmp-1480716111.08-113929165791759/" > /dev/null 2>&1 && sleep 0'"'"''

##### EXPECTED RESULTS
Expected setup json output.

##### ACTUAL RESULTS
The second command execution above hangs




Discussion:
@jforman, we need more information to resolve this issue. For that reason, we put this Issue into the 'needs_info' state.

Here are the required items we could not find in your description:
- issue type
 
When you have filled in the missing data, we will notify the module maintainer for further action.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: issue_needs_info --->
##### ISSUE TYPE
 - Bug Report

The magic of ANSIBLE_DEBUG=true. 

Output: https://gist.github.com/jforman/39025388d9f0a1260104669830cdc722

The last bit of output:
  9883 1480728150.72411: become_success: (source=stdout, state=awaiting_escalation): 'BECOME-SUCCESS-sdqpvwtelfvmtjmgycktemaudxixafsk'
  9883 1480728150.72418: Escalation succeeded
  9883 1480728150.73395: stdout chunk (state=3):
doas (ansibler@openbsdhost) password: <<<


I've run into the same issue with the same debug output. The only way out was switching to using sudo instead of doas.
Could I get an update on fixing this? It's been over a year and I'm still seeing the same hanging behavior on version 2.4:

jforman@desktop1:/tank/git/ansible-configs$ ansible-playbook --version
 12656 1513170264.63183: starting run
ansible-playbook 2.4.1.0
  config file = /tank/git/ansible-configs/ansible.cfg
  configured module search path = [u'/home/jforman/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118]


jforman@desktop1:/tank/git/ansible-configs$ ansible-playbook --become-method=doas -l 'openbsdmachine' site.yml 
......
 12622 1513170211.78632: become_success: (source=stdout, state=awaiting_escalation): 'BECOME-SUCCESS-ycrbjzpsreqoxxyxggardmdhjkfhocny'
 12622 1513170211.78638: Escalation succeeded
 12622 1513170211.79651: stdout chunk (state=3):
doas (ansibler@openbsdmachine) password: <<<

(hang)

I'm seeing the issue as well with Ansible version 2.4.2.0.

Any updates or hints what could be the cause of this?
The merged PR #13451 fixes the issue, yet it seems the changes only made it to the [stable-2.0](https://github.com/ansible/ansible/tree/stable-2.0) branch, but not into other branches relevant for the current releases.

@bcoca as the author and merger of PR #13451, would mind having another look?
I'd also be interested in an explanation of what the issue is here :)

@jforman, @tolbrino, FWIW I was able to hotfix the issue by changing the appropriate doas-line in `playbook/play_context.py` in my Ansible installation to [`becomecmd = '%s %s %s -c %s' % (exe, flags, executable, success_cmd)`](https://github.com/ansible/ansible/pull/13451/files#diff-efa3a13dd7cbec87c75b359049e1c7d3R484)
@afh The need to hotpatch my local checkout of Ansible is not a solution I'm willing to accept. I'm hoping @bcoca can provide some insight here into what it would take to get this into more recent (and future) ansible versions.
@jforman You are right, a hotfix is unacceptable and I find that having the changes in one of the next releases is important. For me the hotfix meant being able to finish writing my orchestration, tough :)

Resolving patch:
diff --git a/lib/ansible/playbook/play_context.py b/lib/ansible/playbook/play_context.py
--- a/lib/ansible/playbook/play_context.py
+++ b/lib/ansible/playbook/play_context.py
@@ -544,7 +544,7 @@ def detect_ksu_prompt(b_data):
                     flags += ' -u %s ' % self.become_user
 
                 # FIXME: make shell independent
-                becomecmd = '%s %s echo %s && %s %s env ANSIBLE=true %s' % (exe, flags, success_key, exe, flags, cmd)
+                becomecmd = '%s %s %s -c %s' % (exe, flags, executable, success_cmd)
 
             elif self.become_method == 'dzdo':
 


Test patch:
diff --git a/test/units/playbook/test_play_context.py b/test/units/playbook/test_play_context.py
--- a/test/units/playbook/test_play_context.py
+++ b/test/units/playbook/test_play_context.py
@@ -153,8 +153,7 @@ def test_play_context_make_become_cmd(parser):
 
     play_context.become_method = 'doas'
     cmd = play_context.make_become_cmd(cmd=default_cmd, executable="/bin/bash")
-    assert (cmd == """%s %s echo %s && %s %s env ANSIBLE=true %s""" % (doas_exe, doas_flags, play_context.
-                                                                       success_key, doas_exe, doas_flags, default_cmd))
+    assert (cmd == """%s %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, default_exe, play_context.success_key, default_cmd))
 
     play_context.become_method = 'ksu'
     cmd = play_context.make_become_cmd(cmd=default_cmd, executable="/bin/bash")