Bootstrap FreeKB - Ansible - Jinja2 templating
Ansible - Jinja2 templating

Updated:   |  Ansible articles

Ansible can use the Jinja templating system for variables. If you are not familiar with Jinja, check out Getting Started with Jinja.

In Jinja2, an expression has this structure. This could be a variable, an array, et cetera.

{{ ... }}

 

For example, let's say a variable named "foo" contains a value of "bar".

vars:
  foo: bar

 

This is how you would use the "foo" variable to return "bar".

{{ foo }}

 

Here is an example of using a Jinja2 variable in the debug module.

---
- hosts: localhost
  tasks:
  - name: output the 'foo' variable
    debug:
      msg: The foo variable contains a value of {{ foo }}
...

 

AVOID TROUBLE

When a Jinja2 variable is used at the beginning of a parameter, it must be wrapped in quotes

---
- hosts: localhost
  tasks:
  - name: output the 'foo' variable
    debug:
      msg: "{{ foo }}"
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2658a1 in the box below so that we can be sure you are a human.