# How to read variable file each iteration of a task?

**URL:** <https://forum.ansible.com/t/how-to-read-variable-file-each-iteration-of-a-task/22820>\
**Category:** Ansible Project\
**Created:** [October 18, 2016, 7:02pm UTC](https://forum.ansible.com/t/how-to-read-variable-file-each-iteration-of-a-task/22820 "2016-10-18T19:02:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Tkach](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@Nick\_Tkach](https://forum.ansible.com/u/Nick_Tkach)\
**Post date:** [October 18, 2016, 7:02pm UTC](https://forum.ansible.com/t/how-to-read-variable-file-each-iteration-of-a-task/22820/1 "2016-10-18T19:02:43Z")

</div>

So, the general idea is I’ve got a playbook that has a set of “property” files (yaml format). Different filenames, but the variable names inside are the same:

`  
group\_vars/siteA.yml

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [October 18, 2016, 8:56pm UTC](https://forum.ansible.com/t/how-to-read-variable-file-each-iteration-of-a-task/22820/2 "2016-10-18T20:56:52Z")

</div>

Have you considered [conditional includes](http://docs.ansible.com/ansible/playbooks_conditionals.html#applying-when-to-roles-and-includes)? [Along with include\_vars](http://docs.ansible.com/ansible/include_vars_module.html#include-vars-load-variables-from-files-dynamically-within-a-task) I think you should be able to achieve the desired effect.

Something like the below:

`

---

<div class="post-metadata">

**Author:** ![Nick\_Tkach](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@Nick\_Tkach](https://forum.ansible.com/u/Nick_Tkach)\
**Post date:** [October 19, 2016, 1:39pm UTC](https://forum.ansible.com/t/how-to-read-variable-file-each-iteration-of-a-task/22820/3 "2016-10-19T13:39:35Z")

</div>

Yes, thanks I think that does help some. I think the part I’m missing still is how to somehow pull in each file name one at a time. I know in pseudo-code it’s something like

`  
for( one\_filename in fileglob( vars/\*.yml) {

- include\_vars “{{ one\_filename ))”
- debug: msg=“{{ site\_name }} on port {{ port }}”  
}

`

I know you can’t just do something like

`

- include\_vars “{{ item }}”  
with\_fileglob:
- “vars/\*.yml”

`  
since that’s going to just pull in the whole list immediately and print out the results at the end.

---

<div class="post-metadata">

**Author:** ![Nick\_Tkach](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@Nick\_Tkach](https://forum.ansible.com/u/Nick_Tkach)\
**Post date:** [October 19, 2016, 2:09pm UTC](https://forum.ansible.com/t/how-to-read-variable-file-each-iteration-of-a-task/22820/4 "2016-10-19T14:09:26Z")

</div>

I just realized I left out one key piece of info on this. It’s all running on localhost always. The idea with this is we’re generating a series of config files locally that a separate script later is going to deal with actually pushing out (for various reasons it needs to be outside of Ansible for now). So unfortunately I don’t think we can take advantage of inventory or group.
