31

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
Anthon
  • 69,918
  • 32
  • 186
  • 246
kick07
  • 614
  • 1
  • 8
  • 19
  • 1
    Where did you get this example? It looks like this play is being setup for parsing by a second Jinja2 execution. The result would be "dest={{ docroot }} mode=755 ..." – dan_linder Apr 04 '17 at 05:17
  • Only some of those files are YAML files. They are regular files that can be used as templates from which YAML files can be generated. – Anthon Apr 04 '17 at 15:07

2 Answers2

16

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
Noam Manos
  • 15,216
  • 3
  • 86
  • 85
  • 1
    sometimes I am seeing a dash used, e.g. `{{- if ...}} or {{- end }}`, why is this? – boardtc Feb 19 '21 at 15:34
  • 3
    @boardtc Maybe that is Helm? They use `{{-` to left trim whitespace, and `-}}` to right trim whitespace. https://helm.sh/docs/chart_template_guide/control_structures/ – rybo111 Jul 20 '21 at 10:57
13

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Akshay Apte
  • 1,539
  • 9
  • 24