Storing nginx variables and using in templates

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.