Does anyone know how to put playbooks into folders, but share the same roles, group_vars, and other stuff typically located at the root dir?
Here's what I'd like to have:
root_dir: - group_vars - roles - inventory - playbooks - my_playbook.yml - site.yml - deploy.yml
Our root dir is getting pretty big now and I'd like to split out some playbooks into their own folder (shown as playbooks/ above). An identical tiny playbook fails to run when inside a directory (say, playbooks/) vs at the root dir, because it doesn't grab stuff from group_vars.
I can partially work around this, and run a playbook inside my playbooks/
directory:
- hosts: host_group sudo: true gather_facts: false vars_files: - ../group_vars/all/main.yml
This picks up the vars defined in main.yml. However, it's not clear to me if this would add group variables defined in group_vars/, as opposed to the explicitly specificed ../group_vars/.
Thanks!
Best Answer
Ansible will pick up
group_vars
without stating path explicitly:Here is the example directory structure in
/tmp/ansible
:Inventory file:
Main Playbook
site.yml
:Secondary Playbook
foo.yml
:Group Variables
test_group.yml
:Here are all the ways to execute the secondary playbook:
From root folder using relative path
/tmp/ansible
:From Playbooks subfolder with inventory in parent path
/tmp/ansible/playbooks
:As we can see in the above examples, ansible-playbook will look for variables based on the path of
inventory file
or folder were ansible was executed from. Playbooks can be separated without additional effort.