vars_files中的动态文件名

pse*_*udo 7 ansible ansible-playbook

以下是一个试图动态加载变量的简单剧本:

site.yml
---
- hosts: localhost
  vars_files:
  - "{{ name }}.yml"
  tasks:
  - debug: var={{ foo }}
Run Code Online (Sandbox Code Playgroud)

变量foo在此文件中定义:

vars/myvars.yml
---
foo: "Hello"
Run Code Online (Sandbox Code Playgroud)

然后playbook就像这样运行:

ansible-playbook test.yml -e "name=myvars"
Run Code Online (Sandbox Code Playgroud)

但是,这会导致此错误:

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

根据我从几个代码片段中理解的情况,这应该是可能的,并从myvars.yml导入变量.当尝试使用ansible 1.7.x时,它似乎确实有效(尽管我遇到了另一个问题,文件名vas正确解析).

这种行为是否已更改(可能已删除对动态变量文件的支持?).有没有不同的方法来实现这种行为(我可以使用include_vars任务,但它不是很合适)?

编辑:为了确保我的playbook结构是正确的,这里是一个github存储库:https://github.com/jcechace/ansible_sinppets/tree/master/dynamic_vars

Arb*_*zar 5

Just change your site.yml like this:

- hosts: localhost
  vars_files:
    - "vars/{{ name }}.yml"
  tasks:
    - debug: var={{ foo }}
Run Code Online (Sandbox Code Playgroud)

Then run the command like this:

ansible-playbook site.yml -e "name=myvars" -c local
Run Code Online (Sandbox Code Playgroud)

Hope this will help you.