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

77 views
Skip to first unread message

ishan jain

unread,
Jul 11, 2017, 10:48:39 AM7/11/17
to Ansible Project
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
---
- hosts: "{{hostList1}}"
  tasks:
     - debug: msg="works"


Error:
ERROR! the field 'hosts' has an invalid value, which appears to include a variable that is undefined. The error was: 'hostList1' is undefined

The error appears to have been in '/home/ishan/playbook.yaml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Executing playbook
  ^ here



But if i explicitly load the same file, it works just fine:

playbook.yaml
---
- name: Executing playbook
  hosts: "{{hostList1}}"
  vars_files:
        - "{{ inventory_dir ~ '/group_vars/all/targets.yaml'}}"
  tasks:
     - debug: msg="works"


Clearly, Ansible is loading/resolving host list too fast. I really don't want to explicitly load the vars file. I need that to be in inventory and loading it again is not a good thing. How can i make sure that some vars are loaded with the inventory first thing when i execute a playbook ? 
Please note that there might be some typos when i am specifying files and code above, but this is really not a problem of typos.

Brian Coca

unread,
Jul 11, 2017, 1:27:06 PM7/11/17
to Ansible Project
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.


----------
Brian Coca

ishan jain

unread,
Jul 12, 2017, 3:17:08 AM7/12/17
to Ansible Project
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.

Brian Coca

unread,
Jul 12, 2017, 12:22:13 PM7/12/17
to Ansible Project
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.

----------
Brian Coca

chafik bel

unread,
Jul 13, 2017, 1:59:59 PM7/13/17
to Ansible Project
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].

Brian Coca

unread,
Jul 13, 2017, 7:07:07 PM7/13/17
to Ansible Project
>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

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages