Ryan
Ryan

Reputation: 1221

Relative paths in Jinja2 template

I have a .xml.j2 template that I'm populating through ansible. I have a variable being passed in by ansible that may be a relative or an absolute path. If the path is relative, I need to prepend a parent directory path. Is there a way for jinja2 to check whether a path is relative or absolute, and proceed accordingly? If not, can I do this in ansible and pass in the modified variable?

Upvotes: 2

Views: 3235

Answers (1)

techraf
techraf

Reputation: 68629

Ansible gives you the realpath filter (along with some other useful ones for manipulating paths):

debug:
  msg: "{{ '.' | realpath }}"

Answering your later comment:

Unfortunately, I don't just need the real path. I need "if it's a realpath, great. If it's not, prepend this path to it, which is set in another variable"

You can use a == operator to verify if path is a relative or an absolute one:

path == path | realpath

Not to mention something as trivial as checking for / at the front.

Upvotes: 5

Related Questions