Multiple loop_control loop_var In Ansible Does Not Work.
Environment
Red Hat Enterprise Linux 7
Non-Red Hat provided Ansible 2.1.1.0-1
Issue
If I have the following tasks defined in a main.yml role, there are no issues:
---
- name: "Installing jdk1.8.0_121"
yum: name="jdk1.8.0_121" state=latest
register: p
- name: "Output of jdk1.8.0_121 install"
shell: "echo \"\""
with_items: ""
I can have multiple tasks listing a different package name. Everything works just fine.
If I combine the multiple tasks into a single task file (e.g. yum-install.yml):
- name: "Installing "
yum: name="" state=latest
register: p
- name: "Output of install"
shell: "echo \"\""
with_items: ""
...and include that in a main.yml task with a loop, like the following:
---
- include: yum-install.yml obj=
with_items:
- jdk1.8.0_121
- foo
loop_control:
loop_var: pn
...it also works.
If I replace the item array with another loop control, like the following:
---
- name: "Installing "
yum: name="" state=latest
register: p
- name: "Output of install"
shell: "echo \"\""
with_items: ""
loop_control:
loop_var: rl
...then the yum output is never displayed. Here is part of the output of the second scenario:
# '/bin/ansible-playbook ./server.yml'
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [java : include] **********************************************************
included: /root/ansible/java/tasks/yum-install.yml for localhost
included: /root/ansible/java/tasks/yum-install.yml for localhost
TASK [java : Installing jdk1.8.0_121] ******************************************
changed: [localhost]
TASK [java : Output of jdk1.8.0_121 install] ***********************************
changed: [localhost] => (item=Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
While here is the same section of output of replacing the item array with another loop control:
# '/bin/ansible-playbook ./server.yml'
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [java : include] **********************************************************
included: /root/ansible/java/tasks/yum-install.yml for localhost
included: /root/ansible/java/tasks/yum-install.yml for localhost
TASK [java : Installing jdk1.8.0_121] ******************************************
changed: [localhost]
TASK [java : Output of jdk1.8.0_121 install] ***********************************
changed: [localhost] => (item=None)
Resolution
This problem could only be replicated in version 2.1.1. Tests in both 2.2.1 and 2.3.0 showed the expected output. Some bugs in earlier versions of Ansible appear to have fixed the problem:
- Nested task includes when passed a list to iterate over, receive the list passed to the outermost parent.
- Ansible 2.0.0.2: Nested include + with_items does not work properly
Please also note that Red Hat does not support Ansbile Core. Ansible Tower is our paid support offering.
The quickest and most efficient method to report or inquire about Ansible Core specific issues would be through the community. Bug reports can be filed here: https://github.com/ansible/ansible/issues/new. The community mailing list is also a good resource for clearing up questions or verifying expected behavior.
Root Cause
Bug that has since been fixed in newer versions.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments