# \[Issue\] Update variable in vars\_file and call it in a playbook doesn't work

**URL:** <https://forum.ansible.com/t/issue-update-variable-in-vars-file-and-call-it-in-a-playbook-doesnt-work/30333>\
**Category:** Ansible Project\
**Created:** [May 15, 2019, 7:47am UTC](https://forum.ansible.com/t/issue-update-variable-in-vars-file-and-call-it-in-a-playbook-doesnt-work/30333 "2019-05-15T07:47:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tran\_Khang](https://avatars.discourse-cdn.com/v4/letter/t/bbce88/32.png) [@Tran\_Khang](https://forum.ansible.com/u/Tran_Khang)\
**Post date:** [May 15, 2019, 7:47am UTC](https://forum.ansible.com/t/issue-update-variable-in-vars-file-and-call-it-in-a-playbook-doesnt-work/30333/1 "2019-05-15T07:47:32Z")

</div>

Hi all,

Please see my playbook below. I call 2 executions on different hosts in 1 playbook.

The first execution changes some variables in a vars file which would be used in next execution (create new backup folder and update its name to another vars file)

The second execution try to use 2 vars file to do backup tasks (using roles). The issue I met here is the vars file value were loaded with old value not the updated. Seem all defined vars files were loaded before tasks executed on all hosts, and update during running task doesn’t effect.

I can separate this playbook into 2 playbooks and all work well as expected but I’m trying to combine 1 playbook. Do anyone can show me how to reload vars file in correctly executing tasks/roles ? I exactly want the second execution load the updated vars file before running roles.

## backup playbook

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 15, 2019, 12:52pm UTC](https://forum.ansible.com/t/issue-update-variable-in-vars-file-and-call-it-in-a-playbook-doesnt-work/30333/2 "2019-05-15T12:52:12Z")

</div>

> Hi all,
> 
> Please see my playbook below. I call 2 executions on different hosts in 1  
> playbook.

Just to get the terminology correct, it's 2 plays in 1 playbook.

> The first execution changes some variables in a vars file which would be  
> used in next execution (create new backup folder and update its name to  
> another vars file)
> 
> The second execution try to use 2 vars file to do backup tasks (using  
> roles). The issue I met here is the vars file value were loaded with old  
> value not the updated. Seem all defined vars files were loaded before tasks  
> executed on all hosts, and update during running task doesn't effect.
> 
> I can separate this playbook into 2 playbooks and all work well as expected  
> but I'm trying to combine 1 playbook. Do anyone can show me how to reload  
> vars file in correctly executing tasks/roles ? I exactly want the second  
> execution load the updated vars file before running roles.

Instead of using vars\_files use the module include\_vars, inlude\_vars load the file dynamically  
[https://docs.ansible.com/ansible/latest/modules/include\_vars\_module.html](https://docs.ansible.com/ansible/latest/modules/include_vars_module.html)

Add the include\_vars in the pre\_tasks: so it runs before roles is run.

---

<div class="post-metadata">

**Author:** ![Tran\_Khang](https://avatars.discourse-cdn.com/v4/letter/t/bbce88/32.png) [@Tran\_Khang](https://forum.ansible.com/u/Tran_Khang)\
**Post date:** [May 16, 2019, 3:10am UTC](https://forum.ansible.com/t/issue-update-variable-in-vars-file-and-call-it-in-a-playbook-doesnt-work/30333/3 "2019-05-16T03:10:01Z")

</div>

> Instead of using vars\_files use the module include\_vars, inlude\_vars  
> load the file dynamically  
> [https://docs.ansible.com/ansible/latest/modules/include\_vars\_module.html](https://docs.ansible.com/ansible/latest/modules/include_vars_module.html)
> 
> Add the include\_vars in the pre\_tasks: so it runs before roles is run.
> 
> –  
> Kai Stian Olstad

Thanks for suggest and I did successful by using include\_vars in roles’s tasks.  
You are rock!
