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.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/219da734-69a6-42d9-8892-ea4aaa21de63%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to