From f69da544f771d1ca6dad50260f4e71c5c0f8b7cb Mon Sep 17 00:00:00 2001 From: Jon Hawkesworth Date: Wed, 5 Aug 2015 18:38:15 +0100 Subject: [PATCH] add win_basename and win_dirname filters --- docsite/rst/playbooks_filters.rst | 8 ++++++++ lib/ansible/plugins/filter/core.py | 3 +++ 2 files changed, 11 insertions(+) diff --git a/docsite/rst/playbooks_filters.rst b/docsite/rst/playbooks_filters.rst index d95f617f19..eb84ecd203 100644 --- a/docsite/rst/playbooks_filters.rst +++ b/docsite/rst/playbooks_filters.rst @@ -338,10 +338,18 @@ To get the last name of a file path, like 'foo.txt' out of '/etc/asdf/foo.txt':: {{ path | basename }} +To get the last name of a windows style file path (new in version 2.0):: + + {{ path | win_basename }} + To get the directory from a path:: {{ path | dirname }} +To get the directory from a windows path (new version 2.0):: + + {{ path | win_dirname }} + To expand a path containing a tilde (`~`) character (new in version 1.5):: {{ path | expanduser }} diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index fa8d0e5cbb..4b8bf2ed35 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -21,6 +21,7 @@ import sys import base64 import json import os.path +import ntpath import types import pipes import glob @@ -251,6 +252,8 @@ class FilterModule(object): 'realpath': partial(unicode_wrap, os.path.realpath), 'relpath': partial(unicode_wrap, os.path.relpath), 'splitext': partial(unicode_wrap, os.path.splitext), + 'win_basename': partial(unicode_wrap, ntpath.basename), + 'win_dirname': partial(unicode_wrap, ntpath.dirname), # value as boolean 'bool': bool,