Templating Migration Guide

Last updated: January 12, 2026

⚠️ Critical: This guide addresses the significant templating changes introduced in ansible-core 2.19/Ansible 12. These changes affect security, performance, and user experience. Review this guide before upgrading.

Why This Guide Exists

In late 2025, the Ansible project introduced major templating engine improvements in ansible-core 2.19 (Ansible 12). While these changes bring substantial benefits, they also break some previously "working" playbooks that relied on problematic behaviors. This guide will help you understand what changed, why it changed, and how to update your automation to be compatible.

What Changed in ansible-core 2.19

The templating changes can be grouped into three main categories:

1. Stricter Template Validation

Templates that previously failed silently now report errors explicitly.

2. Security Enhancements

Unsafe operations that could lead to code injection are now blocked by default.

3. Performance Optimizations

Template rendering is faster and more efficient, but requires cleaner syntax.

Impact Assessment: Is Your Content Affected?

Run this quick check to see if you need to migrate:

Common Breaking Changes and Fixes

Change 1: Undefined Variables in Templates

Problem: Previously, undefined variables in templates would sometimes fail silently or return empty strings.

Old Behavior (ansible-core 2.18):

New Behavior (ansible-core 2.19):

Solution: Use the default filter or proper conditional checks:

Change 2: Type Coercion in Conditionals

Problem: Loose type coercion in conditionals could lead to unexpected behavior.

Old Behavior:

New Behavior: Stricter boolean evaluation

Solution: Be explicit in your conditionals:

Change 3: Template Syntax Validation

Problem: Malformed Jinja2 syntax that was previously tolerated is now rejected.

Old Behavior:

New Behavior: Strict syntax enforcement

Solution: Use proper syntax and escaping:

Change 4: Filter Argument Validation

Problem: Filters with incorrect argument types were sometimes accepted.

Old Behavior:

New Behavior: Type checking enforced

Solution: Use correct types:

Change 5: Loop Variable Scope

Problem: Loop variables leaking into nested contexts.

Old Behavior:

New Behavior: Stricter variable scoping

Solution: Use loop_control to avoid conflicts:

Change 6: Template Security Restrictions

Problem: Templates that attempt to access Python internals are now blocked.

Old Behavior:

New Behavior: Security restrictions enforced

Solution: Don't try to access Python internals. Use proper Ansible constructs:

Change 7: Implicit String Concatenation

Problem: Implicit string concatenation in templates is no longer supported.

Old Behavior:

New Behavior: Explicit concatenation required

Solution: Use the concatenation operator or filters:

Migration Strategy

Phase 1: Assessment (Week 1)

  1. Inventory Your Playbooks

  2. Test in Non-Production

  3. Collect Errors

Phase 2: Prioritization (Week 1-2)

Create a migration priority matrix:

Priority
Criteria
Action

P0

Production-critical playbooks with errors

Fix immediately

P1

Production playbooks with warnings

Fix within 2 weeks

P2

Development/test playbooks with errors

Fix within 1 month

P3

Deprecated but working playbooks

Document for future fix

Phase 3: Fixing (Week 2-4)

Work through your priority list systematically:

Phase 4: Testing (Week 3-5)

Phase 5: Deployment (Week 5-6)

  1. Deploy to development

  2. Run automated tests

  3. Deploy to staging

  4. Conduct user acceptance testing

  5. Deploy to production (with rollback plan)

Automated Migration Tools

Script to Identify Common Issues

Save this as check_templates.py and run:

Real-World Migration Example

Here's a before/after of a real playbook migration:

Before (ansible-core 2.18)

After (ansible-core 2.19)

Testing Your Migration

Create a comprehensive test playbook:

Rollback Plan

If migration causes issues in production:

Best Practices for Future-Proof Templates

  1. Always Use Defaults

  2. Explicit Type Checking

  3. Named Loop Variables

  4. Validate Templates

  5. Use Check Mode

Conclusion

The ansible-core 2.19/Ansible 12 templating changes represent a significant improvement in security, performance, and reliability. While migration requires effort, the result is more robust, maintainable automation code.

Take the time to migrate properly – your future self (and your team) will thank you!


Further Reading

Ready to upgrade? Follow this guide step-by-step for a smooth migration! πŸš€

Last updated