如何在Ansbile中传递变量?

时间:2017-12-01 00:17:08

标签: ansible ansible-2.x ansible-template

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

1 个答案:

答案 0 :(得分:0)

  1. Ansible没有阅读group_vars/test1.yml,因为您没有名为test1的群组。

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

  2. Ansible未阅读host_vars/test1,因为您没有名为test1的主持人。

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