Role directory path variable

I am wondering if there is any recommended approach for passing filename paths across roles.

As an example, take a look at https://bitbucket.org/neubyr/ansible-example/src repository. There are two roles myapp and webserver.

  • webserver role configures webserver and accepts custom configuration file as an input parameter. It will install config file and do some other tasks.
  • myapp role configures application and one of it’s step is webserver configuration. It will pass webserver configuration file path to the webserver role so that it can handle that part.

Both roles are tied together in playbook test.yml. Right now file name passed to the webserver role is relative path from the playbook file. Is there any way to make this path independent of playbook location and reference it using role directory? I’ve tried using role_path variable in role dependencies meta.yml file, but role_path is evaluated after calling dependency role so it is not helping either.

Is there any way to provide role directory variable? Or Is there any way to pass dependency calling role’s path?

  • thanks, N

Yes - if you have tasks in a role 'thingy' like this:

- name: config
  copy: src=etc/file.conf
            dest=/etc/file.conf

- name: more config
   template: src=etc/file2.conf.j2
                   dest=/etc/file2.conf

then

* the copy task will load its source file from roles/thingy/files/etc/file.conf
* the template task will load its template from
roles/thingy/templates/etc/file2.conf.j2

Thank you for the details. I am wondering how to pass file name in one role to another role as variable parameter.

As mentioned in previous post, take example of configuring webserver for a particular application. webserver setup remains same for multiple applications except configuration file. So if we develop a webserver role that accepts file path parameter, then same webserver role could be reused by other application roles. Now it makes sense to keep application specific webserver configuration file inside application role and pass it’s path to webserver role. This helps in keeping all application specific configuration files inside a single role.

If Ansible had some roles path variable or special path ‘role’, then it would make above pattern easy. So we could say something like src=ROLES/myapp/files/testfile .

It seems like that is not possible with Ansible.

  • N

Right, let me check I get the requirement -
so for example we're talking about say an 'apache' role and a 'kibana'
role that's going to need a vhost setup.

(with ansible 1.9 at least), if I set a vhost_conf_dir var in my
apache role (which is a path to a directory you can drop .vhost files
into to have them auto-included, nginx etc. has a similar feature) and
then have my kibana role list 'apache' as a dependency in its
meta/main.yml, it will be able to see that var and use it.

Another way is to explicitly set a 'global' (play wide or host wide,
point is its set external to the roles ) vhost_conf_dir and have each
role use it (that's less 'magical' so personally I prefer it, it's
easier to see what's going on).

One downside is the 'kibana' role needs handlers to restart the
webserver, which is a bit ugly.

Either way, you're going to have 2 roles that are co-dependant and not
too re-usable. That would
still be the case if you had the exact feature you describe, of course.

I'm of the opinion that 'role reuse' is a wild goose chase in CM
systems, so I'm fine with that :slight_smile:

Sorry to reply to myself, but I checked and (with the meta pattern on
1.9.x at least), the kibana role _will_ be
able to trigger the handlers defined in the apache role. Which is nice.