[Issue] Update variable in vars_file and call it in a playbook doesn't work

23 views
Skip to first unread message

Tran Khang

unread,
May 15, 2019, 3:47:32 AM5/15/19
to Ansible Project
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
---
- hosts: ftpserver01
  gather_facts: no
  ignore_errors: yes
  vars_files:
    - ./vars_files/lab1.yml
  roles:
    - create_define_backup_folder  <--- this role will create new backup folder and update its name in ./vars_files/global_vars.yml

- hosts: terminal02
  become: yes
  become_method: su
  gather_facts: no
  ignore_errors: yes
  vars_files:
    - ./vars_files/lab1.yml
    - ./vars_files/global_vars.yml  <-- expect run with new update from upper task but not.
  roles:
    - backup_feature01
    - backup_feature02

Kai Stian Olstad

unread,
May 15, 2019, 8:52:25 AM5/15/19
to ansible...@googlegroups.com
On 15.05.2019 09:47, Tran Khang wrote:
> 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

Add the include_vars in the pre_tasks: so it runs before roles is run.

--
Kai Stian Olstad

Tran Khang

unread,
May 15, 2019, 11:10:02 PM5/15/19
to Ansible Project

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

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!
Reply all
Reply to author
Forward
0 new messages