Jinja2 is a templating engine for python. However we can use it in Ansible for generating dynamic content. There are many filters available in Jinja2. Please check it on here: Link. In this article we are going to check Jinja2 string filter examples.
Why we use Jinja2 templates:
Lets say you want to print simple sentence like below:
“My name is Mirkar”
In the above print sentence we used hard-coded value for name. Now we will replace the name with variable say “my_name” with initializing it with value “Mirkar” and we can use it dynamically to replace its contents while printing or using it as below within flower braces:
My Name is {{ my_name }}
So just to print the variable value use following statement along with debug module.
msg: "This is my name as usual is: '{{ my_name }}'"
Here in the below Jinja2 string filter examples we have used debug module so that we can able to print it on screen.
Jinja2 string filter example 1: upper case
In this Jinja2 string filter example we will be converting the variable value to upper case with following statement.
msg: "This is my name in upper case: '{{ my_name|upper }}'"
Output:
"This is my name in upper case: 'MIRKAR'"
Jinja2 string filter example 2: lower case
In this Jinja2 string filter example we will be converting the variable value to lower case with following statement.
msg: "This is my name in upper case: '{{ my_name|lower }}'"
Output:
"This is my name in Lowewe case: 'mirkar'"
Jinja2 string filter example 3: Title case
In this Jinja2 string filter example we will be converting the variable value to Titile case with following statement.
msg: "This is my name in upper case: '{{ my_name|title }}'"
Output:
"This is my name in Title case: 'Mirkar'"
Jinja2 string filter example 4: replace
Let suppose if you want to replace the value of the variable with some other string. For example you want to use “Mirkar1” instead of “Mirkar”. Then use below replace method :
msg: "This is replacing example: '{{ my_name|replace('mirkar','Mirkar1')}}'"
Output:
"This is replacing example: 'Mirkar1'"
Jinja2 string filter example 5: default value
Let suppose if the variable is initialed with NULL value. Then to use other value which is default one then use following jinja2 default method. Here in the below method we have not initialized value of the variable “first_name” so we have given default value as “Manmohan” instead. Please check the complete playbook example to understand this concept.
msg: "The Name is: '{{ first_name|default('Manmohan') }} {{ my_name }}' "
"The Name is: 'Manmohan mirkar' "
To explain these all Jinja2 examples in one go refer below ansible playbook and corresponding output. Hope it will cover all your doubts.
Playbook:
[osboxes@ansiblecontroller]$ cat new.yml
-
name: Examples for various JINJA2 filters!
hosts: localhost
vars:
my_name: mirkar
first_nme:
tasks:
- debug:
#Here we are just printing out the variable value for verification
msg: "This is my name as usual is: '{{ my_name }}'"
- debug:
#Here are converting string into uppercase
msg: "This is my name in upper case: '{{ my_name|upper }}'"
- debug:
#Here are are converting string into Lower case
msg: "This is my name in Lowewe case: '{{ my_name|lower }}'"
- debug:
#Here are converting string into Title case
msg: "This is my name in Title case: '{{ my_name|title}}'"
- debug:
#Here are replacing string with other string
msg: "This is replacing example: '{{ my_name|replace('mirkar','Mirkar1')}}'"
- debug:
#Here are placing default value for variable first_nme to Manmohan if its NULL.
msg: "The Name is: '{{ first_name|default('Manmohan') }} {{ my_name }}' "
OUTPUT
$ ansible-playbook new.yml -i inventory.txt
PLAY [Examples for various JINJA2 filters!] *****************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************************************************
ok: [localhost] => {
"msg": "This is my name as usual is: 'mirkar'"
}
TASK [debug] ************************************************************************************************************************
ok: [localhost] => {
"msg": "This is my name in upper case: 'MIRKAR'"
}
TASK [debug] ************************************************************************************************************************
ok: [localhost] => {
"msg": "This is my name in Lowewe case: 'mirkar'"
}
TASK [debug] ************************************************************************************************************************
ok: [localhost] => {
"msg": "This is my name in Title case: 'Mirkar'"
}
TASK [debug] ************************************************************************************************************************
ok: [localhost] => {
"msg": "This is replacing example: 'Mirkar1'"
}
TASK [debug] ************************************************************************************************************************
ok: [localhost] => {
"msg": "The Name is: 'Manmohan mirkar' "
}
PLAY RECAP **************************************************************************************************************************
localhost : ok=7 changed=0 unreachable=0 failed=0
Leave a Reply