简体   繁体   English

在 ansible-playbook 我得到一个错误 'foo' is undefined running this playbook

[英]In ansible-playbook I'm getting an error 'foo' is undefined running this playbook

Any ideas why this happens?任何想法为什么会发生这种情况?

undefined variable - The error was: 'foo' is undefined.未定义的变量 - 错误是:“foo”未定义。 I've tried using test.vars in yaml (test.yml) also.我也尝试在 yaml (test.yml) 中使用 test.vars。

test.yml测试.yml

---
- hosts: all
  roles:
    - test

roles/test/tasks/main.yml角色/测试/任务/main.yml

- name: test
  debug:
    msg: "{{ foo }}"

group_vars/test.vars group_vars/test.vars

foo=bar

inventory存货

[all]
localhost
ansible-playbook -i inventory --extra-vars group_vars/@test.vars test.yml

They're called group_vars because they're related to the hosts group name.它们被称为 group_vars 因为它们与主机组名称相关。

  • Instead of [all] the group name should be [test] in the inventory file and in the main playbook.在清单文件和主剧本中,组名应该是[test]而不是[all]
  • The variables file name should be equivalent to the hosts group name and in YAML form test.yml变量文件名应等同于主机组名称,并采用 YAML 格式test.yml
  • The variables should be written in YAML foo: bar变量应该写在 YAML foo: bar

After these changes, there is no need to pass the file on the command line, because ansible will detect it automatically:做完这些改动后,就不需要在命令行传递文件了,因为ansible会自动检测:

ansible-playbook -i inventory test.yml

I found the answer is moving the @ outside, so;我发现答案是将@移到外面,所以; it looks like this:它看起来像这样:

--extra-vars @group_vars/test.vars

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM