0

Could not find out why variables not being picked up in Ansible 2.3.1.0.

File structure:

.
├── 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"

Content of roles/install/tasks/main.yml:

---

- name: Debug
  debug: var=test_var

Result is:

 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   

Expect output:

test_var = This is from host_vars file
techraf
  • 64,883
  • 27
  • 193
  • 198
wzcwts521
  • 15
  • 4
  • The answer might be similar. but the title was not really clear. Thanks though! How to pass variable in Ansbile? is much better!!!! – wzcwts521 Dec 01 '17 at 01:02

1 Answers1

0
  1. Ansible is not reading group_vars/test1.yml because you don't have a group named test1.

    If you wan't to define group_vars for any group, you should name it all, so the file should be group_vars/all.yml.

  2. Ansible is not reading host_vars/test1 because you don't have a host named test1.

    If you wan't to define host_vars for your localhost, you should name it localhost, so the file should be host_vars/localhost.yml.

Community
  • 1
  • 1
techraf
  • 64,883
  • 27
  • 193
  • 198