<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ ansible -m raw localhost -a 'echo connection' --sudo -K -vvvv -c local          
Using /etc/ansible/ansible.cfg as config file
SUDO password: 
Loaded callback minimal of type stdout, v2.0
ESTABLISH LOCAL CONNECTION FOR USER: badger
127.0.0.1 EXEC /bin/sh -c 'sudo -H -S  -p &quot;[sudo via ansible, key=dsvxgavwyxalsjrucewuzwdyxmpasolo] password: &quot; -u root /bin/sh -c '&quot;'&quot;'echo BECOME-SUCCESS-dsvxgavwyxalsjrucewuzwdyxmpasolo; echo connection'&quot;'&quot;''
localhost | FAILED | rc=0 &gt;&gt;
ERROR! privilege output closed while waiting for password prompt:
BECOME-SUCCESS-dsvxgavwyxalsjrucewuzwdyxmpasolo
connection


[pts/36@roan /srv/ansible/ansible-friday]$ ansible -m raw localhost -a '/bin/echo connection' --sudo -K -vvvv -c local
SUDO password: 
localhost | SUCCESS | rc=0 &gt;&gt;
connection


[pts/36@roan /srv/ansible/ansible-friday]$ ansible -m raw -i 'rhel7-test,' '*' -a 'echo connection' --sudo -K
SUDO password: 
rhel7-test | SUCCESS | rc=0 &gt;&gt;

connection


[pts/36@roan /srv/ansible/ansible-friday]$ ansible -m raw -i 'rhel7-test,' '*' -a '/bin/echo connection' --sudo -K
SUDO password: 
rhel7-test | SUCCESS | rc=0 &gt;&gt;

connection


[pts/36@roan /srv/ansible/ansible-friday]$                                                      *[devel]  (20:03:38)"><pre class="notranslate"><code class="notranslate">$ ansible -m raw localhost -a 'echo connection' --sudo -K -vvvv -c local          
Using /etc/ansible/ansible.cfg as config file
SUDO password: 
Loaded callback minimal of type stdout, v2.0
ESTABLISH LOCAL CONNECTION FOR USER: badger
127.0.0.1 EXEC /bin/sh -c 'sudo -H -S  -p "[sudo via ansible, key=dsvxgavwyxalsjrucewuzwdyxmpasolo] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-dsvxgavwyxalsjrucewuzwdyxmpasolo; echo connection'"'"''
localhost | FAILED | rc=0 &gt;&gt;
ERROR! privilege output closed while waiting for password prompt:
BECOME-SUCCESS-dsvxgavwyxalsjrucewuzwdyxmpasolo
connection


[pts/36@roan /srv/ansible/ansible-friday]$ ansible -m raw localhost -a '/bin/echo connection' --sudo -K -vvvv -c local
SUDO password: 
localhost | SUCCESS | rc=0 &gt;&gt;
connection


[pts/36@roan /srv/ansible/ansible-friday]$ ansible -m raw -i 'rhel7-test,' '*' -a 'echo connection' --sudo -K
SUDO password: 
rhel7-test | SUCCESS | rc=0 &gt;&gt;

connection


[pts/36@roan /srv/ansible/ansible-friday]$ ansible -m raw -i 'rhel7-test,' '*' -a '/bin/echo connection' --sudo -K
SUDO password: 
rhel7-test | SUCCESS | rc=0 &gt;&gt;

connection


[pts/36@roan /srv/ansible/ansible-friday]$                                                      *[devel]  (20:03:38)
</code></pre></div>
<p dir="auto">The first run is the problem run.  The rest work.  Could it be a problem with the echo shell builtin (since the version with full path to echo works?)  I also notice that the full path to echo but still running on localhost has one less newline in its output (between the header and "connection") than the output when run over ssh.  Maybe that's a clue?</p>
<p dir="auto">This does work in 1.9.x:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ ansible -m raw localhost -a 'echo connection' --sudo -K
SUDO password: 
localhost | success | rc=0 &gt;&gt;
connection

$"><pre class="notranslate"><code class="notranslate">$ ansible -m raw localhost -a 'echo connection' --sudo -K
SUDO password: 
localhost | success | rc=0 &gt;&gt;
connection

$
</code></pre></div>
<p dir="auto"><strong>EDIT:</strong> pipelining must be on</p>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">plugins/connection/ssh</p>
<h5 dir="auto">ANSIBLE VERSION</h5>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.3.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]"><pre class="notranslate"><code class="notranslate">ansible 2.3.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
</code></pre></div>
<p dir="auto">But I believe the bug also exists in v2.4</p>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">[ssh_connection]<br>
control_path = %(directory)s/%%h-%%p-%%r</p>

<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">CentOS 7 control &amp; target machines</p>

<h5 dir="auto">SUMMARY</h5>

<p dir="auto">This is another incarnation of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="255993076" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/29095" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/29095/hovercard" href="https://github.com/ansible/ansible/issues/29095">#29095</a> but in case of ssh retry due to ControlPersist broken pipe (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="165880251" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/16731" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/16731/hovercard" href="https://github.com/ansible/ansible/issues/16731">#16731</a> fix).</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">I'm sorry, but it is hard for me to specify reproduction steps.<br>
The issue has occurred in various different tasks of a long playbook.<br>
It was executed on a fairly slow VM control machine and was provisioning over 30 remote machines.</p>



<h5 dir="auto">EXPECTED RESULTS</h5>

<p dir="auto">sshpass pipe should be recreated in case of retry due to ControlPersist broken pipe</p>
<h5 dir="auto">ACTUAL RESULTS</h5>


<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(...)
&lt;192.168.0.22&gt; ESTABLISH SSH CONNECTION FOR USER: XXX
(...)
&lt;192.168.0.22&gt; SSH: ansible.cfg set ssh_args: (-C)(-o)(ControlMaster=auto)(-o)(ControlPersist=60s)
&lt;192.168.0.22&gt; SSH: ANSIBLE_HOST_KEY_CHECKING/host_key_checking disabled: (-o)(StrictHostKeyChecking=no)
&lt;192.168.0.22&gt; SSH: ANSIBLE_REMOTE_PORT/remote_port/ansible_port set: (-o)(Port=22)
&lt;192.168.0.22&gt; SSH: ANSIBLE_REMOTE_USER/remote_user/ansible_user/user/-u set: (-o)(User=XXX)
&lt;192.168.0.22&gt; SSH: ANSIBLE_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)
&lt;192.168.0.22&gt; SSH: PlayContext set ssh_common_args: (-o)(StrictHostKeyChecking=no)
&lt;192.168.0.22&gt; SSH: PlayContext set ssh_extra_args: (-o)(UserKnownHostsFile=/dev/null)
&lt;192.168.0.22&gt; SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/XXX/.ansible/cp/%h-%p-%r)
&lt;192.168.0.22&gt; SSH: EXEC sshpass -d14 ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=22 -o User=XXX -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ControlPath=/home/XXX/.ansible/cp/%h-%p-%r 192.168.0.22 '/bin/sh -c '&quot;'&quot;'echo ~ &amp;&amp; sleep 0'&quot;'&quot;''
(...)
RETRYING BECAUSE OF CONTROLPERSIST BROKEN PIPE
&lt;192.168.0.22&gt; SSH: EXEC sshpass -d14 ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=22 -o User=XXX -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ControlPath=/home/XXX/.ansible/cp/%h-%p-%r 192.168.0.22 '/bin/sh -c '&quot;'&quot;'echo ~ &amp;&amp; sleep 0'&quot;'&quot;''
(...)
close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor
(...)
The full traceback is:
Traceback (most recent call last):
  File &quot;/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py&quot;, line 125, in run
    res = self._execute()
  File &quot;/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py&quot;, line 522, in _execute
    result = self._handler.run(task_vars=variables)
  File &quot;/usr/lib/python2.7/site-packages/ansible/plugins/action/package.py&quot;, line 69, in run
    result.update(self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async))
  File &quot;/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py&quot;, line 637, in _execute_module
    tmp = self._make_tmp_path()
  File &quot;/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py&quot;, line 248, in _make_tmp_path
    tmpdir =  self._remote_expand_user(C.DEFAULT_REMOTE_TMP, sudoable=False)
  File &quot;/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py&quot;, line 545, in _remote_expand_user
    data = self._low_level_execute_command(cmd, sudoable=False)
  File &quot;/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py&quot;, line 883, in _low_level_execute_command
    rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
  File &quot;/usr/lib/python2.7/site-packages/ansible/plugins/connection/ssh.py&quot;, line 788, in exec_command
    (returncode, stdout, stderr) = self._run(cmd, in_data, sudoable=sudoable)
  File &quot;/usr/lib/python2.7/site-packages/ansible/plugins/connection/ssh.py&quot;, line 85, in wrapped
    return_tuple = func(self, *args, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/ansible/plugins/connection/ssh.py&quot;, line 461, in _run
    os.write(self.sshpass_pipe[1], to_bytes(self._play_context.password) + b'
')
OSError: [Errno 9] Bad file descriptor
fatal: [192.168.0.22]: FAILED! =&gt; {
    &quot;failed&quot;: true, 
    &quot;msg&quot;: &quot;Unexpected failure during module execution.&quot;, 
    &quot;stdout&quot;: &quot;&quot;
}"><pre class="notranslate"><code class="notranslate">(...)
&lt;192.168.0.22&gt; ESTABLISH SSH CONNECTION FOR USER: XXX
(...)
&lt;192.168.0.22&gt; SSH: ansible.cfg set ssh_args: (-C)(-o)(ControlMaster=auto)(-o)(ControlPersist=60s)
&lt;192.168.0.22&gt; SSH: ANSIBLE_HOST_KEY_CHECKING/host_key_checking disabled: (-o)(StrictHostKeyChecking=no)
&lt;192.168.0.22&gt; SSH: ANSIBLE_REMOTE_PORT/remote_port/ansible_port set: (-o)(Port=22)
&lt;192.168.0.22&gt; SSH: ANSIBLE_REMOTE_USER/remote_user/ansible_user/user/-u set: (-o)(User=XXX)
&lt;192.168.0.22&gt; SSH: ANSIBLE_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)
&lt;192.168.0.22&gt; SSH: PlayContext set ssh_common_args: (-o)(StrictHostKeyChecking=no)
&lt;192.168.0.22&gt; SSH: PlayContext set ssh_extra_args: (-o)(UserKnownHostsFile=/dev/null)
&lt;192.168.0.22&gt; SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/XXX/.ansible/cp/%h-%p-%r)
&lt;192.168.0.22&gt; SSH: EXEC sshpass -d14 ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=22 -o User=XXX -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ControlPath=/home/XXX/.ansible/cp/%h-%p-%r 192.168.0.22 '/bin/sh -c '"'"'echo ~ &amp;&amp; sleep 0'"'"''
(...)
RETRYING BECAUSE OF CONTROLPERSIST BROKEN PIPE
&lt;192.168.0.22&gt; SSH: EXEC sshpass -d14 ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=22 -o User=XXX -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ControlPath=/home/XXX/.ansible/cp/%h-%p-%r 192.168.0.22 '/bin/sh -c '"'"'echo ~ &amp;&amp; sleep 0'"'"''
(...)
close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor
(...)
The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 125, in run
    res = self._execute()
  File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 522, in _execute
    result = self._handler.run(task_vars=variables)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/action/package.py", line 69, in run
    result.update(self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async))
  File "/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py", line 637, in _execute_module
    tmp = self._make_tmp_path()
  File "/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py", line 248, in _make_tmp_path
    tmpdir =  self._remote_expand_user(C.DEFAULT_REMOTE_TMP, sudoable=False)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py", line 545, in _remote_expand_user
    data = self._low_level_execute_command(cmd, sudoable=False)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py", line 883, in _low_level_execute_command
    rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/connection/ssh.py", line 788, in exec_command
    (returncode, stdout, stderr) = self._run(cmd, in_data, sudoable=sudoable)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/connection/ssh.py", line 85, in wrapped
    return_tuple = func(self, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/connection/ssh.py", line 461, in _run
    os.write(self.sshpass_pipe[1], to_bytes(self._play_context.password) + b'
')
OSError: [Errno 9] Bad file descriptor
fatal: [192.168.0.22]: FAILED! =&gt; {
    "failed": true, 
    "msg": "Unexpected failure during module execution.", 
    "stdout": ""
}
</code></pre></div>