将多行jinja2块转换为单行

问题描述 投票:0回答:1

我使用ansible(使用jinja2)来管理nginx。我的nginx配置文件的行很长,很难维护:

add_header Content-Security-Policy "default-src 'self' http: https: https://*.foo.com: https://*.bar.com: https://*.baz.com: https://*.qux.com: https://*.spam.com: https://*.ham.com: https://*.eggs.com: wss://*.foo.com: object-src 'none'" always;

由于配置文件是jinja2模板,因此我希望将该单行重写为多行(以便于维护),但要如上所述将其转换为一行。

我该怎么做?换句话说,像这样的东西(当然是行不通的):

add_header Content-Security-Policy {%
"default-src 'self'
http:
https:
https://*.foo.com:
https://*.bar.com:
https://*.baz.com:
https://*.qux.com:
https://*.spam.com:
https://*.ham.com:
https://*.eggs.com:
wss://*.foo.com:
object-src 'none'"
always;%}

...转换后的哪一个,会给我上面显示的一个衬里?

nginx ansible jinja2 content-security-policy nginx-config
1个回答
0
投票
一种可能的解决方案:将所需的值放在列表中,并用空格将它们连接起来:

{%- set my_values=[ "default-src 'self'", "http:", "https:", "https://*.foo.com:", "https://*.bar.com:", "https://*.baz.com:", "https://*.qux.com:", "https://*.spam.com:", "https://*.ham.com:", "https://*.eggs.com:", "wss://*.foo.com:", "object-src 'none'", "always", ]-%} add_header Content-Security-Policy "{{ my_values | join(' ') }}";

蛋糕上的樱桃:将清单从清单/剧本中声明的变量直接传递到模板中。
© www.soinside.com 2019 - 2024. All rights reserved.