Playbooks in subdirectories

1,518 views
Skip to first unread message

Max Kraszewski

unread,
Mar 19, 2016, 7:27:21 PM3/19/16
to Ansible Project
Hi!
After reading the docs, dozens of group messages and several articles, I can't find the way to locate  playbooks in subdirs.
I'm having a large, complex project to deploy several and different installations in multiple hosts, and the amount of playbooks are growing up.
At this moment, everything works fine with my playbooks located at project's root folder, but I'd like a little more organization
So I decided to create a playbooks folder and move them in some subdirectories inside.
Following this repository guideline, I created an ansible.cfg file with role_path=../roles

This is my folder (re)structure

project
      
|_ inventory
      
|_ playbooks
              
|_ ansible.cfg
               |_ webservers.yml
               |_ databases.yml
       |_ roles
               |_ nginx
               |_ php
               |_ mysql
           

But when I run
> ansible-playbook -i inventory plays/webservers.yml

it throws the error

ERROR! the role 'nginx' was not found in /Users/max/Work/project/playbooks/roles:/Users/max/Work/project/playbooks:/etc/ansible/roles

It seems that the role_path is not working, and I don't have any more ideas-
So my question is: is there a way to achieve subfolder organization to playbooks?

Thanks a lot for any help!
Max


Benjamin Redling

unread,
Mar 19, 2016, 8:49:28 PM3/19/16
to ansible...@googlegroups.com
Hi Max,

On 2016-03-19 04:34, Max Kraszewski wrote:
> [...] but I'd like a little more organization
> So I decided to create a *playbooks* folder and move them in some
> subdirectories inside.

Isn't a vast number of tasks in the project's main folder a sign that
you aren't using roles enough?

> Following this repository
> <https://github.com/enginyoyen/ansible-best-practises> guideline, I created
> an ansible.cfg file with role_path=../roles

roles_path not role_path -- but I guess it's just a typo in the mail.

http://docs.ansible.com/ansible/playbooks_roles.html
suggests to use a central location for all roles across projects and use
roles_path with one (absolute) path:
"
In Ansible 1.4 and later you can configure a roles_path to search for
roles. Use this to check all of your common roles out to one location,
and share them easily between multiple playbook projects. See
Configuration file for details about how to set this up in ansible.cfg.
"

Using recommendations and best practices from ansible.com has the
advantage that on-boarding others is relatively easy. You'll have a
single source to point them to.

Regards,
Benjamin
--
FSU Jena | JULIELab.de/Staff/Benjamin+Redling.html
vox: +49 3641 9 44323 | fax: +49 3641 9 44321

Mike Biancaniello

unread,
Mar 21, 2016, 11:02:18 AM3/21/16
to Ansible Project
I agree that it's annoying, especially if you try to organize by includes and reusable playbooks, it's hard to tell, looking at a list of *.yml files which ones are the top-level and which are meant to be included.

If you're not using roles and only using tasks, you can put your tasks in a tasks/ subfolder and then include them and everything will use the dir of the playbook as the ansible root.

However, if you're using roles, the roles are assumed to be in a subdir of the playbook, so you can either create a playbooks/ subdir and reference your roles like:

./stuff.yml
---
- include: playbooks/wtfstuff.yml

playbooks/wtfstuff.yml
--
- name: Do wtf stuff

  roles
:
 
- role: ../roles/wtf

or put roles as a subdir of your playbook subdir and then require that any top-level playbooks must only include playbooks from the playbooks/ subdir:
project
├── inventory
├── site.yml
└── playbooks
   
├── ansible.cfg
   
├── webservers.yml
   
├── databases.yml
   
└── roles
       
├── nginx
       
├── php
       
└── mysql


Max Kraszewski

unread,
Mar 21, 2016, 7:21:05 PM3/21/16
to Ansible Project
Benjamin, Mike:
Thanks for your replies and your advices.
Now I know I need to reorganize a bit mi playboks and try to follow the Ansible best practices more closely.

Thanks in advance!!

Max
Reply all
Reply to author
Forward
0 new messages