<p dir="auto">I was unable to find a duplicate issue related to the <code class="notranslate">script</code> module similar to this issue.</p>
<h5 dir="auto">ISSUE TYPE</h5>

<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>

<p dir="auto">script</p>
<h5 dir="auto">ANSIBLE VERSION</h5>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.5.0"><pre class="notranslate"><code class="notranslate">ansible 2.5.0
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>

<h5 dir="auto">OS / ENVIRONMENT</h5>

<p dir="auto">Ubuntu 16.04.3 LTS</p>
<h5 dir="auto">SUMMARY</h5>

<p dir="auto">Script module returns different results for same shell script depending if it is run locally or if it is run via ssh.   Most particularly data printed by  the script on stderr gets reported on stdin when executed over ssh but is correctly tagged when executed locally.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>

<p dir="auto">A script <code class="notranslate">foo</code> containing the following:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#!/bin/bash

echo this is stout
echo this is stderr &gt;&amp;2"><pre class="notranslate"><code class="notranslate">#!/bin/bash

echo this is stout
echo this is stderr &gt;&amp;2
</code></pre></div>
<p dir="auto">is executed via ansible locally as follows:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible localhost -m script -a foo"><pre class="notranslate"><code class="notranslate">ansible localhost -m script -a foo
</code></pre></div>
<p dir="auto">It generates the output below:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=" [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'

localhost | SUCCESS =&gt; {
    &quot;changed&quot;: true, 
    &quot;rc&quot;: 0, 
    &quot;stderr&quot;: &quot;this is stderr\n&quot;, 
    &quot;stdout&quot;: &quot;this is stout\n&quot;, 
    &quot;stdout_lines&quot;: [
        &quot;this is stout&quot;
    ]
}"><pre class="notranslate"><code class="notranslate"> [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'

localhost | SUCCESS =&gt; {
    "changed": true, 
    "rc": 0, 
    "stderr": "this is stderr\n", 
    "stdout": "this is stout\n", 
    "stdout_lines": [
        "this is stout"
    ]
}
</code></pre></div>
<p dir="auto">When executed via ssh as follows:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible -i inventory localhost -m script -a foo"><pre class="notranslate"><code class="notranslate">ansible -i inventory localhost -m script -a foo
</code></pre></div>
<p dir="auto">it generates the following output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="localhost | SUCCESS =&gt; {
    &quot;changed&quot;: true, 
    &quot;rc&quot;: 0, 
    &quot;stderr&quot;: &quot;Shared connection to localhost closed.\r\n&quot;, 
    &quot;stdout&quot;: &quot;this is stout\r\nthis is stderr\r\n&quot;, 
    &quot;stdout_lines&quot;: [
        &quot;this is stout&quot;, 
        &quot;this is stderr&quot;
    ]
}"><pre class="notranslate"><code class="notranslate">localhost | SUCCESS =&gt; {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to localhost closed.\r\n", 
    "stdout": "this is stout\r\nthis is stderr\r\n", 
    "stdout_lines": [
        "this is stout", 
        "this is stderr"
    ]
}
</code></pre></div>

<p dir="auto">Details are committed here:<br>
<a href="https://github.com/cfedde/ansible_shell_issue">https://github.com/cfedde/ansible_shell_issue</a></p>
<h5 dir="auto">EXPECTED RESULTS</h5>

<p dir="auto">I expected to see stderr output from the script "foo" to be in the stderr  attribute:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="localhost | SUCCESS =&gt; {
    &quot;changed&quot;: true, 
    &quot;rc&quot;: 0, 
    &quot;stderr&quot;: &quot;this is stderr\n&quot;, 
    &quot;stdout&quot;: &quot;this is stout\n&quot;, 
    &quot;stdout_lines&quot;: [
        &quot;this is stout&quot;
    ]
}"><pre class="notranslate"><code class="notranslate">localhost | SUCCESS =&gt; {
    "changed": true, 
    "rc": 0, 
    "stderr": "this is stderr\n", 
    "stdout": "this is stout\n", 
    "stdout_lines": [
        "this is stout"
    ]
}
</code></pre></div>
<h5 dir="auto">ACTUAL RESULTS</h5>

<p dir="auto">Remote results did not identify scripts's stderr output.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="localhost | SUCCESS =&gt; {
    &quot;changed&quot;: true, 
    &quot;rc&quot;: 0, 
    &quot;stderr&quot;: &quot;Shared connection to localhost closed.\r\n&quot;, 
    &quot;stdout&quot;: &quot;this is stout\r\nthis is stderr\r\n&quot;, 
    &quot;stdout_lines&quot;: [
        &quot;this is stout&quot;, 
        &quot;this is stderr&quot;
    ]
}"><pre class="notranslate"><code class="notranslate">localhost | SUCCESS =&gt; {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to localhost closed.\r\n", 
    "stdout": "this is stout\r\nthis is stderr\r\n", 
    "stdout_lines": [
        "this is stout", 
        "this is stderr"
    ]
}
</code></pre></div>
<p dir="auto">I recently opened issue <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="119863158" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/13388" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/13388/hovercard" href="https://github.com/ansible/ansible/issues/13388">#13388</a> where I tried to introduce the <code class="notranslate">--user=&lt;user&gt;</code> option for Ansibles Docker connection plugin. I concluded within this issue, that my changes made the execution of Ansible tasks on Docker containers really slow.</p>
<p dir="auto">Today I started a Playbook on another machine (at work), where I also deploy tasks to a container with the Docker connectio plugin and everything went fine, till I updated my Ansible installation. Since then every task executed on a Docker containe takes very long and Python consumes 100% CPU.</p>
<p dir="auto">After some time of investigation it turns out that this behavior was caused by the pull request <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="117521066" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/13200" data-hovercard-type="pull_request" data-hovercard-url="/ansible/ansible/pull/13200/hovercard" href="https://github.com/ansible/ansible/pull/13200">#13200</a> which was merged yesterday: <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/ansible/ansible/commit/bebd2c5f3422b3c52ca0018d19ea4702c066c3e2/hovercard" href="https://github.com/ansible/ansible/commit/bebd2c5f3422b3c52ca0018d19ea4702c066c3e2"><tt>bebd2c5</tt></a>.</p>
<p dir="auto">Here is an example Playbook, which will reproduce the current behavior:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: this playbook is for testing purposes
  hosts: localhost
  tasks:

    - name: run jenkins container
      docker:
        name: jenkinstest
        image: jenkins
        state: started
        docker_user: root

    - name: add container to inventory
      add_host:
        name: jenkinstest
        ansible_connection: docker

    - name: run a task inside the container
      delegate_to: jenkinstest
      file:
        path: /root/test/me
        state: directory"><pre class="notranslate"><code class="notranslate">- name: this playbook is for testing purposes
  hosts: localhost
  tasks:

    - name: run jenkins container
      docker:
        name: jenkinstest
        image: jenkins
        state: started
        docker_user: root

    - name: add container to inventory
      add_host:
        name: jenkinstest
        ansible_connection: docker

    - name: run a task inside the container
      delegate_to: jenkinstest
      file:
        path: /root/test/me
        state: directory
</code></pre></div>
<p dir="auto">If you reset your local repository to the mentioned pull request, this playbook will hang on the third task and Python consumes 100% CPU for a long time. If you reset your local repository to the commit before, everything works as expected and the playbook should be executed within seconds.</p>
<p dir="auto">The changes made are too sophisticated for my humble Python knowledge. So <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/amenonsen/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/amenonsen">@amenonsen</a> please have a look, what could cause the behavior. Maybe <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/abadger/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/abadger">@abadger</a> has some good ideas too.</p>
<p dir="auto">This issue would enable feature request <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="119863158" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/13388" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/13388/hovercard" href="https://github.com/ansible/ansible/issues/13388">#13388</a>.</p>