Fixing Ansible Jinja2 Template Syntax | Generated by AI

Home 2025.12

The error is clear and comes from this line in your hello.yml playbook:

content: "Managed by Ansible  hello from \n"
    dest: /tmp/hello.txt

Option 2 – If you really want to show the literal “” string (not evaluate it)

- name: Create a file with some text
  ansible.builtin.copy:
    content: "Managed by Ansible  hello from ' }}\n"
    dest: /tmp/hello.txt

Option 3 – Multi-line with | (literal block scalar)

- name: Create a file with some text
  ansible.builtin.copy:
    content: |
      Managed by Ansible — hello from 
      This is line 2
      This is line 3
    dest: /tmp/hello.txt

Option 4 – Using > for folded style

- name: Create a file with some text
  ansible.builtin.copy:
    content: >-
      Managed by Ansible — hello from 

      Second line starts here.
    dest: /tmp/hello.txt

Full corrected minimal playbook (working example)

---
- name: Tiny demo
  hosts: localhost
  gather_facts: yes

  tasks:
    - name: Create a file with some text
      ansible.builtin.copy:
        content: "Managed by Ansible  hello from  on \n"
        dest: /tmp/hello_from_ansible.txt
        mode: '0644'

Run it again:

ansible-playbook scripts/ansible/hello.yml

It will now succeed without any template syntax errors.

The root cause was just that stray ! and the unprocessed \n. Remove the !, use a real variable (or escape the braces if you want them literal), and you’re good to go!


Back

x-ai/grok-4.1-fast

Donate