File-Related Filters
To extract the file name from a complete path on a Linux system, you use thebasename filter. For example, given the path /etc/hosts, applying the basename filter returns hosts. However, note that this method does not work with Windows paths—which use backslashes. Instead, use the win_basename filter for Windows paths.
To separate the drive letter from the rest of a Windows path, use the win_splitdrive filter. This filter returns an array where the first element is the drive letter and the second element is the remaining path. If you need only the drive letter, simply chain the first filter to the result.
Examples of File-Related Filters
Below are examples demonstrating how to use these filters:last filter:
Integrating Jinja2 with Ansible Playbooks
Before executing an Ansible playbook, Ansible processes the file through the Jinja2 templating engine. This step replaces variables with actual values from the inventory, ensuring the final playbook is ready for execution. Consider the example below, which includes a simple inventory file and a playbook that updates the DNS server settings:For more examples and detailed explanations on using filters with Ansible, please visit the Ansible documentation on filters.
