S
S
SwoDs2018-11-29 12:46:57
Ansible
SwoDs, 2018-11-29 12:46:57

How to access host variables in Ansible?

I have a group with a host

[app_admin]
adm-01.local

To access host variables that are stored in the host_vars folder , I can use this method: hostvars [groups["app_admin"][0]]["some_var"]
host, and 5, where guarantees that they will follow the order as in the inventory file.
Actually the question is: is it possible to somehow access host variables through some kind of alias or some other, more correct, way

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SwoDs, 2018-11-29
@SwoDs

Everything turned out to be simple:
in the inventory file we set:

[app_admin]
adm_01 ansible_host=adm-01.local

or you can just:
[app_admin]
adm_01

but then you need to write in host_vars in adm_01.yml file : ansible_host : adm- 01.local

M
mureevms, 2018-11-29
@mureevms

Everything is much easier. If the variable is specified in host_vars or group_vars, then just refer to it from anywhere.
For group_vars. File group_vars/app_admin:
Ie all hosts in the app_admin group have myvar defined with the value my_value_for_app_admin_group. If you need to redefine a variable for some hosts, then you should use host_vars. File host_vars/adm-01.local:
To refer to a variable in a role, playbook or warm it is necessary like this {{ myvar }}.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question