# Undefined variable error when passing to include\_task file in loop

**URL:** <https://forum.ansible.com/t/undefined-variable-error-when-passing-to-include-task-file-in-loop/31364>\
**Category:** Ansible Project\
**Created:** [September 26, 2019, 7:48am UTC](https://forum.ansible.com/t/undefined-variable-error-when-passing-to-include-task-file-in-loop/31364 "2019-09-26T07:48:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 26, 2019, 7:48am UTC](https://forum.ansible.com/t/undefined-variable-error-when-passing-to-include-task-file-in-loop/31364/1 "2019-09-26T07:48:36Z")

</div>

I am dealing with nested loops inorder to build dynamic host using add\_host.

> Outer Loop: with\_items: “{{ command\_result.stdout\_lines }}” // gets me the list of users  
> Inner Loop: with\_items: “{{ dest\_ip.split(‘,’) }}” // gets me the list of IP addresses seperated by comma (,)

The below code works fine.

`

- name: Add hosts

include\_tasks: “{{ playbook\_dir }}/gethosts.yml”

vars:

dest\_ip: “{{ item.split(‘\t’)[0] }}”  
file\_dets: “{{ item.split(‘\t’)[1] }}”  
USER: “anystring”

# USER: “{% if item.split(‘\t’)[3] == ‘FrontEnd’ %}user1{% elif item.split(‘\t’)[3] == ‘BackEnd’ %}user2{% else %}{% endif %}”

ansible\_host: localhost  
install\_dir\_pass: “anystring”

# install\_dir: “{{ item.split(‘\t’)[2] }}”

with\_items: “{{ command\_result.stdout\_lines }}”  
`

Below is my include\_task gethost.yml file:

`
