tarky
tarky

Reputation: 481

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.

Upvotes: 19

Views: 27182

Answers (2)

Omer
Omer

Reputation: 9

In order for the host_var and grop_var to work they should be in same dir as your inventory. You can follow either of the below setup:

ansible/ ├── ansible.cfg
├── inventory/
| inventory
│ ├── development
│ └── production | host_vars/
│ ├── 192.168.100.34.yml │ └── 192.168.100.35.yml l ├── site.yml ├── roles/
└── ...

OR

ansible/ ├── ansible.cfg
├── inventory inventory/
│ ├── development
│ └── production
├── playbbok/site.yml ├── roles/
└── ...

Upvotes: 0

ProfHase85
ProfHase85

Reputation: 12173

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.

Upvotes: 25

Related Questions