<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">core<br>
(<code class="notranslate">with_items</code> and nested <code class="notranslate">environment</code>s)</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.4.1.0
  config file = /home/centos/repos/ansible/ansible.cfg
  configured module search path = [u'/home/centos/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  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.4.1.0
  config file = /home/centos/repos/ansible/ansible.cfg
  configured module search path = [u'/home/centos/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  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>
<h5 dir="auto">CONFIGURATION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt; ansible-config dump --only-changed
ANSIBLE_FORCE_COLOR(/home/centos/repos/ansible/ansible.cfg) = True
RETRY_FILES_ENABLED(/home/centos/repos/ansible/ansible.cfg) = False"><pre class="notranslate"><code class="notranslate">&gt; ansible-config dump --only-changed
ANSIBLE_FORCE_COLOR(/home/centos/repos/ansible/ansible.cfg) = True
RETRY_FILES_ENABLED(/home/centos/repos/ansible/ansible.cfg) = False
</code></pre></div>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Control + Remote (local playbook):<br>
Centos 7</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt; uname -sr
Linux 3.10.0-693.5.2.el7.x86_64"><pre class="notranslate"><code class="notranslate">&gt; uname -sr
Linux 3.10.0-693.5.2.el7.x86_64
</code></pre></div>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">Use of <code class="notranslate">with_items</code> in a <code class="notranslate">command</code> task that runs within a nested set of <code class="notranslate">environment</code>s is flattening the nesting and reverting to the top level <code class="notranslate">environment</code> instead of keeping the most nested <code class="notranslate">environment</code></p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">Create the below files in a single directory and then run this command from within that directory:<br>
<code class="notranslate">SHELL_ENV_VAR=shell_env_var SHARED_ENV_VAR=shell_env_var ansible-playbook playbook.yml -i localhost, -c local -v</code></p>
<p dir="auto"><em>ansible.cfg</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[defaults]
force_color=1
retry_files_enabled=False"><pre class="notranslate"><code class="notranslate">[defaults]
force_color=1
retry_files_enabled=False
</code></pre></div>
<p dir="auto"><em>playbook.yml</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---
- hosts: all
  gather_facts: false

  tasks:
    - debug: msg=&quot;This is the playbook level&quot;

    - import_tasks: first-level-tasks.yml
      environment:
        PLAYBOOK_ENV_VAR: &quot;playbook_env_var&quot;
        SHARED_ENV_VAR: &quot;playbook_env_var&quot;"><pre class="notranslate"><code class="notranslate">---
- hosts: all
  gather_facts: false

  tasks:
    - debug: msg="This is the playbook level"

    - import_tasks: first-level-tasks.yml
      environment:
        PLAYBOOK_ENV_VAR: "playbook_env_var"
        SHARED_ENV_VAR: "playbook_env_var"
</code></pre></div>
<p dir="auto"><em>first-level-tasks.yml</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
    - debug: msg=&quot;This is the first level tasks&quot;

    - import_tasks: second-level-tasks.yml
      environment:
        LVL1_ENV_VAR: &quot;lvl1_env_var&quot;
        SHARED_ENV_VAR: &quot;lvl1_env_var&quot;"><pre class="notranslate"><code class="notranslate">
    - debug: msg="This is the first level tasks"

    - import_tasks: second-level-tasks.yml
      environment:
        LVL1_ENV_VAR: "lvl1_env_var"
        SHARED_ENV_VAR: "lvl1_env_var"
</code></pre></div>
<p dir="auto"><em>second-level-tasks.yml</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
    - debug: msg=&quot;This is the second level tasks&quot;

    - name: Command singular
      command: &quot;echo Shell: \&quot;${SHELL_ENV_VAR}\&quot;; Playbook: \&quot;${PLAYBOOK_ENV_VAR}\&quot;; Lvl1: \&quot;${LVL1_ENV_VAR}\&quot;; Shared: \&quot;${SHARED_ENV_VAR}\&quot;&quot;

    - name: Command with items
      command: &quot;echo Shell: \&quot;${SHELL_ENV_VAR}\&quot;; Playbook: \&quot;${PLAYBOOK_ENV_VAR}\&quot;; Lvl1: \&quot;${LVL1_ENV_VAR}\&quot;; Shared: \&quot;${SHARED_ENV_VAR}\&quot;&quot;
      with_items:
        - 1"><pre class="notranslate"><code class="notranslate">
    - debug: msg="This is the second level tasks"

    - name: Command singular
      command: "echo Shell: \"${SHELL_ENV_VAR}\"; Playbook: \"${PLAYBOOK_ENV_VAR}\"; Lvl1: \"${LVL1_ENV_VAR}\"; Shared: \"${SHARED_ENV_VAR}\""

    - name: Command with items
      command: "echo Shell: \"${SHELL_ENV_VAR}\"; Playbook: \"${PLAYBOOK_ENV_VAR}\"; Lvl1: \"${LVL1_ENV_VAR}\"; Shared: \"${SHARED_ENV_VAR}\""
      with_items:
        - 1
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">The command <code class="notranslate">stdout</code>/<code class="notranslate">stdout_lines</code> from both tasks in <code class="notranslate">second-level-tasks.yml</code> (<code class="notranslate">TASK [Command singular]</code> and <code class="notranslate">TASK [Command with items]</code>)  should be identical</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">The addition of <code class="notranslate">with_items: [1]</code> (which should be a non-semantic change to a command that doesn't actually reference the <code class="notranslate">{{ item }}</code>s) causes the output of the task to change. It appears that the nested <code class="notranslate">environment</code> is reverting to the top-level (i.e. playbook level) <code class="notranslate">environment</code> when <code class="notranslate">with_items</code> is used (see playbook output below)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Using /home/centos/repos/ansible/ansible.cfg as config file

PLAY [all] ***************************************************************************************************************************************************

TASK [debug] *************************************************************************************************************************************************
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;This is the playbook level&quot;
}

TASK [debug] *************************************************************************************************************************************************
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;This is the first level tasks&quot;
}

TASK [debug] *************************************************************************************************************************************************
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;This is the second level tasks&quot;
}

TASK [Command singular] **************************************************************************************************************************************
changed: [localhost] =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: [&quot;echo&quot;, &quot;Shell:&quot;, &quot;${SHELL_ENV_VAR};&quot;, &quot;Playbook:&quot;, &quot;${PLAYBOOK_ENV_VAR};&quot;, &quot;Lvl1:&quot;, &quot;${LVL1_ENV_VAR};&quot;, &quot;Shared:&quot;, &quot;${SHARED_ENV_VAR}&quot;], &quot;delta&quot;: &quot;0:00:00.014676&quot;, &quot;end&quot;: &quot;2017-11-29 09:02:18.033588&quot;, &quot;failed&quot;: false, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2017-11-29 09:02:18.018912&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stderr_lines&quot;: [], &quot;stdout&quot;: &quot;Shell: shell_env_var; Playbook: playbook_env_var; Lvl1: lvl1_env_var; Shared: lvl1_env_var&quot;, &quot;stdout_lines&quot;: [&quot;Shell: shell_env_var; Playbook: playbook_env_var; Lvl1: lvl1_env_var; Shared: lvl1_env_var&quot;]}

TASK [Command with items] ************************************************************************************************************************************
changed: [localhost] =&gt; (item=1) =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: [&quot;echo&quot;, &quot;Shell:&quot;, &quot;${SHELL_ENV_VAR};&quot;, &quot;Playbook:&quot;, &quot;${PLAYBOOK_ENV_VAR};&quot;, &quot;Lvl1:&quot;, &quot;${LVL1_ENV_VAR};&quot;, &quot;Shared:&quot;, &quot;${SHARED_ENV_VAR}&quot;], &quot;delta&quot;: &quot;0:00:00.005918&quot;, &quot;end&quot;: &quot;2017-11-29 09:02:18.384945&quot;, &quot;failed&quot;: false, &quot;item&quot;: 1, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2017-11-29 09:02:18.379027&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stderr_lines&quot;: [], &quot;stdout&quot;: &quot;Shell: shell_env_var; Playbook: playbook_env_var; Lvl1: lvl1_env_var; Shared: playbook_env_var&quot;, &quot;stdout_lines&quot;: [&quot;Shell: shell_env_var; Playbook: playbook_env_var; Lvl1: lvl1_env_var; Shared: playbook_env_var&quot;]}

PLAY RECAP ***************************************************************************************************************************************************
localhost                 : ok=5    changed=2    unreachable=0    failed=0   
"><pre class="notranslate"><code class="notranslate">Using /home/centos/repos/ansible/ansible.cfg as config file

PLAY [all] ***************************************************************************************************************************************************

TASK [debug] *************************************************************************************************************************************************
ok: [localhost] =&gt; {
    "msg": "This is the playbook level"
}

TASK [debug] *************************************************************************************************************************************************
ok: [localhost] =&gt; {
    "msg": "This is the first level tasks"
}

TASK [debug] *************************************************************************************************************************************************
ok: [localhost] =&gt; {
    "msg": "This is the second level tasks"
}

TASK [Command singular] **************************************************************************************************************************************
changed: [localhost] =&gt; {"changed": true, "cmd": ["echo", "Shell:", "${SHELL_ENV_VAR};", "Playbook:", "${PLAYBOOK_ENV_VAR};", "Lvl1:", "${LVL1_ENV_VAR};", "Shared:", "${SHARED_ENV_VAR}"], "delta": "0:00:00.014676", "end": "2017-11-29 09:02:18.033588", "failed": false, "rc": 0, "start": "2017-11-29 09:02:18.018912", "stderr": "", "stderr_lines": [], "stdout": "Shell: shell_env_var; Playbook: playbook_env_var; Lvl1: lvl1_env_var; Shared: lvl1_env_var", "stdout_lines": ["Shell: shell_env_var; Playbook: playbook_env_var; Lvl1: lvl1_env_var; Shared: lvl1_env_var"]}

TASK [Command with items] ************************************************************************************************************************************
changed: [localhost] =&gt; (item=1) =&gt; {"changed": true, "cmd": ["echo", "Shell:", "${SHELL_ENV_VAR};", "Playbook:", "${PLAYBOOK_ENV_VAR};", "Lvl1:", "${LVL1_ENV_VAR};", "Shared:", "${SHARED_ENV_VAR}"], "delta": "0:00:00.005918", "end": "2017-11-29 09:02:18.384945", "failed": false, "item": 1, "rc": 0, "start": "2017-11-29 09:02:18.379027", "stderr": "", "stderr_lines": [], "stdout": "Shell: shell_env_var; Playbook: playbook_env_var; Lvl1: lvl1_env_var; Shared: playbook_env_var", "stdout_lines": ["Shell: shell_env_var; Playbook: playbook_env_var; Lvl1: lvl1_env_var; Shared: playbook_env_var"]}

PLAY RECAP ***************************************************************************************************************************************************
localhost                 : ok=5    changed=2    unreachable=0    failed=0   

</code></pre></div>
<p dir="auto">For example, the following yaml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  environment:
    str: &quot;play level&quot;
  tasks:
    - name: test 1
      command: &quot;/bin/echo $str&quot;
      environment:
        str: &quot;task level&quot;
    - name: test 2
      command: &quot;/bin/echo $str&quot;
      with_items: [ 1, 2, 3]
      environment:
        str: &quot;task level&quot;"><pre lang="-" class="notranslate"><code class="notranslate">  environment:
    str: "play level"
  tasks:
    - name: test 1
      command: "/bin/echo $str"
      environment:
        str: "task level"
    - name: test 2
      command: "/bin/echo $str"
      with_items: [ 1, 2, 3]
      environment:
        str: "task level"
</code></pre></div>
<p dir="auto">And execute: <code class="notranslate">ansible-playbook bug.yml -v</code></p>
<p dir="auto">Task 'test 1' will have 'task level' in stdout<br>
Task 'test 2' will loop 3 times with 'play level' in stdout</p>
<p dir="auto">IF environment is not defined on play-level, 'test 2' will use the task-level setting.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [localhost]

TASK [test 1] *************************************************************************************************************************************************************
changed: [localhost] =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: [&quot;/bin/echo&quot;, &quot;$str&quot;], &quot;delta&quot;: &quot;0:00:00.001448&quot;, &quot;end&quot;: &quot;2017-11-08 15:36:32.769761&quot;, &quot;failed&quot;: false, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2017-11-08 15:36:32.768313&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stderr_lines&quot;: [], &quot;stdout&quot;: &quot;task level&quot;, &quot;stdout_lines&quot;: [&quot;task level&quot;]}

TASK [test 2] *************************************************************************************************************************************************************
changed: [localhost] =&gt; (item=1) =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: [&quot;/bin/echo&quot;, &quot;$str&quot;], &quot;delta&quot;: &quot;0:00:00.001317&quot;, &quot;end&quot;: &quot;2017-11-08 15:36:32.971494&quot;, &quot;failed&quot;: false, &quot;item&quot;: 1, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2017-11-08 15:36:32.970177&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stderr_lines&quot;: [], &quot;stdout&quot;: &quot;play level&quot;, &quot;stdout_lines&quot;: [&quot;play level&quot;]}
changed: [localhost] =&gt; (item=2) =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: [&quot;/bin/echo&quot;, &quot;$str&quot;], &quot;delta&quot;: &quot;0:00:01.002670&quot;, &quot;end&quot;: &quot;2017-11-08 15:36:34.150920&quot;, &quot;failed&quot;: false, &quot;item&quot;: 2, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2017-11-08 15:36:33.148250&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stderr_lines&quot;: [], &quot;stdout&quot;: &quot;play level&quot;, &quot;stdout_lines&quot;: [&quot;play level&quot;]}
changed: [localhost] =&gt; (item=3) =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: [&quot;/bin/echo&quot;, &quot;$str&quot;], &quot;delta&quot;: &quot;0:00:01.002600&quot;, &quot;end&quot;: &quot;2017-11-08 15:36:35.349230&quot;, &quot;failed&quot;: false, &quot;item&quot;: 3, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2017-11-08 15:36:34.346630&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stderr_lines&quot;: [], &quot;stdout&quot;: &quot;play level&quot;, &quot;stdout_lines&quot;: [&quot;play level&quot;]}

PLAY RECAP ****************************************************************************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0   "><pre lang="PLAY" class="notranslate"><code class="notranslate">
TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [localhost]

TASK [test 1] *************************************************************************************************************************************************************
changed: [localhost] =&gt; {"changed": true, "cmd": ["/bin/echo", "$str"], "delta": "0:00:00.001448", "end": "2017-11-08 15:36:32.769761", "failed": false, "rc": 0, "start": "2017-11-08 15:36:32.768313", "stderr": "", "stderr_lines": [], "stdout": "task level", "stdout_lines": ["task level"]}

TASK [test 2] *************************************************************************************************************************************************************
changed: [localhost] =&gt; (item=1) =&gt; {"changed": true, "cmd": ["/bin/echo", "$str"], "delta": "0:00:00.001317", "end": "2017-11-08 15:36:32.971494", "failed": false, "item": 1, "rc": 0, "start": "2017-11-08 15:36:32.970177", "stderr": "", "stderr_lines": [], "stdout": "play level", "stdout_lines": ["play level"]}
changed: [localhost] =&gt; (item=2) =&gt; {"changed": true, "cmd": ["/bin/echo", "$str"], "delta": "0:00:01.002670", "end": "2017-11-08 15:36:34.150920", "failed": false, "item": 2, "rc": 0, "start": "2017-11-08 15:36:33.148250", "stderr": "", "stderr_lines": [], "stdout": "play level", "stdout_lines": ["play level"]}
changed: [localhost] =&gt; (item=3) =&gt; {"changed": true, "cmd": ["/bin/echo", "$str"], "delta": "0:00:01.002600", "end": "2017-11-08 15:36:35.349230", "failed": false, "item": 3, "rc": 0, "start": "2017-11-08 15:36:34.346630", "stderr": "", "stderr_lines": [], "stdout": "play level", "stdout_lines": ["play level"]}

PLAY RECAP ****************************************************************************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0   
</code></pre></div>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.4.1.0"><pre class="notranslate"><code class="notranslate">ansible 2.4.1.0
</code></pre></div>