Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic file name in vars_files

The following is a simple playbook which tries to dynamically load variables:

site.yml
---
- hosts: localhost
  vars_files:
  - "{{ name }}.yml"
  tasks:
  - debug: var={{ foo }}

Variable foo is defined in this file:

vars/myvars.yml
---
foo: "Hello"

Then playbook is run like this:

ansible-playbook test.yml -e "name=myvars"

However this results in this error:

ERROR! vars file {{ name }}.yml was not found

From what I understood from several code snippets this should be possible and import the variables from myvars.yml. When trying with ansible 1.7.x it indeed seemed to work (although I hit a different issue the file name vas resolved correctly).

Was this behaviour changed (perhaps support for dynamic variable files was removed?). Is there a different way to achieve this behaviour (I can use include_vars tasks, however it is not quite suitable)?

EDIT: To make sure my playbook structure is correct, here is a github repository: https://github.com/jcechace/ansible_sinppets/tree/master/dynamic_vars

like image 835
pseudo Avatar asked Apr 14 '16 10:04

pseudo


1 Answers

Just change your site.yml like this:

- hosts: localhost
  vars_files:
    - "vars/{{ name }}.yml"
  tasks:
    - debug: var={{ foo }}

Then run the command like this:

ansible-playbook site.yml -e "name=myvars" -c local

Hope this will help you.

like image 171
Arbab Nazar Avatar answered Oct 30 '22 11:10

Arbab Nazar