17

we can change the path of roles by modifying roles_path in ansible.cfg. But the document doesn't seems to mention anything about changing the path of group_vars and host_vars.

How can I change those paths?

I will integrate the files related to ansible with rails app repsitory. I want to gather the roles and vars directory under single directory but leave hosts file and ansible.cfg at the top directory so that the top directory is easy to see and still I can run ansible-playbook at the top directory without moving to deep directory.

Thanks, in advance.

tarky
  • 371
  • 1
  • 2
  • 13

1 Answers1

23

You cannot change the path for host_vars nor group_vars.

Those paths are always relative to your hostfile. You can set a standard hostfile in your ansible config:

hostfile = /path/to/hostfile/hostfile.ini

in this case your default host_vars are to be found at

/path/to/hostfile/host_vars/

You might as well use multiple hostfiles, assume you got:

/path/to/your/project/inventory/inventory.ini

with the host_vars at

/path/to/your/project/inventory/host_vars/

In this case you might call ansible from anywhere using:

ansible -i /path/to/your/project/inventory/inventory.ini my_playbook.yml

Just remember: the host_vars and group_vars are related to your inventory (hostfile) and therefore you can change your inventory location and put the according host_vars below it.

ProfHase85
  • 11,763
  • 7
  • 48
  • 66
  • Thank you!@ProfHase85 I've decided to place hostfile together with group_vars in the directory one level deep And then execute `ansible-playbook` with `-i` from the top directory. – tarky Aug 19 '14 at 05:31
  • 12
    Another location (that has priority over host_vars/group_vars next to the inventory file) is in the same directory as the playbook itself. It's a shame that this location is not configurable either, because it effectively keeps you from having a proper directory tree structure for the playbook files. – um-FelixFrank Sep 08 '17 at 09:31
  • 3
    Incongruence found: "The ansible-playbook command looks for these directories in the current working directory by default. Other Ansible commands (for example, ansible, ansible-console, and so on) will only look for group_vars/ and host_vars/ in the inventory directory." I think that makes this answer misleading when you write "paths are always relative to your hostfile." (or the official documentation is wrong) Source: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html – Jonathan Komar Dec 15 '20 at 08:49