Ansible Pilot

Ansible troubleshooting - fatal template error while templating string

FATAL template error while templating string

Ansible troubleshooting

Today we’re going to talk about Ansible troubleshooting and specifically about the “FATAL template error while templating string” error. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

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!

---
- name: file module demo
  hosts: all
  vars:
    myfile: "{{ ~/example.txt }}"
  tasks:
    - name: Creating an empty file
      ansible.builtin.file:
        path: "{{ myfile }}"
        state: touch
$ 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

---
- name: file module demo
  hosts: all
  vars:
    myfile: "~/example.txt"
  tasks:
    - name: Creating an empty file
      ansible.builtin.file:
        path: "{{ myfile }}"
        state: touch

$ 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

code with ❤️ in GitHub

Recap

Now you know better how to troubleshoot the template error while templating string error. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

Donate

Want to keep this project going? Please donate

Follow me

Subscribe not to miss any new releases

September 21, 2021