Load variables for a multiple environments / multiple clusters setup

Hi all,

We are implementing Ansible at our company and with the current setup we are managing multiple applications into multiple environments by loading the appropriate hosts files.

Our issues come when loading the variables files. For each project / environments we have the following variable files:

  • vars/project_1/common.yml

  • vars/project_1/development.yml

  • vars/project_1/staging.yml

  • vars/project_1/production.yml

  • vars/project_1/secret/development.yml

  • vars/project_1/secret/staging.yml

  • vars/project_1/secret/production.yml

By passing some extra vars to “ansible-playbook” command we are able to load the variable files by project.

Problem with this approach is that there is a lot of repetition in the code (vars_files) and we have wondered if there’s a better way to handle this.

# playbook-project.yml
- hosts: app
sudo: yes
vars_files:
- vars/{{ app_name }}/common.yml

- vars/{{ app_name }}/{{ app_env }}.yml

- vars/{{ app_name }}/secret/{{ app_env }}.yml
roles:
- ...

- hosts: db
sudo: yes
vars_files:
- vars/{{ app_name }}/common.yml

- vars/{{ app_name }}/{{ app_env }}.yml

- vars/{{ app_name }}/secret/{{ app_env }}.yml
roles:
- ...

Any ideas/improvements?

Thanks in advance,

Francesc