【发布时间】:2021-03-27 02:15:30
【问题描述】:
我正在使用最新版本的 Ansible,我正在尝试在 role-one 中使用主机 one 上的默认变量,在 role-two 中,在主机 two 上使用,但我不能让它工作。
我在文档或 StackOverflow 上发现的任何内容都没有真正的帮助。我不确定我做错了什么。理想情况下,我想设置一次变量的值,并能够在我的剧本中的任何主机上以另一个角色使用它。
我在下面把它分解了。
在我的清单中,我有一个名为 [test] 的主机组,其中有两个主机别名为 one 和 two。
[test]
one ansible_host=10.0.1.10 ansible_connection=ssh ansible_user=centos ansible_ssh_private_key_file=<path_to_key>
two ansible_host=10.0.1.20 ansible_connection=ssh ansible_user=centos ansible_ssh_private_key_file=<path_to_key>
我有一个单个剧本,其中每个主机都有一个播放,我为主机one 和@987654332 提供hosts: 值作为"{{ host_group }}[0]" @代表主机two。
主持人one 使用一个名为role-one 的角色,而主持人two 使用一个名为role-two 的角色。
- name: Test Sharing Role Variables
hosts: "{{ host_group }}[0]"
roles:
- ../../ansible-roles/role-one
- name: Test Sharing Role Variables
hosts: "{{ host_group }}[1]"
roles:
- ../../ansible-roles/role-two
在role-one我设置了一个变量variable-one。
---
# defaults file for role-one
variable_one: Role One Variable
我想在role-two 的模板中使用variable_one 的值,但我没有运气。我在role-two 中使用以下作为任务来测试变量是否被“拾取”。
---
# tasks file for role-two
- debug:
msg: "{{ variable_one }}"
当我使用 ansible-playbook test.yml --extra-vars "host_group=test" 运行剧本时,出现以下故障。
TASK [../../ansible-roles/role-two : debug] ***********************************************************************************************************************************************************************************************
fatal: [two]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['test']\" is undefined\n\nThe error appears to be in 'ansible-roles/role-two/tasks/main.yml': line 3, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# tasks file for role-two\n- debug:\n ^ here\n"}
【问题讨论】:
标签: ansible