ansible

How can I change the path of group_vars and host_vars?


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.


Solution

  • 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.