# Unable to read variable in a loop using include\_vars.

**URL:** <https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371>\
**Category:** Ansible Project\
**Created:** [March 3, 2020, 4:43am UTC](https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371 "2020-03-03T04:43:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 3, 2020, 4:43am UTC](https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371/1 "2020-03-03T04:43:45Z")

</div>

Below is my playbook which loads variables and passes it to ls command on remote hosts.

`

- name: Load repository  
include\_vars:  
file=“{{ playbook\_dir }}/vars/listing.yml”  
name=user1

- set\_fact:  
allinonecmd: “{{ allinonecmd | default(‘’) + 'ls -ltr ’ + item.path + ’ '}}”  
loop: “{{ user1[inventory\_hostname] }}”

- name: Get stat  
shell: “ssh {{ USER }}@{{ inventory\_hostname }} ‘{{ allinonecmd }}’ \>/web/htdocs/{{ item.ccode }}{{ item.ipseg }}.html”  
delegate\_to: localhost  
loop: “{{ user1[inventory\_hostname] }}”  
`

below is my listing.yml  
`  
10.0.0.15:

- name: SCRIPT  
path: /tmp/scripts  
ccode: GRM-APP-  
ipseg: “0.15”
- name: MOC  
path: /tmp/moc
- name: EXE  
path: /tmp/exe
- name: MTR  
path: /tmp/mtr  
`

This worked fine without ccode and ipseg entries in the listing.yml. I have multiple entries for path which i loop over and construct the allinonecmd string but the ccode is just needed once.

I get this error with a single entry of ccode in listing.yml

`fatal: [10.0.0.15]: FAILED! => { "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'ccode'`

For my existing playbook to work i have to specify ccode and ipseg under each entry like below which works but looks ugly. It should be specified just one time and the playbook should work.

`  
10.0.0.15:

- name: SCRIPT  
path: /tmp/scripts  
ccode: GRM-APP-  
ipseg: “0.15”
- name: MOC  
path: /tmp/moc  
ccode: GRM-APP-  
ipseg: “0.15”
- name: EXE  
path: /tmp/exe  
ccode: GRM-APP-  
ipseg: “0.15”
- name: MTR  
path: /tmp/mtr  
ccode: GRM-APP-  
ipseg: “0.15”  
`

Can you please suggest a clean way to get this to work ?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 3, 2020, 7:55am UTC](https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371/2 "2020-03-03T07:55:57Z")

</div>

Can you tell what is the big picture? What task is this intended for?

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 3, 2020, 8:19am UTC](https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371/3 "2020-03-03T08:19:52Z")

</div>

Under an ip I want a variable ccode which will be global variable for that ip.

I also want variable name & path which will have multiple entries.

How can I read both single global and multiple variables name and path under a given IP ?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 3, 2020, 9:25am UTC](https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371/4 "2020-03-03T09:25:08Z")

</div>

I meant HIGH level.  
You are using ansible to achieve something - what is that?

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 3, 2020, 9:53am UTC](https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371/5 "2020-03-03T09:53:30Z")

</div>

I need to get the output of ls command from list of remote ips and under each ip there is are different directories. The output of ls command needs to be saved in a file and the name of the file should be the value of ccode variable under the variable.

Thus my variable file has ip listed. Under each ip is the list of directories with name and path variables and finally the entire output of all the ls commands on all the directories for that ip is stored in a file with ccode variable.

Hope the requirement is clear now.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 3, 2020, 9:58am UTC](https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371/6 "2020-03-03T09:58:30Z")

</div>

> I need to get the output of ls command from list of remote ips and under each ip there is are different directories. The  
> output of ls command needs to be saved in a file and the name of the file should be the value of ccode variable under  
> the variable.
> 
> Thus my variable file has ip listed. Under each ip is the list of directories with name and path variables and finally  
> the entire output of all the ls commands on all the directories for that ip is stored in a file with ccode variable.
> 
> Hope the requirement is clear now.

It is still unclear what you are going to do with the ls output. Also I would recommend to use the find module instead  
of ls on the target.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 3, 2020, 10:11am UTC](https://forum.ansible.com/t/unable-to-read-variable-in-a-loop-using-include-vars/32371/7 "2020-03-03T10:11:18Z")

</div>

The reason for grabbing the ls output across servers is for audit purpose. The output of ls is send as reports and audit team uses the ls output reports to do random checks on the server.
