I want to define the target hosts for my playbooks somewhere in the inventory itself. But apparently, Ansible is resolving target hosts before it is finished loading the inventory. When i ‘include’ the file with these vars, it works just fine.
inventoryFile.yaml
`
[machines]
one ansible_host=000
two ansible_host=000
three ansible_host=000
`
group_vars/all/targets.yaml
`
hostList1:
- one
- two
hostList2:
- two
- three
`
playbook.yaml
`
The 'hostList1' inventory variable is not available until AFTER the
hosts: is processed, this is a chicken/egg issue.
Inventory variables are flattened per host, so only once you have a
host can you query the value of the variable.
`hosts:` defines which hosts are available, as such it cannot depend
on a host nor a variable associated to a host.
So is there no way to define something like ‘global’ variables along with the inventory ? I thought the implicit ‘all’ group can be treated like global variables.
Why don’t you simply use the power of the inventory file itself:
[group:children]
child1
child2
[child1]
host1
host2
[child2]
host3
host4
and use global vars or per host vars in the appropriate files?
N.B: the inventory is an “ini” file not a “yaml” one [inventoryFile.yaml].
No, inventory vars are always flattened to the host, 'all' is a way to
assign a variable to all hosts, but it is still 'host level'.
Use vars/vars_files or extra_vars instead.
N.B: the inventory is an "ini" file not a "yaml" one [inventoryFile.yaml].
it can be either, Ansible support YAML format for inventory also