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 Playbook 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.
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 Playbook/inventory troubleshooting/template_error_string_fix.yml
) should now complete without errors:
TASK [Creating an empty file] ***********************************************************************************************************
changed: [demo.example.com]
Conclusion
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.
Playbook
The best way of talking about Ansible troubleshooting is to jump in a live Playbook 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 Playbook/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 Playbook/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
Conclusion
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 Udemy 300+ Lessons Video Course.
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