模板中的Ansible全局变量

时间:2015-09-25 17:00:01

标签: jinja2 ansible

我无法从ansible获取jinja2模板,以便能够读取group_vars/all.yml中设置的全局变量。

**我的问题是**:如何让我的j2模板能够在group_vars/all.yml中看到全局变量?根据我的理解,group_vars/all应适用于所有群体。

我的ansible项目如下:

group_vars/
  all.yml

playbooks/
  roles/
    some-role/
      tasks/
        main.yml
      templates/
        template.conf.j2
  my-playbook.yml

任务/ main.yml

---
- name: do it
  template:
    src: template.conf.j2
    dest: ...

模板/ template.conf.j2

The global variable is: {{ GlobalVariable }}

group_vars / all.yml

GlobalVariable: something

当我运行playbook时,我在尝试编写该模板时遇到错误。

AnsibleUndefinedVariable: One or more undefined variables: 'GlobalVariable' is undefined

1 个答案:

答案 0 :(得分:4)

group_vars文件夹放在playbooks文件夹中,它可以解决您的问题。

类似的东西:

playbooks/
  group_vars/
    all.yml
  roles/
    some-role/
      tasks/
        main.yml
      templates/
        template.conf.j2
  my-playbook.yml

然后无论你在哪里执行ansible-playbook -i hosts my-playbook.yml命令(在剧本之外或在里面)它都应该工作

以下是关于source-code

的更多解释
   """
    Loads variables from group_vars/<groupname> and host_vars/<hostname> in directories parallel
    to the inventory base directory or in the same directory as the playbook.  Variables in the playbook
    dir will win over the inventory dir if files are in both.
    """
相关问题