From f78ea02bdde693a8fc7882827c5e8b0fc2b7b10f Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Tue, 2 Jan 2024 23:25:59 +0100 Subject: [PATCH] feat(vars): load group vars dynamically if files exist --- playbooks/deploy.yml | 4 +++ playbooks/load_vars.yml | 70 +++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/playbooks/deploy.yml b/playbooks/deploy.yml index ce1f556..e6f06bd 100644 --- a/playbooks/deploy.yml +++ b/playbooks/deploy.yml @@ -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" diff --git a/playbooks/load_vars.yml b/playbooks/load_vars.yml index 61ea9eb..56caadf 100644 --- a/playbooks/load_vars.yml +++ b/playbooks/load_vars.yml @@ -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" - block: - - name: "Stat global configuration file" - ansible.builtin.stat: - path: "{{ configuration_directory }}/{{ configuration_global_vars_file }}" - register: _global_config_file - - - name: "Make sure global configuration file exists" - ansible.builtin.assert: - that: - - _global_config_file.stat.exists - - - name: "Load global variables" - ansible.builtin.include_vars: - dir: "{{ configuration_directory }}" - files_matching: "{{ configuration_global_vars_file }}" - - - name: "Load group specific variables" ansible.builtin.include_vars: - dir: "" - files_matching: "" + dir: "{{ configuration_directory }}" + files_matching: "{{ configuration_global_vars_file }}" + delegate_to: localhost - - name: "Load host specific variables" +- name: "Load group specific variables" + 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: "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: \ No newline at end of file