I have ansible inventory with below entries in it.
[local]
localhost port=80
[apacheweb]
kputtegowda2c.mylabserver.com
[appserver]
kputtegowda3c.mylabserver.com
Now, if I want to access the host variable "port" of the local group. I have a simple playbook to display the variable value.
- hosts: local
gather_facts: yes
tasks:
- debug: var={{ port }}
the above sample produces the output.
TASK [debug] *************
ok: [localhost] => {
"80": "VARIABLE IS NOT DEFINED!"
}
instead of directly accessing the port variable if I use hostvars[inventory_hostname].port I get the output as below.
TASK [debug] ****************
ok: [localhost] => {
"hostvars[inventory_hostname].port": "80"
}