## TL;DR
Ansible fails templating when the Jinja2 has a syntax error or references an undefined variable. Run with `-vvv` to see the exact expression, fix the syntax or define the variable (or use the `default` filter), and the task renders.

## Error
```text
ansible: template error while templating string
```

## Steps
1. Get the detail: re-run with `-vvv` and read the full templating error, which names the expression and the failure (undefined, syntax, filter). Expected: the exact offending expression.
2. Run `ansible-playbook --syntax-check` to catch structural issues early. Expected: playbook structure is valid, isolating the problem to the template itself.
3. Fix undefined variables: define the variable in inventory, group vars, or task vars, or guard the reference with the `default` filter. Expected: the reference resolves.
4. Fix syntax: match every opening brace, quote nested quotes correctly, and keep filters in valid order. Expected: the Jinja2 parses.
5. Re-run the playbook. Expected: the template renders and the task proceeds.

## When to use
- Tasks fail with "template error while templating string"
- New templates or recently edited variable references
- After upgrading Ansible or Jinja2 (stricter parsing)

## When not to use
- SSH connection failures (transport layer)
- Permission errors on the target (become/sudo)

## Tool compatibility
- Ansible playbooks, Jinja2 templating; `-vvv`, `--syntax-check`

## Variant phrasings
### ansible undefined variable in template
Define it or use the `default` filter; undefined is the most common templating failure.

### jinja2 syntax error ansible
Check brace matching and quoting; the `-vvv` output points at the expression.

## Why it happens
Ansible renders every string through Jinja2 before executing the task, so a single bad expression aborts the task. The short error message hides whether it is syntax or an undefined name, which is why verbose output is the first step.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_di612rKC5IKzYO41bq7xKw
