# One playbook can load vars files which is updated by another playbook in runtime?

**URL:** <https://forum.ansible.com/t/one-playbook-can-load-vars-files-which-is-updated-by-another-playbook-in-runtime/12657>\
**Category:** Ansible Project\
**Created:** [April 10, 2013, 6:13am UTC](https://forum.ansible.com/t/one-playbook-can-load-vars-files-which-is-updated-by-another-playbook-in-runtime/12657 "2013-04-10T06:13:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rella.guo](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@rella.guo](https://forum.ansible.com/u/rella.guo)\
**Post date:** [April 10, 2013, 6:13am UTC](https://forum.ansible.com/t/one-playbook-can-load-vars-files-which-is-updated-by-another-playbook-in-runtime/12657/1 "2013-04-10T06:13:55Z")

</div>

Hi, I have a main.yml which include 1.yml and 2.yml.  
1.yml has one task to append a new variable in local vars file - “vars.yml”.  
2.yml uses vars file - “vars.yml” and must use the new variable which is appended by 1.yml.

I did some test. 2.yml cannot find out the new variable and it always load the original vars file.

Do you have any solution to resolve my question? Thank you!

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [April 10, 2013, 8:14am UTC](https://forum.ansible.com/t/one-playbook-can-load-vars-files-which-is-updated-by-another-playbook-in-runtime/12657/2 "2013-04-10T08:14:45Z")

</div>

I am not sure if dynamically updating vars file, would take effect in the same playbook, probably you can use ‘register’ to add this dynamic variable, and later down access it using hostvars.

Benno

---

<div class="post-metadata">

**Author:** ![rella.guo](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@rella.guo](https://forum.ansible.com/u/rella.guo)\
**Post date:** [April 11, 2013, 8:16am UTC](https://forum.ansible.com/t/one-playbook-can-load-vars-files-which-is-updated-by-another-playbook-in-runtime/12657/3 "2013-04-11T08:16:44Z")

</div>

But how to use hostvars in playbook?

I wrote:

- action: shell echo -n $hostvars[‘localhost.localdomain’].message  
register: msg
- action: template src=template.conf dest=/root/out.conf owner=root group=root mode=0644

The out.conf content is not value of variable message.

If I wrote:

- action: shell echo -n ${hostvars[‘localhost.localdomain’].message}

It showed syntax error: invalid arithmetic operator (error token is “.localdomain”)

---

<div class="post-metadata">

**Author:** ![rella.guo](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@rella.guo](https://forum.ansible.com/u/rella.guo)\
**Post date:** [April 11, 2013, 9:28am UTC](https://forum.ansible.com/t/one-playbook-can-load-vars-files-which-is-updated-by-another-playbook-in-runtime/12657/4 "2013-04-11T09:28:38Z")

</div>

action: shell echo -n ${hostvars.{localhost.localdomain}.message}  
shows:

stderr: /bin/sh: ${hostvars.{localhost.localdomain}.message}: bad substitution

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 11, 2013, 2:10pm UTC](https://forum.ansible.com/t/one-playbook-can-load-vars-files-which-is-updated-by-another-playbook-in-runtime/12657/5 "2013-04-11T14:10:34Z")

</div>

Playbooks should not be dynamically editing yaml variable files.

Do use register, we have also theorized including a “set\_variable” action plugin for adding new variables if need be, but in most cases register is all you need.
