Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do double curly braces ({{) mean in YAML files (as used by Ansible)?

Tags:

I'm fairly new to Ansible and trying to understand the YAML file. In that i'm not clear about this piece of line - file: dest={{ '{{' }} docroot {{ '}}' }. Can some one please explain me what those curly braces '{{' '}}' are doing?

- name: Create Web Root      when: nginxinstalled|success      file: dest={{ '{{' }} docroot {{ '}}' }} mode=775 state=directory owner=www-data group=www-data    notify:        - Reload Nginx 
like image 672
kick07 Avatar asked Apr 03 '17 12:04

kick07


People also ask

What is curly braces in YAML?

YAML can also use flow styles for collections, using explicit indicators rather than indentation to denote scope. A sequence can be written as a comma separated list within square brackets ( [] ): Copy. [PHP, Perl, Python] A mapping can be written as a comma separated list of key/values within curly braces ( {} ):

What does {{ }} mean in YAML?

If a value after a colon starts with a “{”, YAML will think it is a dictionary, so you must quote it, like so: foo: "{{ variable }}" If your value starts with a quote the entire value must be quoted, not just part of it.

What does double curly braces mean in Python?

The curly braces are part of Django Template Language. The part encapsulated between double curly braces {{ }} is nothing but a variable. That's how DTL, Jinja2 and other template languages work. They have their own set of rules which translates the template in to python and later to HTML code.

What do double curly braces mean in HTML?

Declare a template in the HTML file. Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.


2 Answers

Ansible uses the jinja2 template

the {{ }} are used to evaluate the expression inside them from the context passed.

So {{ '{{' }} evaluates to the string {{

And the while expression {{ docroot }} is written to a template, where docroot could be another template variable

refer https://docs.ansible.com/ansible-container/container_yml/template.html for more details.

like image 83
Akshay Apte Avatar answered Oct 25 '22 04:10

Akshay Apte


I found this YAML syntax overview in Ansible documantation, to be fairly useful.

It says that double curly braces {{ variable }} are used to evaluate expressions. Whilst distinguishing a single curly braces (after a colon), that is used to declare a dictionary. For example:

satchmo: {name: Louis Armstrong, music: Jazz, instrument: Trumpet} 

Also, take a look at Jinja Template Designer Documentation. Jinja template is rendered before YAML, which means it is evaluated prior to Ansible execution.

{% ... %} for Statements {{ ... }} for Expressions to print to the template output {# ... #} for Comments not included in the template output #  ... ## for Line Statements 
like image 25
Noam Manos Avatar answered Oct 25 '22 04:10

Noam Manos