<p>I think you can just include_vars at directory level with <code>dir</code></p>
<pre><code>tasks:
  - name: &quot;Read my pets from pets folder&quot;
    include_vars:
      dir: &quot;pets&quot;
      name: all_pets
</code></pre>

CC BY-SA 4.0
2021-03-03T14:00:59.943Z
2021-03-03T14:00:59.943Z
Iván
<p>Sounds like you want the <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html?highlight=filter#combining-hashes-dictionaries" rel="nofollow noreferrer">combine</a> filter.</p>

<p>You could read each file into a specific variable, then use the combine filter to create <code>all-pets</code>. At that point, it would be in memory of course, so you might want to use the <a href="https://stackoverflow.com/a/26640778/2707870">answer</a> to <a href="https://stackoverflow.com/questions/26638180/write-variable-to-a-file-in-ansible">this question</a> to write it to a file:</p>

<pre><code>- name: Read vars
  include_vars:
    file: "pets/{{ item }}_pets.yml"
    var: "{{ item }}"
  loop:
    - steves
    - jacks

- name: Combine vars
  set_fact:
    all_pets: {{ steves | combine(jacks) }}


- name: Write the file
  copy:
    content: "{{ all_pets | to_yaml }}"
    dest: pets/all_pets.yml
</code></pre>

CC BY-SA 4.0
2019-07-31T07:21:59.547Z
Many thanks for this but the folder could actually contain a large number of files so can't really type out each - ideally it should just load every file in the folder.
Sam
CC BY-SA 4.0
2019-07-31T07:25:18.773Z
In that case you can use a fileglob in the include_vars but the same strategy should hold for combining the cars. However if there are so many files, perhaps a vars plugin should be considered for combining them efficiently. It's a cost benefit trade-off
Bruce Becker
CC BY-SA 4.0
2019-07-31T09:33:58.820Z
Thanks Bruce, think I've got it (maybe) for `with_fileglob` in the first task and the third looks straightforward enough, but how do I use the combine bit if not specifying steves etc?
Sam
CC BY-SA 4.0
2019-07-31T09:47:01.763Z
You should be able to use loop control to reference the individual vars during the loop - https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#adding-controls-to-loops
Bruce Becker
CC BY-SA 4.0
2019-07-31T13:53:11.903Z
Unfortunately it doesn't actually seem to work as is. If I make a couple of minor tweaks to your tasks (`var` is invalid for `include_vars` - think it should be `name` and the second block needs quotes around the value) then you'll only get one hamster, because the value read from the second file will overwrite the first file's value from the same name variable.
Sam
CC BY-SA 4.0
2019-12-29T10:57:37.033Z
In this particular case, you might be able to obtain what you want using [`hash_behavior=merge` in ansible.cfg](https://docs.ansible.com/ansible/2.3/intro_configuration.html#hash-behaviour)
Zeitounator
CC BY-SA 4.0
2019-07-30T15:42:35.223Z
2019-07-30T15:49:37.170Z
2019-07-30T15:49:37.170Z
Bruce Becker
<p>What I'm looking for is a task (or tasks) to read whatever .yml files exist in a specified folder and combine them into a file containing all the variables. For example:</p>

<p>pets/steves_pets.yml:</p>

<pre><code>cats:
  - spot
  - snowy
hamsters:
  - mephistopheles
</code></pre>

<p>And</p>

<p>pets/jacks-pets.yml:</p>

<pre><code>dogs:
  - rex
  - woof
hamsters:
  - prometheus
</code></pre>

<p>Becomes</p>

<p>wherever/all-pets.yml:</p>

<pre><code>cats:
  - spot
  - snowy
dogs:
  - rex
  - woof
hamsters:
  - mephistopheles
  - prometheus
</code></pre>

<p>In fact it's not even necessary to generate an all-pets.yml - as long as the full combined list is available to Ansible for use in roles / tasks. But a combined list file approach would be fine if easier.</p>

<p>Any pointers would be greatly appreciated!</p>

<ansible><jinja>
Ansible fetch all vars files from a directory to generate a combined vars file
Sam
# Ansible fetch all vars files from a directory to generate a combined vars file

(Asked by: username_0 on 2019-07-30)

What I'm looking for is a task (or tasks) to read whatever .yml files exist in a specified folder and combine them into a file containing all the variables. For example:
pets/steves_pets.yml:
cats:
  - spot
  - snowy
hamsters:
  - mephistopheles

And
pets/jacks-pets.yml:
dogs:
  - rex
  - woof
hamsters:
  - prometheus

Becomes
wherever/all-pets.yml:
cats:
  - spot
  - snowy
dogs:
  - rex
  - woof
hamsters:
  - mephistopheles
  - prometheus

In fact it's not even necessary to generate an all-pets.yml - as long as the full combined list is available to Ansible for use in roles / tasks. But a combined list file approach would be fine if easier.
Any pointers would be greatly appreciated!

**Tags:** ansible, jinja


---

## 2 answers

### Answer by username_1 on 2021-03-03. Score: 0

I think you can just include_vars at directory level with dir
tasks:
  - name: "Read my pets from pets folder"
    include_vars:
      dir: "pets"
      name: all_pets


---

### Answer by username_2 on 2019-07-30. Score: 0

Sounds like you want the combine filter.
You could read each file into a specific variable, then use the combine filter to create all-pets. At that point, it would be in memory of course, so you might want to use the answer to this question to write it to a file:
- name: Read vars
  include_vars:
    file: "pets/{{ item }}_pets.yml"
    var: "{{ item }}"
  loop:
    - steves
    - jacks

- name: Combine vars
  set_fact:
    all_pets: {{ steves | combine(jacks) }}


- name: Write the file
  copy:
    content: "{{ all_pets | to_yaml }}"
    dest: pets/all_pets.yml

> **Comments:**
> **username_0**: Many thanks for this but the folder could actually contain a large number of files so can't really type out each - ideally it should just load every file in the folder.  
> **username_2**: In that case you can use a fileglob in the include_vars but the same strategy should hold for combining the cars. However if there are so many files, perhaps a vars plugin should be considered for combining them efficiently. It's a cost benefit trade-off  
> **username_0**: Thanks Bruce, think I've got it (maybe) for `with_fileglob` in the first task and the third looks straightforward enough, but how do I use the combine bit if not specifying steves etc?  
> **username_2**: You should be able to use loop control to reference the individual vars during the loop - https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#adding-controls-to-loops  
> **username_0**: Unfortunately it doesn't actually seem to work as is. If I make a couple of minor tweaks to your tasks (`var` is invalid for `include_vars` - think it should be `name` and the second block needs quotes around the value) then you'll only get one hamster, because the value read from the second file will overwrite the first file's value from the same name variable.  
> **username_3**: In this particular case, you might be able to obtain what you want using [`hash_behavior=merge` in ansible.cfg](https://docs.ansible.com/ansible/2.3/intro_configuration.html#hash-behaviour)  

---

