Open dbv-rl opened 1 year ago
TLDR: Running your example on a single machine (localhost
) is not going to show the testhost
data because it doesn't exist in your single node setup.
When you called the ansible-playbook
with the -i localhost,
inventory option, you told Ansible to run the playbook on a single machine, "localhost" (i.e. the machine you're running Ansible from).
Ansible then got the variable information from host_vars/localhost.yml
and that's what you see in the localhost output
play in your playbook.
The second play named testhost output
will show the values collected or provided for the host named testhost
. Since your inventory file only referenced the single system named localhost
, Ansible doesn't have any variables for testhost
and reported the "VARIABLE IS NOT DEFINED".
To help you understand this, I would suggest having three systems setup: one as the systems (ex: lab1) you run Ansible from, and the other two (ex: lab2, lab3) as test end points to execute the playbooks against. These can be small VMs running in your lab and able to communicate with each other over SSH (i.e. add lab2 and lab3 into the the /etc/hosts file on lab1). Adjust your host_vars
files to be named lab2.yml
and lab3.yml
, adjust your playbook to display the var: hostvars['lab2']
and var: hostvars['lab3']data, then re-run the playbook with
-i lab2,lab3` you'll see the results you expect.
TLDR: Running your example on a single machine (localhost) is not going to show the testhost data because it doesn't exist in your single node setup.
Ofc, my bad. I stopped thinking about the inventory as list of available hosts that would define the whole spectrum of available machines.
The code was working as expected when I ran
ansible-playbook -i localhost,testhost -l localhost -c local playbook.yml
instead. No need to make testhost actually available at all. It would have been a bit concerning to me if Ansible would have loaded all files in all cases, no matter if required or referenced at all.
Sorry for not re-building your lab-suggestion, but that solution was requiring simply less effort on my side. I'm sure it would have worked just as well.
My only comment then would be that the quoted paragraph from the book does not give the context that a machine must be defined in the inventory, in order to resolve the host_vars files as well.
Anyhow, I learned something new today that totally slipped my mind: host-variables of not-addressed hosts are available when included in an inventory.
I think it's up to geerlingguy if he thinks that paragraph could benefit from rephrasing or not.
thanks for the clarification. @dglinder
Hi there,
I have trouble understanding a paragraph around page 98 regarding magic variables with host and group variables.
I was wondering how I missed that behavior and did a small test:
If I run this with the following
ansible-playbook
-command, the variable content fortesthost.yml
is not present.