# Storing nginx variables and using in templates

**URL:** <https://forum.ansible.com/t/storing-nginx-variables-and-using-in-templates/33380>\
**Category:** Ansible Project\
**Created:** [July 12, 2020, 5:39pm UTC](https://forum.ansible.com/t/storing-nginx-variables-and-using-in-templates/33380 "2020-07-12T17:39:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ing\_Chrstopher\_Golde](https://avatars.discourse-cdn.com/v4/letter/i/53a042/32.png) [@Ing\_Chrstopher\_Golde](https://forum.ansible.com/u/Ing_Chrstopher_Golde)\
**Post date:** [July 12, 2020, 5:39pm UTC](https://forum.ansible.com/t/storing-nginx-variables-and-using-in-templates/33380/1 "2020-07-12T17:39:43Z")

</div>

I have two problems.

I want to store the variables of both examples in the host\_vars/server\_name.yml file.

Here are the raw format of the values that I want to store:

(1) nginx log formats:

log\_format main '$remote\_addr - $remote\_user [$time\_local] “$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;

log\_format simple ‘$remote\_addr [$time\_local]  
“$request” $status $ssl\_protocol $ssl\_cipher  
“$http\_referer” “$http\_user\_agent”’;

With the log formats, depending on the remote server there could be one or many different log formats, therefore mayn iterations can exist.

(2) SSL config

“ssl\_protocol”: “$ssl\_protocol”, ’  
“ssl\_cipher”: “$ssl\_cipher”, ’  
“ssl\_session\_reused”: “$ssl\_session\_reused”, ’  
“http\_referrer”: “$http\_referer”, ’  
“$http\_user\_agent”';

The challenge with storing these as variables is the many special characters.

I would like to use the above in a jinja 2 template.

Here are the contents of my jinja template nginx.cof.j2

user {{ nginx\_user }};

error\_log {{ nginx\_error\_log }};  
pid {{ nginx\_pidfile }};

{% block worker %}  
worker\_processes {{ nginx\_worker\_processes }};  
{% endblock %}

{% block events %}  
events {  
worker\_connections {{ nginx\_worker\_connections }};  
}  
{% endblock %}

http {  
{% block http\_begin %}{% endblock %}

{% block http\_basic %}  
include {{ nginx\_mime\_file\_path }};  
default\_type application/octet-stream;

client\_max\_body\_size {{ nginx\_client\_max\_body\_size }};

{% for line in nginx\_log\_format|indent(23) %}

\n{{ line }}\n

{% endfor %}

access\_log {{ nginx\_access\_log }};

sendfile {{ nginx\_sendfile }};  
tcp\_nopush {{ nginx\_tcp\_nopush }};

keepalive\_timeout {{ nginx\_keepalive\_timeout }};  
keepalive\_requests {{ nginx\_keepalive\_requests }};

server\_tokens {{ nginx\_server\_tokens }};

{% endblock %}

{% block http\_gzip %}  
gzip: {{ nginx\_gzip }};  
gzip\_comp\_level: {{ nginx\_gzip\_comp\_level }};  
gzip\_min\_length: {{ nginx\_gzip\_min\_length }};  
gzip\_proxied: {{ nginx\_gzip\_proxied }};  
gzip\_vary: {{ nginx\_gzip\_vary }};

{% for values in nginx\_gzip\_types | from\_yaml | list %}  
{{ values }}  
{% endfor %}

{% endblock %}

{% if nginx\_ssl\_conf %}  
{% for ssl in nginx\_ssl\_conf %}  
{{ ssl }}  
{% endfor %}  
{% endif %}

{% block http\_includes %}  
include {{ nginx\_sites\_path }};  
{% endblock %}

{% block http\_end %}{% endblock %}  
}

So far I cannot seem to get this to work and successfully store the examples above as variables and call them in the template based on the remote server. Because log\_format can appear more than once, how can I achieve my goal?

I tried creating a list but failed.
