Role lookup

tl;dr - Is there a reason that roles are looked for relative to the current location?

We have a site.yml playbook that includes a bunch of other playbooks (one for each role), but this clutters our root directory. I was hoping that I could just move the playbooks to a “playbooks” directory and then include them from site.yml with their new path. However, the include seems to move the cwd to the playbooks directory instead of being at the root. Thusly, the roles can no longer be found.

Is there a good reason for roles specified in playbooks to not be looked up relative to the root or at least the cwd of the ansible-playbook invocation?

Shouldn’t be the case.

Roles are looked for relative to the playbook basedir, which shouldn’t require cluttering up your root directory at all.

The roles path is also configurable in ansible.cfg as of 1.4

I don’t think it’s appropriate to consider your current cwd, as you might be sitting around in /tmp, or /root, or anywhere, relative to the playbook is a good default, if not, it’s definitely configurable in ansible.cfg.

Okay, so maybe this is a bug then. I’ve put together a toy and get the output shown:
http://pastebin.com/ir1C1VNQ

If this is not the expected output, I’ll file a bug. Thanks.

ansible-playbook 1.5 (devel 332d3d6a0e) last updated 2014/01/14 18:18:47 (GMT -700)

It’s relative to your top level playbook, so if site.yml isn’t in playbooks that would explain it (i.e. not a bug), and you can just move roles up a level.

site.yml
playbooks/*
roles/*

etc.

Though otherwise you should probably configure your roles path, which is also helpful when working with, say, Galaxy, like have your roles regularly checked out in /opt/roles or /serv/roles, etc.

It’s relative to your top level playbook, so if site.yml isn’t in playbooks that would explain it (i.e. not a bug), and you can just move roles up a level.

site.yml
playbooks/*
roles/*

etc.

This is exactly what we have. If I am following correctly, what I am doing should work, correct?

It appears I wasn’t remembering the code correctly.

Just ran a test of this and it does look like it’s looking relative to the playbook files that get included.

That being said, we must not change this behavior, but it should probably also look in the location of site.yml

Our standard practice is to actually have all the playbooks at top level and then have a roles subdirectory.

I don’t consider this a bug more so “the way it works”, but I’m not against it looking in additional locations.

Cool. I just wanted to determine bug/not bug. I’ll submit a feature request for this.

Thanks.

For the record, the reason it looks relative to the included file is such that you can have a top level site.yml and subfiles in a playbooks subdirectory, and it can still find them.

In your proposal, where you have site.yml at the TOP level, and playbooks in a subdir, if you invoked site.yml, and it only looked relative to the first, it wouldn’t find them.

Thus, you can technically do “playbooks/roles/*” now, and will be totally happy, provided you have nothing in that top level playbook looking for things.

I think I’d still recommend roles path in that case.

I guess while we are on the topic, can you explain why an include directive doesn’t just include the file contents within the current scope? I suppose, however, that Python works the way ansible does.

“can you explain why an include directive doesn’t just include the file contents within the current scope?”

IMHO there’s nothing scope related here per se, i.e. variable related, but rather just a question of whether ansible moves the basedir.

And we’re using the basedir of the included file so that relative paths work.

What the above is proposing is that roles path uses a bit more additionally than just that.

Okay. That makes sense. Thanks.