How to use group_vars from a relative path in Ansible

Viewed 1214

I have a requirement where i need to use the group_vars in Ansible but it does not need to be on the same path. Basically my playbooks are in SVN and whenever we want to deploy we actually checkout the latest playbook with release version and then deploy.

Playbook which actually executes the deployment has this structure -

/home/ansible/myapp/ansible-myapp-release/deploy/ansible-myapp-5.80.0-05
 - bin
 - roles
 - myapp-main.yml
 - myapp-full-deployment.yml
 - myapp-config.yml

The other thing which i checkout from SVN is this which contains the configuration for my application and it does not contain any plays and it could be different for different environments like preprod, prod, test etc.

/home/ansible/myapp/ansible-myapp-release/configuration/5.80.0-05/preprod
 - config
 - group_vars
    - preprod.yml
 - inventory
     - myapp

inventory myapp -

[preprod]
myapp-preprod

Now when i execute my first playbook I want to use this group_vars because that's where my configuration is in preprod.yml which will be loaded into jinja templates at run time.

How can i use this inventory and group_vars from this path OR do i need to copy these folders from here to the original playbook root directory and then execute the playbook?

Please let me know if someone can advice on this.

The documentation says the path should be relative to where your inventory is stored https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables

1 Answers

Quoting from Organizing host and group variables

Ansible loads host and group variable files by searching paths relative to the inventory file or the playbook file

Below is the example of how to put the group_vars relative to inventory file

shell> tree /home/ansible/myapp/ansible-myapp-release/configuration/5.80.0-05/preprod
/home/ansible/myapp/ansible-myapp-release/configuration/5.80.0-05/preprod
└── inventory
    ├── group_vars
    │   └── preprod.yml
    └── myapp
Related