# How to repeat a set of tasks for each var file and flush the previous hashed vars?

**URL:** <https://forum.ansible.com/t/how-to-repeat-a-set-of-tasks-for-each-var-file-and-flush-the-previous-hashed-vars/8807>\
**Category:** Get Help\
**Created:** [September 27, 2024, 6:51am UTC](https://forum.ansible.com/t/how-to-repeat-a-set-of-tasks-for-each-var-file-and-flush-the-previous-hashed-vars/8807 "2024-09-27T06:51:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![w0nderw0lf](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/w0nderw0lf/32/4885_2.png) [@w0nderw0lf](https://forum.ansible.com/u/w0nderw0lf)\
**Post date:** [September 27, 2024, 6:51am UTC](https://forum.ansible.com/t/how-to-repeat-a-set-of-tasks-for-each-var-file-and-flush-the-previous-hashed-vars/8807/1 "2024-09-27T06:51:57Z")

</div>

Hi everyone,

I currently try to build up a config repository for multiple departments. Every department has it’s own var file with a fixed format. This helps us to better analyze incoming changes in a merge request.

for example:  
vars/qualitygates/department1.yml

```auto
qualitygate:
  - qualitygate_name: codeverify
    operators: 
      - metric: cover
        operator: test1
        error: 1
  - qualitygate_name: department_analysis
    operators: 
      - metric: cover_unverified
        operator: test2
        error: 2
     - metric: quality
       operator: LT
       error: 0

```

the file for my next department:

vars/qualitygates/department2.yml

```auto
qualitygate:
  - qualitygate_name: codequality
    operators: 
      - metric: cover
        operator: test3
        error: 1
      - metric: security
        operator: GT
        error: 3
  - qualitygate_name: conditions
    operators: 
      - metric: cover_unverified
        operator: test4
        error: 2

```

Unfortunately including multiple var files with identical key’s will be overwritten with the newest. More information about that here. [Ansible Configuration Settings — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-hash-behaviour)

I could set DEFAULT\_HASH\_BEHAVIOUR to merge, but that is not recommended and I should use combine, vars and varnames…  
However these modules require non-identical key’s

Regarding my thread title, I try to achieve the configuration as code for a service.  
I am unsure here what might be the better solution…  
Never done this before…  
Should I…  
…change the dict’s to have a unique key and adjust my tasks?  
…use a loop that includes a var file, run tasks, drop vars and include the next file?

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [October 3, 2024, 5:36pm UTC](https://forum.ansible.com/t/how-to-repeat-a-set-of-tasks-for-each-var-file-and-flush-the-previous-hashed-vars/8807/2 "2024-10-03T17:36:06Z")

</div>

I think you could do something like this. The key being that `include_vars` allows you to store the included variables inside another variable so theres no conflict

```auto
- hosts: localhost
  vars:
    departments:
      - a
      - b
  tasks:
    - name: Include different department vars
      ansible.builtin.include_vars:
        file: department_{{ item }}_vars.yml
        name: dep_{{ item }}_vars
      loop: "{{ departments }}"
    - name: Print Department Vars
      ansible.builtin.debug:
        var: lookup('ansible.builtin.vars', 'dep_' + item + '_vars')
      loop: "{{ departments }}"

```

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 3, 2024, 6:13pm UTC](https://forum.ansible.com/t/how-to-repeat-a-set-of-tasks-for-each-var-file-and-flush-the-previous-hashed-vars/8807/3 "2024-10-03T18:13:15Z")

</div>

I think you should

> [@w0nderw0lf](#):
>
> …change the dict’s to have a unique key and adjust my tasks?

because variables are meant to be overwritten according to this [precedence](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable) in Ansible.
