Variable file inclusion advice sought

28 views
Skip to first unread message

Rod Oliver

unread,
Apr 12, 2016, 12:57:35 AM4/12/16
to Ansible Project
I'm trying to create a playbook in which I have a single role though I expect this will expand with time. I want to load the variables in a file per play_host. I think I can include the variable file with the same name as the host with .yml or .yaml suffix in ./host_vars or ./group_vars/<role_name> relative to the location of the inventory. However Ansible complains that these host variable files are not found unless they are in the same directory as the main playbook and inventory file.

The variable files are in ./host_vars. The main playbook & inventory are ./site.yml & ./hosts, the role playbook is ./<role_name>/main.yml and is as below.

---
  - name: Include host variables
    include_vars: "{{ item }}.yml"
    with_items: "{{ play_hosts}}"

I suspect that the above play is not the best way of doing things as I see too many items in the task status (when I move the files to same directory as site.yml & inventory files).

Advice on the above sought :-)

Brian Coca

unread,
Apr 14, 2016, 9:49:28 AM4/14/16
to ansible...@googlegroups.com
host_vars/group_vars can be relative to inventory or to play, but variable files in host_vars/ or group_vars/ are automatically included, no need to use include_vars. 

include_vars is relative to play only, as it does not make sense to use it to include inventory host/group vars (as they are automatic).


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

Johannes Kastl

unread,
May 16, 2016, 5:06:21 AM5/16/16
to ansible...@googlegroups.com
On 12.04.16 06:31 Rod Oliver wrote:

> The variable files are in ./host_vars. The main playbook & inventory are
> ./site.yml & ./hosts, the role playbook is ./<role_name>/main.yml and is as
> below.

Shouldn't your main.yml file be in ./roles/<role name>/tasks/main.yml?

Johannes

signature.asc
Reply all
Reply to author
Forward
0 new messages