Ansible troubleshooting - fatal template error while templating string
How to reproduce, troubleshoot, and fix the "FATAL template error while templating string" Ansible runtime error.
Introduction
Welcome to another episode of Ansible Pilot! I’m Luca Berton, and today we’ll be diving into Ansible troubleshooting, focusing on the “FATAL template error while templating string” runtime error. Join me as we explore how to reproduce, troubleshoot, and fix this challenging issue.
The Demo
Let’s jump straight into a live demo to understand the error practically. In this example, we have a playbook (template_error_string_error.yml
) attempting to create an empty file with a variable referencing ~/example.txt
.
# template_error_string_error.yml
---
- name: file module demo
hosts: all
vars:
myfile: "{{ ~/example.txt }}"
tasks:
- name: Creating an empty file
ansible.builtin.file:
path: "{{ myfile }}"
state: touch
Executing this playbook (ansible-playbook -i inventory template_error_string_error.yml
) results in a fatal error:
TASK [Creating an empty file] ***********************************************************************************************************
fatal: [demo.example.com]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ ~/example.txt }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: unexpected '~'. String: {{ ~/example.txt }}"}
Understanding the Error
The error message is clear: “template error while templating string: unexpected ‘~’.” The issue lies in the attempt to use the tilde (~
) symbol in the variable, which is not a valid attribute for templating.
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Fixing the Code
To resolve the issue, we need to correct our playbook. The fixed version (template_error_string_fix.yml
) uses the correct variable format:
# template_error_string_fix.yml
---
- name: file module demo
hosts: all
vars:
myfile: "~/example.txt"
tasks:
- name: Creating an empty file
ansible.builtin.file:
path: "{{ myfile }}"
state: touch
Executing the fixed playbook (ansible-playbook -i demo/inventory troubleshooting/template_error_string_fix.yml
) should now complete without errors:
TASK [Creating an empty file] ***********************************************************************************************************
changed: [demo.example.com]
Recap
In this tutorial, we walked through reproducing, troubleshooting, and fixing the “FATAL template error while templating string” error in Ansible. The key takeaway is to ensure that variables are formatted correctly in your playbooks.
I hope this guide helps you navigate and resolve similar issues in your Ansible automation journey. If you found this information valuable, consider subscribing for more Ansible insights.
demo
The best way of talking about Ansible troubleshooting is to jump in a live demo to show you practically the not a valid attribute for a Play
error and how to solve it!
error code
- template_error_string_error.yml
---
- name: file module demo
hosts: all
vars:
myfile: "{{ ~/example.txt }}"
tasks:
- name: Creating an empty file
ansible.builtin.file:
path: "{{ myfile }}"
state: touch
error execution
- output
$ ansible-playbook -i demo/inventory troubleshooting/template_error_string_error.yml
PLAY [file module demo] ****************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************
ok: [demo.example.com]
TASK [Creating an empty file] **********************************************************************************************************************************************************************
fatal: [demo.example.com]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ ~/example.txt }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: unexpected '~'. String: {{ ~/example.txt }}"}
PLAY RECAP *****************************************************************************************************************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
fix code
- template_error_string_fix.yml
---
- name: file module demo
hosts: all
vars:
myfile: "~/example.txt"
tasks:
- name: Creating an empty file
ansible.builtin.file:
path: "{{ myfile }}"
state: touch
fix execution
- output
$ ansible-playbook -i demo/inventory troubleshooting/template_error_string_fix.yml
PLAY [file module demo] ****************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************
ok: [demo.example.com]
TASK [Creating an empty file] **********************************************************************************************************************************************************************
changed: [demo.example.com]
PLAY RECAP *****************************************************************************************************************************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0
Recap
Now you know better how to troubleshoot the template error while templating string error. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate