Templates with Jinja2
Part of the Ansible course
Copying a static file to every machine works fine right up until one machine needs a different port number, a different hostname, or a config block that only applies to some servers and not others. At that point you have two bad options: keep a slightly different copy of the file for every machine and hope you remember to update all of them together, or write the file by hand on each server after Ansible finishes.
You have already met the idea of a form letter with blanks in it, filled in per recipient. Jinja2, the templating language Ansible uses, is that same idea taken further. A basic mail merge only fills in blanks: a name here, an address there. Jinja2 templates can also decide, line by line, whether a paragraph should appear at all, and they can repeat a line once for every item in a list without you writing that line...
Want to learn Ansible Templates with Jinja2 in a real environment?
Click below, get a fresh Linux box with Ansible ready to go, and work through Templates with Jinja2 hands-on with an AI tutor that knows your code.
Start this lessonTemplates with Jinja2What you'll learn
- Template module
- Jinja2 expressions
- Filters
- If blocks in templates
- For loops in templates
Read this lesson in your language
All lessons in this course
12 lessons- 1What Is Ansible20 min
- 2Inventory and Ad Hoc Commands25 min
- 3Your First Playbook30 min
- 4Tasks, Modules, and Handlers30 min
- 5Variables and Facts30 min
- 6Templates with Jinja230 min
- 7Loops and Conditionals30 min
- 8Roles and Galaxy35 min
- 9Vault and Secrets25 min
- 10Tags and Debugging25 min
- 11Collections and Plugins25 min
- 12Capstone: Multi Host Deploy60 min
