# Ansible fails to execute a playbook when the value for 'hosts' is defined somewhere in inventory

**URL:** <https://forum.ansible.com/t/ansible-fails-to-execute-a-playbook-when-the-value-for-hosts-is-defined-somewhere-in-inventory/24953>\
**Category:** Ansible Project\
**Created:** [July 11, 2017, 2:48pm UTC](https://forum.ansible.com/t/ansible-fails-to-execute-a-playbook-when-the-value-for-hosts-is-defined-somewhere-in-inventory/24953 "2017-07-11T14:48:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [July 11, 2017, 2:48pm UTC](https://forum.ansible.com/t/ansible-fails-to-execute-a-playbook-when-the-value-for-hosts-is-defined-somewhere-in-inventory/24953/1 "2017-07-11T14:48:39Z")

</div>

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**

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 11, 2017, 5:26pm UTC](https://forum.ansible.com/t/ansible-fails-to-execute-a-playbook-when-the-value-for-hosts-is-defined-somewhere-in-inventory/24953/2 "2017-07-11T17:26:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [July 12, 2017, 7:17am UTC](https://forum.ansible.com/t/ansible-fails-to-execute-a-playbook-when-the-value-for-hosts-is-defined-somewhere-in-inventory/24953/3 "2017-07-12T07:17:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chafik\_bel](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@chafik\_bel](https://forum.ansible.com/u/chafik_bel)\
**Post date:** [July 12, 2017, 9:18am UTC](https://forum.ansible.com/t/ansible-fails-to-execute-a-playbook-when-the-value-for-hosts-is-defined-somewhere-in-inventory/24953/4 "2017-07-12T09:18:17Z")

</div>

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**].

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 12, 2017, 4:22pm UTC](https://forum.ansible.com/t/ansible-fails-to-execute-a-playbook-when-the-value-for-hosts-is-defined-somewhere-in-inventory/24953/5 "2017-07-12T16:22:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 13, 2017, 11:06pm UTC](https://forum.ansible.com/t/ansible-fails-to-execute-a-playbook-when-the-value-for-hosts-is-defined-somewhere-in-inventory/24953/6 "2017-07-13T23:06:54Z")

</div>

> 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
