0

无法找出在 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
4

1 回答 1

0
  1. Ansible 没有阅读group_vars/test1.yml,因为您没有名为test1.

    如果您不想为任何组定义 group_vars,则应将其命名为all,因此文件应为group_vars/all.yml.

  2. Ansible 没有阅读host_vars/test1,因为您没有名为test1.

    如果您不想为本地主机定义 host_vars,则应将其命名为localhost,因此文件应为host_vars/localhost.yml.

于 2017-12-01T00:32:34.363 回答