1️⃣ What is an Ansible Template?
In Ansible, a template is a file (usually with .j2 extension, meaning Jinja2 template) that contains dynamic content. You can define placeholders or variables in the file, and when Ansible runs, it replaces these placeholders with actual values from your variables, host facts, or inventory.
Template file → usually in
templates/folder in your Ansible project-
Module used →
ansible.builtin.template -
Purpose → To generate configuration files dynamically based on variables.
Example placeholder in template:
2️⃣ How It Works
Suppose you want to create a database configuration file for Oracle. Instead of writing separate files for each database, you can use a template:
-
Template contains variables:
{{ db_name }},{{ db_user }}, etc. -
Playbook provides values for these variables.
-
Template module copies the rendered file to the target server.
Scenario:
You want to create an init.ora (Oracle initialization parameter file) for multiple Oracle databases dynamically using Ansible.
Step 1: Create a template file
templates/init.ora.j2
Step 2: Create a playbook
create_oracle_init.yml
Step 3: What happens when you run this playbook
-
For
PRODDB, the template will create:
-
For
TESTDB, it will create:
✅ No need to manually edit files for each database.
4️⃣ Why this is useful
-
Automates repetitive tasks for multiple databases.
-
Ensures consistency across environments (dev, test, prod).
-
Variables can come from inventory, host facts, or external files.
-
Reduces human errors when configuring multiple Oracle databases.
No comments:
Post a Comment