How can we access the variables defined in host_vars/* files into playbook

31 views
Skip to first unread message

Chandramohan Reddy

unread,
Dec 18, 2017, 12:33:42 PM12/18/17
to Ansible Project






Hey there,

I am facing below error when i am trying to access the variables defined in host_vars/ansible.com.

I have defined variable name in host vars file called "ansible.com" like

---
my_company: cisco


After when i am trying to access the variables inside main.yml under tasks folder facing below error.

Can anyone support how to fix the issue.

[root@gitserver tasks]# ls

group_vars  host_vars  main.retry  main.yml

[root@gitserver tasks]# ansible-playbook main.yml

PLAY [localhost] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************
ok: [localhost]

TASK [prints host_vars] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ntp_server' is undefined\n\nThe error appears to have been in '/home/git/roles/zabbix_agent/tasks/main.yml': line 16, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n#     debug: msg=\"My Company name is {{ my_company }}\"\n   - name: prints host_vars\n     ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'ntp_server' is undefined"}
        to retry, use: --limit @/home/git/roles/zabbix_agent/tasks/main.retry

PLAY RECAP ******************************************************************************************************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=1

Brian Coca

unread,
Dec 18, 2017, 4:18:08 PM12/18/17
to Ansible Project
Your play is targeting 'localhost', but you defined the variable for
the 'ansible.com' host, either move the file to 'localhost' to match
the host name or use the hostvars[<hostname>][<varname>] method to
access variables defined in other hosts.


--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages