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
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 ( {} ):
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With