# How to load external group\\host\_vars into playbook.

**URL:** <https://forum.ansible.com/t/how-to-load-external-group-host-vars-into-playbook/44971>\
**Category:** Get Help\
**Tags:** variables\
**Created:** [December 8, 2025, 4:41pm UTC](https://forum.ansible.com/t/how-to-load-external-group-host-vars-into-playbook/44971 "2025-12-08T16:41:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew\_lovegreen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matthew_lovegreen/32/9925_2.png) [@matthew\_lovegreen](https://forum.ansible.com/u/matthew_lovegreen)\
**Post date:** [December 8, 2025, 4:41pm UTC](https://forum.ansible.com/t/how-to-load-external-group-host-vars-into-playbook/44971/1 "2025-12-08T16:41:43Z")

</div>

Hello community,  
Need your advice. Before ansible 2.16 we were able to load host\_vars and group\_vars so to say “dynamically” on the fly from external repository. The idea here was to store playbooks and variables separately from each other, due to some requirements.

Here is an example:

```auto
- hosts: alerta
  pre_tasks:
  - name: Fetch vars
    local_action:
      module: git
      repo: https://{{ gitlab_ansible_user | urlencode }}:{{ gitlab_ansible_pass }}@gitlab.example.com/ansible/vars/alerta.git
      dest: /etc/ansible/gitlabvars/alerta
    run_once: true
    no_log: true
    changed_when: false
  - name: Copy group_vars to playbook root
    local_action:
      module: copy
      src: /etc/ansible/gitlabvars/alerta/group_vars
      dest: ./
  roles:
    - alerta

```

The structure of alerta vars repository ([gitlab.example.com/ansible/vars/alerta.git](http://gitlab.example.com/ansible/vars/alerta.git)) is following

```auto
/vars/alerta/group_vars$ tree 
.
├── alerta_dev.yml
├── alerta_prod.yml
├── alerta_rc.yml
└── alerta.yml

```

Unfortunately since 2.16 that type of setup is no longer valid. According to release notes, host\_vars and group\_vars currently are loaded only single time when playbook is started, so host\_vars and group\_vars must be already be in place when playbook is launched.

Have anyone faced something similar to this? Are there any traditional ansible way of loading these variables in to the playbook ?  
p.s include\_vars has some drawbacks that will require you to create logic related to precedence of variables.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 8, 2025, 5:13pm UTC](https://forum.ansible.com/t/how-to-load-external-group-host-vars-into-playbook/44971/2 "2025-12-08T17:13:14Z")

</div>

I would suggest making your pre-tasks a separate playbook to get the group files in place, then as a separate play in that playbook, use `import_playbook` to invoke the playbook that calls your `alerta` role.  
See `ansible-doc -t module ansible.builtin.import_playbook`, and do let us know how that works for you.

Good luck!

---

<div class="post-metadata">

**Author:** ![matthew\_lovegreen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matthew_lovegreen/32/9925_2.png) [@matthew\_lovegreen](https://forum.ansible.com/u/matthew_lovegreen)\
**Post date:** [December 8, 2025, 8:42pm UTC](https://forum.ansible.com/t/how-to-load-external-group-host-vars-into-playbook/44971/3 "2025-12-08T20:42:03Z")

</div>

Hey!  
Thanks! It seemed at the first glance that it should do the trick indeed, but unfortunately no luck for now.

```auto
ansible [core 2.19.3]

```

Created simple test playbook:  
ansible-playbook prepare\_env.yaml --inventory ../hosts

```auto
#prepare_env.yaml
- name: Prepare env
  hosts: localhost
  gather_facts: true
  tasks:
  - name: copy vars
    ansible.builtin.copy:
       src: vars/
       dest: ./
- ansible.builtin.import_playbook: set_facts_debug.yaml

```

and

```auto
---
#set_facts_debug.yaml
- name: Example playbook to set facts and show debug messages
  hosts: localhost
  gather_facts: true
  tasks:
    - name: "Debug: show application name"
      debug:
        msg: "Application name is: {{ app_name }}"

    - name: "Debug: show full facts in structured output"
      debug:
        var: app_version

```

```auto
/vars$ tree
.
└── group_vars
    ├── demo
    │ └── demo.yaml
    ├── demo_abc
    │ └── demo_abc.yaml
    ├── demo_dv
    │ └── demo_dv.yaml

```

Imported playbook is unaware of variables that were copied in the parent playbook, so it fails.  
p.s. on the second run, when vars are already in place, all works good.

So I am just thinking now is our setup anti-pattern or that’s a bug in ansible ?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 8, 2025, 9:50pm UTC](https://forum.ansible.com/t/how-to-load-external-group-host-vars-into-playbook/44971/4 "2025-12-08T21:50:30Z")

</div>

> [@matthew\_lovegreen](#):
>
> So I am just thinking now is our setup anti-pattern or that’s a bug in ansible ?

Perhaps a little bit of both. (?) It obviously isn’t a common way to do things.

Now I’m wondering about the nature of the data. The mechanism that keeps host and group data separate from the Ansible projects that uses them is host facts. Could you add enough Ansible to the data repos to push them out to hosts as facts? Then your “real” playbooks could pull them in with normal fact gathering. That would allow the data project’s modification patterns to be quite different from that of the code project(s).

To be honest, though, that difference of pacing is all I’ve been able to imagine as an advantage for this scheme. I only have our own projects to go by, but my feeling is that the structure of the data and the code that uses them is so thoroughly intertwingled that separating them into separate projects seems borderline questionable.

One more thought that may allow you to keep using them as you have is to use _git submodules_ — put your data project inside your code project at the proper place, but keep them as separate repos. I’ve never done it, but it seems like it might work.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 9, 2025, 9:15pm UTC](https://forum.ansible.com/t/how-to-load-external-group-host-vars-into-playbook/44971/5 "2025-12-09T21:15:20Z")

</div>

According to some comments I’ve seen since writing the above, apparently there are “issues” (?) getting _git submodules_ working reliably under AWX / AAP. If you intend to run this on AWX, that should be tested thoroughly first.
