Using ansible variables in multiline

3,286 views
Skip to first unread message

atea...@gmail.com

unread,
May 26, 2015, 9:35:54 AM5/26/15
to ansible...@googlegroups.com
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?

Brian Coca

unread,
May 26, 2015, 2:02:17 PM5/26/15
to ansible...@googlegroups.com
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.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/bb8e1805-2ed5-4877-bdeb-dd0b9a8b7e82%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

atea...@gmail.com

unread,
May 26, 2015, 5:45:22 PM5/26/15
to ansible...@googlegroups.com
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 %}




Brian Coca

unread,
May 26, 2015, 5:49:08 PM5/26/15
to ansible...@googlegroups.com
both, you are involving multiple parsers, each with it's own quirks.


--
Brian Coca

atea...@gmail.com

unread,
May 26, 2015, 6:17:47 PM5/26/15
to ansible...@googlegroups.com
Ok, I'll keep it in mind, see ya.
Reply all
Reply to author
Forward
0 new messages