ansible.builtin.basename filter – get a path’s base name

Note

This filter plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name basename. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.basename for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

Synopsis

  • Returns the last name component of a path, what is left in the string that is not ‘dirname’.

Input

This describes the input of the filter, the value before | ansible.builtin.basename.

Parameter

Comments

Input

path / required

A path.

Notes

Note

  • The result of this filter is different from the Unix basename program; where basename for /foo/bar/ returns bar, the basename filter returns an empty string ('').

See Also

See also

ansible.builtin.dirname filter plugin

get a path’s directory name.

Examples

# To get the last name of a file path, like 'foo.txt' out of '/etc/asdf/foo.txt'.
{{ mypath | basename }}

Return Value

Key

Description

Return value

string

The base name from the path provided.

Returned: success

Authors

  • ansible core team

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.