DevOps Classroom notes 14/Nov/2026

Ansible Jinja2 Templates

  • Jinja2 is a Python-based templating engine used by Ansible.
  • Templates are used to dynamically generate configuration files.

Where are templates used?

  1. Playbooks: Use {{ variable }} to reference variables.
  2. Template files: Use the .j2 extension to identify Jinja2 template files.

Directory Structure

├── hostname.yml
└── templates/
    └── test.j2

Note: Use the ansible.builtin.template module to generate configuration files from .j2 templates.

jinja2 template will use module ansible.builtin.template

offical docs for templating module

- name: Update sshd configuration safely, avoid locking yourself out
  ansible.builtin.template:
    src: etc/ssh/sshd_config.j2
    dest: /etc/ssh/sshd_config
    owner: root
    group: root
    mode: '0600'
    validate: /usr/sbin/sshd -t -f %s
    backup: yes

Tomact

- name: tomcat
  hosts: all
  become: true
  vars:
    tomcat_group: tomcat
    tomcat_app_version: tomcat-10
    java_package: openjdk-25-jdk
    tomcat_version: 10.1.61
    tomcat_user: tomcat
    home: /opt/tomcat
    url: "https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.61/bin/apache-tomcat-10.1.61.tar.gz"
  tasks:
    - name: java installation
      ansible.builtin.apt:
        name: "{{ java_package }}"
        state: present
        update_cache: yes

    - name: create tomcat group
      ansible.builtin.group:
        name: "{{ tomcat_group }}"
        system: yes
        state: present

    - name: create tomcat user
      ansible.builtin.user:
        name: "{{ tomcat_user }}"
        group: "{{ tomcat_group }}"
        home: "{{ home }}"
        shell: /bin/false
        system: yes
        state: present

    - name: download tomcat
      ansible.builtin.get_url:
        url: "{{ url }}"
        dest: "/tmp/apache-tomcat-{{ tomcat_version }}.tar.gz"

    - name: extract tomcat
      ansible.builtin.unarchive:
        src: "/tmp/apache-tomcat-{{ tomcat_version }}.tar.gz"
        dest: /opt/tomcat
        remote_src: yes

    - name: change ownership of tomcat directory
      ansible.builtin.file:
        path: /opt/tomcat
        state: directory
        recurse: yes
        owner: "{{ tomcat_user }}"
        group: "{{ tomcat_group }}"

    - name: provide execute permission to tomcat scripts
      ansible.builtin.file:
        path: /opt/tomcat/bin
        state: directory
        recurse: yes
        mode: u+x

    - name: update java alternatives
      ansible.builtin.shell:
        cmd: update-java-alternatives -l

    - name: create tomcat service file
      ansible.builtin.copy:
        dest: /etc/systemd/system/tomcat.service
        content: |
          [Unit]
          Description=Tomcat
          After=network.target

          [Service]
          Type=forking

          User=tomcat
          Group=tomcat

          Environment="JAVA_HOME=/usr/lib/jvm/java-1.25.0-openjdk-amd64"
          Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
          Environment="CATALINA_BASE=/opt/tomcat"
          Environment="CATALINA_HOME=/opt/tomcat"
          Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
          Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

          ExecStart=/opt/tomcat/bin/startup.sh
          ExecStop=/opt/tomcat/bin/shutdown.sh

          RestartSec=10
          Restart=always

          [Install]
          WantedBy=multi-user.target
      notify:
        - reload systemd daemon
        - start tomcat service

  handlers:
    - name: reload systemd daemon
      ansible.builtin.systemd:
        daemon_reload: yes

    - name: start tomcat service
      ansible.builtin.systemd:
        name: tomcat
        state: started
        enabled: yes


Tasks – Ansible Tomcat Playbook

1. Convert Tomcat Configuration Files into Templates

  • Convert /opt/tomcat/conf/tomcat-users.xml into an Ansible Jinja2 template file (tomcat-users.xml.j2).
  • Convert /opt/tomcat/webapps/manager/META-INF/context.xml into an Ansible Jinja2 template file (context.xml.j2).

2. Create Ansible Deployment Playbook

  • Create an Ansible deployment playbook to deploy application files from the local machine to the remote Tomcat server.
  • Use the copy or synchronize module to transfer application files.
  • Deploy the application into the Tomcat webapps directory.
  • Set the appropriate file ownership and permissions.
  • Restart the Tomcat service after deployment.

3. Verify Deployment

  • Verify that the Tomcat service is running.
  • Check the deployed application in the Tomcat webapps directory.
  • Access the application using the Tomcat server IP and port 8080.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company
Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube