Ansible-Jinja2 Templating.
Jun 12, 2022
Jinja2 is one of the most powerful and famous python-based template engines. Jinja2 template is able to access the variables in ansible. In Ansible playbook, we generally have to change a lot of configuration & values for each server which is time consuming. With jinja2 templating, one can change variables only to that template which results in saving a lot of time and an easily manage the config changes in systems.
When using jinja templating, it good to give the extension .j2
- hosts: webserver
vars:
pkg_name: httpd
http_port: 8080
web_path: /var/www/html
tasks:
- name: Install apache Webserver
package:
name: "{{ pkg_name }}
state: present
- name: Deploy web page
template:
dest: "{{ web_path }}/index.html"
src: home.html.j2