Multiple Loop Vars Problem

28 views
Skip to first unread message

acharyat...@gmail.com

unread,
Nov 21, 2018, 1:10:16 PM11/21/18
to Ansible Project
Hi,

I am trying to pass each item from a list using "with_items" and trying to control it using "loop_control" & "loop_vars".But when I have defined another "loop_control" & "loop_vars" along with "with_items".The playbook is throwing error.Below are the 2 playbooks details:

#####  main.yml  ####
---
- hosts: localhost
  gather_facts: no
  tasks:
  - include_tasks: inner2.yml
    with_items:
      - 1
      - 2
      - 3
    loop_control:
      loop_var: outer_item

  - include_tasks: inner2.yml
    with_items:
      - 4
      - 5
      - 6
    loop_control:
      loop_var: outer_item2

#####  inner.yml  ####
---
  - debug:
      msg: "outer item={{ outer_item }} inner item={{ item }} outer2 item={{ outer_item2 }}"
    with_items:
      - a
      - b
      - c

And the output is


PLAY [localhost] ********************************************************************************************************

TASK [include_tasks] ****************************************************************************************************
included: /etc/ansible/playbooks/inner2.yml for localhost
included: /etc/ansible/playbooks/inner2.yml for localhost
included: /etc/ansible/playbooks/inner2.yml for localhost

TASK [debug] ************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'outer_item2' is undefined\n\nThe error appears to have been in '/etc/ansible/playbooks/inner2.yml': line 2, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n  - debug:\n    ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'outer_item2' is undefined"}
to retry, use: --limit @/etc/ansible/playbooks/main2.retry

PLAY RECAP **************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1   

TASK: include_tasks ----------------------------------------------------- 0.06s
TASK: debug ------------------------------------------------------------- 0.03s
TASK: include_tasks ----------------------------------------------------- 0.06s
TASK: debug ------------------------------------------------------------- 0.03s
Playbook run took 0 days, 0 hours, 0 minutes, 0 seconds


Can anyone help me how can I pass "outer_item2" values also.

Thanks,
Acharya Tejaswi Indurthy.

Brian Coca

unread,
Nov 21, 2018, 1:40:37 PM11/21/18
to ansible...@googlegroups.com
expected, since when you include in the first task, outer_item2 is not
defined. use `deafult` filter to avoid the errors.




--
----------
Brian Coca

Brian Coca

unread,
Nov 21, 2018, 1:42:13 PM11/21/18
to ansible...@googlegroups.com
this should get it working for both includes:

- debug:
msg: "outer item={{ outer_item|default('none') }} inner item={{
item }} outer2 item={{ outer_item2|default('none') }}"
with_items:
- a
- b
- c


--
----------
Brian Coca

acharyat...@gmail.com

unread,
Nov 21, 2018, 3:15:48 PM11/21/18
to Ansible Project
Thanks Brain that worked for me.
Reply all
Reply to author
Forward
0 new messages