# Using ansible variables in multiline

**URL:** <https://forum.ansible.com/t/using-ansible-variables-in-multiline/18302>\
**Category:** Ansible Project\
**Created:** [May 25, 2015, 6:38pm UTC](https://forum.ansible.com/t/using-ansible-variables-in-multiline/18302 "2015-05-25T18:38:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ateamp66](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@ateamp66](https://forum.ansible.com/u/ateamp66)\
**Post date:** [May 25, 2015, 6:38pm UTC](https://forum.ansible.com/t/using-ansible-variables-in-multiline/18302/1 "2015-05-25T18:38:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 26, 2015, 6:02pm UTC](https://forum.ansible.com/t/using-ansible-variables-in-multiline/18302/2 "2015-05-26T18:02:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ateamp66](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@ateamp66](https://forum.ansible.com/u/ateamp66)\
**Post date:** [May 26, 2015, 9:45pm UTC](https://forum.ansible.com/t/using-ansible-variables-in-multiline/18302/3 "2015-05-26T21:45:21Z")

</div>

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 %}

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 26, 2015, 9:49pm UTC](https://forum.ansible.com/t/using-ansible-variables-in-multiline/18302/4 "2015-05-26T21:49:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ateamp66](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@ateamp66](https://forum.ansible.com/u/ateamp66)\
**Post date:** [May 26, 2015, 10:17pm UTC](https://forum.ansible.com/t/using-ansible-variables-in-multiline/18302/5 "2015-05-26T22:17:46Z")

</div>

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