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:
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.
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.
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.
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'?
“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.