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

with_items flattening nested environments to top level instead of using most nested #33358

Closed
chamila-c opened this issue Nov 28, 2017 · 4 comments
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@chamila-c
Copy link

chamila-c commented Nov 28, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

core
(with_items and nested environments)

ANSIBLE VERSION
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)]
CONFIGURATION
> 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
OS / ENVIRONMENT

Control + Remote (local playbook):
Centos 7

> uname -sr
Linux 3.10.0-693.5.2.el7.x86_64
SUMMARY

Use of with_items in a command task that runs within a nested set of environments is flattening the nesting and reverting to the top level environment instead of keeping the most nested environment

STEPS TO REPRODUCE

Create the below files in a single directory and then run this command from within that directory:
SHELL_ENV_VAR=shell_env_var SHARED_ENV_VAR=shell_env_var ansible-playbook playbook.yml -i localhost, -c local -v

ansible.cfg

[defaults]
force_color=1
retry_files_enabled=False

playbook.yml

---
- 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"

first-level-tasks.yml


    - 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"

second-level-tasks.yml


    - 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
EXPECTED RESULTS

The command stdout/stdout_lines from both tasks in second-level-tasks.yml (TASK [Command singular] and TASK [Command with items]) should be identical

ACTUAL RESULTS

The addition of with_items: [1] (which should be a non-semantic change to a command that doesn't actually reference the {{ item }}s) causes the output of the task to change. It appears that the nested environment is reverting to the top-level (i.e. playbook level) environment when with_items is used (see playbook output below)

Using /home/centos/repos/ansible/ansible.cfg as config file

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

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

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

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

TASK [Command singular] **************************************************************************************************************************************
changed: [localhost] => {"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] => (item=1) => {"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   

@ansibot
Copy link
Contributor

ansibot commented Nov 28, 2017

Files identified in the description:
None

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug_report needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Nov 28, 2017
@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Nov 30, 2017
@wimnat
Copy link
Contributor

wimnat commented Dec 8, 2017

I face a similar issue. Example playbook to demonstate problem:

---
- name: Test play to demonstate environment failing in loop
  hosts: local
  connection: local
  vars:
    alist:
      - item1
      - item2
  tasks:
    - shell: echo $MYVAR

    - shell: echo $MYVAR
      environment:
        MYVAR: override_helloworld

    - shell: echo $MYVAR
      environment:
        MYVAR: override_helloworld_with_a_list
      with_items: alist

  environment:
    MYVAR: helloworld

When you run the play i would expect an output of 'override_helloworld_with_a_list' when i reach the with_items task but i actually get 'helloworld'.

@sivel
Copy link
Member

sivel commented Dec 8, 2017

I've bisected this to df5895e / 34511d1

@ansible ansible deleted a comment from biomassives Dec 8, 2017
@sivel
Copy link
Member

sivel commented Dec 8, 2017

Closing as a duplicate of #32685

@sivel sivel closed this as completed Dec 8, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

5 participants