# ansible 2.4 api and the all.yml file

**URL:** <https://forum.ansible.com/t/ansible-2-4-api-and-the-all-yml-file/26225>\
**Category:** Ansible Developer\
**Created:** [December 7, 2017, 11:19pm UTC](https://forum.ansible.com/t/ansible-2-4-api-and-the-all-yml-file/26225 "2017-12-07T23:19:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Cuzner](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@Paul\_Cuzner](https://forum.ansible.com/u/Paul_Cuzner)\
**Post date:** [December 7, 2017, 11:19pm UTC](https://forum.ansible.com/t/ansible-2-4-api-and-the-all-yml-file/26225/1 "2017-12-07T23:19:20Z")

</div>

Hi,

I’m using ansible 2.4.1 to run a couple of playbooks behind a text UI. At the time the script starts, group\_vars/all.yml doesn’t exist - I run a dynamic playbook within the script to gather some facts to define the all.yml.

My problem is that within the same script, once the all.yml is created I attempt a static playbook, but am finding that because all.yml didn’t exist when the main ansible modules were imported the contents of the all.yml I created are ignored.

Is there a way to update the vars to include the new all.yml, so the 2nd playbook will have access to them?

I tried adding them to the variablemanager instance via extra\_vars and even \_vars\_cache but neither worked.

Any help appreciated!

PC

---

<div class="post-metadata">

**Author:** ![Paul\_Cuzner](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@Paul\_Cuzner](https://forum.ansible.com/u/Paul_Cuzner)\
**Post date:** [December 8, 2017, 6:36am UTC](https://forum.ansible.com/t/ansible-2-4-api-and-the-all-yml-file/26225/2 "2017-12-08T06:36:24Z")

</div>

Some further testing

If I switch the playbook to one that simple dumps all vars on a host, I can see that

- when I start without any yml files in group\_vars, and run my script, the dump on each host contains the vars set by the yml files created by the script…apart from the content in all.yml
- when I start with all.yml already in place, the dump shows all variables

Is group\_vars/all.yml perhaps treated differently?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [December 8, 2017, 6:38am UTC](https://forum.ansible.com/t/ansible-2-4-api-and-the-all-yml-file/26225/3 "2017-12-08T06:38:41Z")

</div>

Have a look here:

[http://docs.ansible.com/ansible/latest/meta\_module.html](http://docs.ansible.com/ansible/latest/meta_module.html)

```auto
meta: refresh_inventory

```

is what you need.

---

<div class="post-metadata">

**Author:** ![Paul\_Cuzner](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@Paul\_Cuzner](https://forum.ansible.com/u/Paul_Cuzner)\
**Post date:** [December 8, 2017, 6:41pm UTC](https://forum.ansible.com/t/ansible-2-4-api-and-the-all-yml-file/26225/4 "2017-12-08T18:41:40Z")

</div>

Hi,

I’d already tried a InventoryManager instance refresh\_inventory call within my code, which didn’t work. I added the meta call to a test playbook, and see the same thing i.e. the vars from all.yml are not present, but the vars from the other group\_vars are.

---

<div class="post-metadata">

**Author:** ![Ed\_C](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@Ed\_C](https://forum.ansible.com/u/Ed_C)\
**Post date:** [December 9, 2017, 9:08pm UTC](https://forum.ansible.com/t/ansible-2-4-api-and-the-all-yml-file/26225/5 "2017-12-09T21:08:10Z")

</div>

I hit a similar issue a while back and found [https://github.com/ansible/ansible/issues/24987](https://github.com/ansible/ansible/issues/24987) which explains why refresh\_inventory doesn’t cover this case.

Based on that, I ended up using include\_vars to explicitly include the variables onto the hosts after creating the yml file. E.g.

- hosts: localhost  
gather\_facts: false  
tasks:

# use stat module to determine if “{{ inventory\_dir }}/group\_vars/all.yml” exists

# create all.yml when it doesn’t

- hosts: all  
gather\_facts: false  
tasks:
- include\_vars:  
file: “{{ inventory\_dir }}/group\_vars/all.yml”  
when: hostvars[‘localhost’]… #whatever you registered in the top host so this only happens if all.yml was created

Bit of a hack, but it works. If you set up the when’s properly then it becomes idempotent and safe to include multiple times.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Paul\_Cuzner](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@Paul\_Cuzner](https://forum.ansible.com/u/Paul_Cuzner)\
**Post date:** [December 10, 2017, 11:53pm UTC](https://forum.ansible.com/t/ansible-2-4-api-and-the-all-yml-file/26225/6 "2017-12-10T23:53:47Z")

</div>

Thanks Ed.

Looks like include\_vars provides me with a workaround. For my use case, I ‘patch’ the main playbook with the additional include\_vars task, run the play, and then remove the patch after the play completes.

Thanks again!
