feat(vars): load group vars dynamically if files exist

This commit is contained in:
Bertrand Lanson 2024-01-02 23:25:59 +01:00
parent 34bb4bd7ad
commit f78ea02bdd
2 changed files with 47 additions and 27 deletions

View File

@ -9,6 +9,10 @@
ansible.builtin.debug:
msg: "{{ hashi_vault_configuration.listener }}"
- name: "Import variables"
ansible.builtin.import_tasks:
file: load_vars.yml
- name: "Vault"
when: enable_vault | bool
when: "'vault_servers' in group_names"

View File

@ -1,33 +1,49 @@
---
# hashistack variable injection playbook
- name: "Deploy"
hosts: all
gather_facts: true
become: true
tasks:
- name: "Load global variables"
block:
- name: "Stat global configuration file"
ansible.builtin.stat:
path: "{{ configuration_directory }}/{{ configuration_global_vars_file }}"
register: _global_config_file
delegate_to: localhost
- name: "Make sure global configuration file exists"
ansible.builtin.assert:
that:
- _global_config_file.stat.exists
delegate_to: localhost
- name: "Load global variables"
ansible.builtin.include_vars:
dir: "{{ configuration_directory }}"
files_matching: "{{ configuration_global_vars_file }}"
delegate_to: localhost
- name: "Load group specific variables"
ansible.builtin.include_vars:
dir: ""
files_matching: ""
block:
- name: "Stat group specific config file"
ansible.builtin.stat:
path: "{{ configuration_directory }}/{{ item }}/{{ configuration_global_vars_file }}"
register: _group_config_file
loop: "{{ group_names }}"
delegate_to: localhost
- name: "Load host specific variables"
- name: "debug"
ansible.builtin.debug:
msg: "{{ _group_config_file }}"
- name: Load group specific variables
ansible.builtin.include_vars:
dir: ""
files_matching: ""
dir: "{{ configuration_directory }}/{{ item.item }}"
files_matching: "{{ configuration_global_vars_file }}"
loop: "{{ _group_config_file.results }}"
when: item.stat.exists
delegate_to: localhost
# - name: "Load host specific variables"
# ansible.builtin.include_vars:
# dir: ""
# files_matching: ""
- fail: