Local path to a file within a role

725 views
Skip to first unread message

Jon Warbrick

unread,
Jul 1, 2014, 2:37:18 PM7/1/14
to ansible...@googlegroups.com
Apologies for what may be a silly question.

Within a role, I'd like to run a verification command on some files from the
.../files/ directory before installing them. I can see how to use a
'local_action' and the command or shell module for this. But I can't see how to
give the verification command an appropriate path to the file to process. The
best I can come up with is a solution based on playbook_dir:

{{ playbook_dir }}/roles/foobar-config/files/{{ item }}

But this is brittle, because it includes the role name which might change. Is
there a better way to do this?

Jon.

--
Jon Warbrick
Information Systems Manager, University of Cambridge Information Services

Michael DeHaan

unread,
Jul 1, 2014, 2:51:12 PM7/1/14
to ansible...@googlegroups.com
This is what we have right now, there isn't a role variable that says what's the path of the current role, because of the way roles are expanded, can have dependencies, and so on.

I don't suspect your role name will change any, so it shouldn't be much of a problem - if it does, the validation step would fail, which is a good reason to test playbooks anyway.

See http://docs.ansible.com/test_strategies.html for some notes about stage environments for example


--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/alpine.DEB.2.02.1407011644490.22659%40mnementh.csi.cam.ac.uk.
For more options, visit https://groups.google.com/d/optout.

Mikhail Koshelev

unread,
Jul 1, 2014, 4:56:17 PM7/1/14
to ansible...@googlegroups.com
Hi Jon,

There's actually a way (hack?) to get role directory by using lookup. Define 'role_dir' var as shown below and you can use it in your roles' tasks files to refer to the current role directory:
role_dir: "{{ lookup('pipe','pwd')|dirname }}"

Just note, this works as long as the tasks file (or included fragment) using 'role_dir' variable is located in the role's 'tasks' dir. Doesn't work if used in the fragment included from outside.

Also, role name can be obtained in the same way:
role_name: "{{ lookup('pipe','pwd')|dirname|basename }}"

Michael DeHaan

unread,
Jul 1, 2014, 6:12:07 PM7/1/14
to ansible...@googlegroups.com
Just note that if you have --forks 200 that "pwd" will get executed 200 times at once, which might make your system unhappy.




--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9d28c301-71d8-445a-9242-932f635227f2%40googlegroups.com.

Jon Warbrick

unread,
Jul 12, 2014, 8:38:35 AM7/12/14
to ansible...@googlegroups.com
On Tue, 1 Jul 2014, Michael DeHaan wrote:

> This is what we have right now, there isn't a role variable that says
> what's the path of the current role, because of the way roles are expanded,
> can have dependencies, and so on.
>
> I don't suspect your role name will change any, so it shouldn't be much of
> a problem - if it does, the validation step would fail, which is a good
> reason to test playbooks anyway.

Belated thanks for that - I'll stick with my current approach, though I'm
now wondering if role dependencies, etc, may mean even that won't always
work.

Jon Warbrick

unread,
Jul 12, 2014, 8:50:01 AM7/12/14
to ansible...@googlegroups.com
On Tue, 1 Jul 2014, Mikhail Koshelev wrote:

> There's actually a way (hack?) to get role directory by using lookup.
> Define 'role_dir' var as shown below and you can use it in your roles'
> tasks files to refer to the current role directory:
> role_dir: "{{ lookup('pipe','pwd')|dirname }}"

So this assumes that, when the role_dir variable is expanded, the current
directory will be one inside the relevant role directory? That's not what
I thought I saw, but perhaps I was wrong.

But if that *is* the case, surely I could use relative paths to construct
paths to any other files in the role and bypass the need to run 'pwd'?

I fear I'm missing something...

Michael DeHaan

unread,
Jul 12, 2014, 8:54:46 AM7/12/14
to ansible...@googlegroups.com
"So this assumes that, when the role_dir variable is expanded, the current directory will be one inside the relevant role directory? That's not what I thought I saw, but perhaps I was wrong."

Might be helpful to share what you saw, showing both the actual vs expected values, and playbook snippets.

I'm not sure I understand the pwd issue but yes "/../" and the like do work in Ansible.




--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/alpine.OSX.2.00.1407121338330.1232%40ramoth.brsn.invalid.
Reply all
Reply to author
Forward
0 new messages