无法找出Ansible 2.3.1.0中未找到变量的原因。
文件结构:
.
├── ansible.cfg
├── group_vars
│ └── test1.yml
├── hosts
├── host_vars
│ └── test1
├── roles
│ └── install
│ └── tasks
│ └── main.yml
├── testing.retry
└── testing.yml
group_vars/test1.yml
:
---
test_var: "This is from host_vars file"
content of host_vars/test1
:
---
test_var: "This is from host_vars file"
roles/install/tasks/main.yml
的内容:
---
- name: Debug
debug: var=test_var
结果是:
ansible-playbook -i hosts testing.yml
PLAY [This is testing] *****************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [localhost]
TASK [install : Debug] *****************************************************************************************************************************************************
ok: [localhost] => {
"test_var": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP *****************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
期待输出:
test_var = This is from host_vars file
答案 0 :(得分:0)
Ansible没有阅读group_vars/test1.yml
,因为您没有名为test1
的群组。
如果您不想为任何群组定义group_vars,则应将其命名为all
,因此该文件应为group_vars/all.yml
。
Ansible未阅读host_vars/test1
,因为您没有名为test1
的主持人。
如果您不想为localhost定义host_vars,则应将其命名为localhost
,因此该文件应为host_vars/localhost.yml
。