Using ansible variables in multiline

Hello, I’ve been using ansible for almost 3 months now, awesome tool.
I’ve hit a little bump, I’m trying to make ansible variables work inside yaml multilines in this way:

ansible 1.9.1 (detached HEAD b47d1d7e69) last updated 2015/05/25 13:25:12 (GMT -500)

lib/ansible/modules/core: (detached HEAD 32e609720a) last updated 2015/05/25 13:25:30 (GMT -500)
lib/ansible/modules/extras: (detached HEAD 8dfa63d1d8) last updated 2015/05/25 13:25:32 (GMT -500)
v2/ansible/modules/core: (detached HEAD 32e609720a) last updated 2015/05/25 13:25:34 (GMT -500)
v2/ansible/modules/extras: (detached HEAD 8dfa63d1d8) last updated 2015/05/25 13:25:37 (GMT -500)

`

file: ./group-vars/www

www_port: 443
nginx_sites:

  • name: www
    servers:
  • conf: |
    listen {{www_port}} ssl spdy;
    listen [::]:{{www_port}} ssl spdy;
    location ~ .php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Prevent clients from accessing hidden files (starting with a dot)

This is particularly important if you store .htpasswd files in the site hierarchy

location ~* (?:^|/). {
deny all;
}

Prevent clients from accessing to backup/config/source files

location ~* (?:.(?:bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$ {
deny all;
}

file: ./roles/nginx/templates/site-template.j2

{{ansible_managed}}

{% for server in item.servers %}
server {
{{ server.conf }}
}
{% endfor %}

`

Unfortunately ansible always sends a missing quotes syntax error, and I’m not sure how to proceed, since enclosing the variable in double-quotes (i.e. “{{www_port}}”) or the entire line does nothing.
Is there a way to do so?

i would put the config in a template that you can then conditionally
include, trying to put the server configuration like this into a
variable is very error prone.

Thanks,
You are indeed right about using templates, and we have our principal sites using one per needed config.
The thing is that we are making lots of tests on ansible, mostly checking for alternate ways to do things, and this just tumbled on us.

By error prone you mean “user-error” prone or “software-error” prone?

We ended up resolving it in this ugly way:

`

group_vars

nginx_sites:

  • name: satis
    servers:
  • conf:
  • listen {{satis_port}} ssl spdy;
  • listen [::]:{{satis_port}} ssl spdy;
  • server_name satis;
  • root {{satis_archive_absolute_directory}};
  • index index.html;

template

{% if item.servers is defined %}
{% for server in item.servers %}
server {
{% for line in server.conf %}
{{ line }}
{% endfor %}
}
{% endfor %}
{% endif %}

`

both, you are involving multiple parsers, each with it's own quirks.

Ok, I’ll keep it in mind, see ya.