DevOps Classroom Series – 22/Oct/2021

Templating using Jinja2

  • Ansible uses Jinja2 templating to enable dynamic expressions and access to variables.
  • First and foremost advantage of using Jinja2 is we can avoid static file copies.
  • Couple of classes ago, we had problem with tomcat.service as the path of the java was different for centos and ubuntu systems. To solve this we created two service files and two tasks
  • We can avoid this by using templates
  • Lets create a new folder for templates and create a tomcat.service.j2 file
  • Refer Here for the changeset containing the usage of jinja2 templates
  • Refer Here for the official docs
  • Refer Here for some example jinja2 filters
  • Using map filter and list filter
- hosts: localhost 
  connection: local 
  gather_facts: no 
  vars: 
    names: 
     - first: Paul 
       last: Thompson 
     - first: Rod 
       last: Johnson
  tasks:
    - debug: 
        msg={{ names | map(attribute='first') | list }} 
    - debug: 
        msg={{ names | map(attribute='last') | list }} 
    - debug: 
        msg={{ names | map('upper') | list }} 
    - debug: 
        msg={{ names | map(attribute='last') | map('upper') | list }}

Ansible Behavioral Parameters

  • The list of behavior parameters
Name Default Description
ansible_host Name of the host Hostname or IP address to SSH
ansible_port 22 port to ssh to
ansible_user root User to SSH as
ansible_password (None) Password to use for SSH Authentication
ansible_ssh_private_key_file (None) SSH Private key to use for SSH authentication
ansible_python_interpreter /usr/bin/python Python interpreter on the host
ansible_connection smart How Ansible will connect to host

Leave a Reply

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

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube