Roles and playbooks in subdirectories

2,255 views
Skip to first unread message

martin f krafft

unread,
Jun 21, 2013, 10:54:43 AM6/21/13
to ansible project list
Dear list,

one thing that is really nice about Ansible is how the playbook
becomes the centre of execution, and if you run

ansible-playbook /path/to/my/playbook

then that directory is where Ansible will also look for templates
required in the tasks of this playbook. Apparently, you can also now
define group_vars/host_vars next to the playbook, so it's a big step
towards self-containment.

Enter roles, reusable stuff to be used from playbooks. Roles are
looked for in the subdirectory roles/ in the parent directory of the
playbook, or you can specify a relative path to the directory of
another playbook, or you can specify an absolute path to a role
directory per se.

Now, what is the best approach if I want to use the same role from
both, playbook1/foo.yml and playbook2/bar.yml?

I'd like to avoid hard-coding absolute paths, as people clone from
Git to their own locations;

I don't even want to assume that playbook1 and playbook2 are in the
same place on the filesystem;

And I'd rather not put one copy of a role into playbook1/ and
another copy into playbook2/.

Is there another way?

If not, I could imagine two solutions:

1. if a role is not found in the usual location, see if a roles/
directory next to the inventory has it;

2. provide a configuration option and maybe environment variable
with which to specify a roles search path. This would be
especially useful if distros started providing roles somewhere
in /usr/share, encouraging people to use them.

The two options can also obviously be combined.

What do you think?

--
martin | http://madduck.net/ | http://two.sentenc.es/

"it usually takes more than three weeks
to prepare a good impromptu speech.
-- mark twain

spamtraps: madduc...@madduck.net
digital_signature_gpg.asc

GW

unread,
Jun 21, 2013, 12:24:10 PM6/21/13
to ansible project list

Hi,

Another way:

- create symlinks (either for whole roles directory or just specific roles inside it) (it is not nice, but it could get your job done for now)

A configuration option and environment variable t specify a list of paths where to search for roles will probably be needed someday.

 

I am wondering if it is possible to create a generic path lookup function that could be used all over the place and always behave in the expected way/order. A function that would use the same approach everywhere:

- locate the role: first check if absolute path, than check in "roles" dir relative to playbook (also recurse through all parents), check in "roles" dir relative to inventory file, check in "roles" dir relative to all paths in configuration option or environment variable, as last resort check in "roles" dir relative to current dir, than repeat everything without "roles" dir

- same could be used for locating the module library and similar (replace "roles" with "library" in above)

- same could also be used for locating group_vars/host_vars, although only relative to main playbook or inventory file should be encouraged

- locate templates in template module, files in file module or file lookup plugin: first check if absolute path, than if lookup plugin used inside a template check relative to the template (?), than if inside a role check relative to its "templates"/"files" folder, than check in "templates"/"files" folder next to playbooks (recurse parents), check in "templates"/"files" folder next to the inventory file, check in "templates"/"files" relative to all paths in configuration option or environment variable, as last resort check in "templates"/"files" dir relative to current dir

To me it seems that using the same approach everywhere would be possible, or not?

Greetings,
   gw

Michael DeHaan

unread,
Jun 22, 2013, 1:28:56 PM6/22/13
to ansible...@googlegroups.com
 just path the role where it lies.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

martin f krafft

unread,
Jun 22, 2013, 1:47:37 PM6/22/13
to ansible...@googlegroups.com
also sprach Michael DeHaan <mic...@ansibleworks.com> [2013.06.22.1928 +0200]:
> just path the role where it lies.

If you are suggesting absolute paths, then how do you propose to do
team maintenance, where user 'bob' checks out to /home/bob/ansible
and user 'alice' has her stuff in
/srv/users/a/alice/sysadmin/ansible/site1 ?
"getting a scsi chain working is perfectly simple if you remember that
there must be exactly three terminations: one on one end of the
cable, one on the far end, and the goat, terminated over the scsi
chain with a silver-handled knife whilst burning *black* candles."
-- anthony deboer

spamtraps: madduc...@madduck.net
digital_signature_gpg.asc

Michael DeHaan

unread,
Jun 22, 2013, 2:13:59 PM6/22/13
to ansible...@googlegroups.com
I am not suggesting absolute paths.
Reply all
Reply to author
Forward
0 new messages