简体   繁体   English

Ansible Jinja2,格式化

[英]Ansible Jinja2, formatting

so currently I run a for loop to produce a list of addresses, here is the loop: 所以目前我运行一个for循环来生成地址列表,这是循环:

sg_nodes_dn: "
  {%- set nodes = [] %}
  {%- for host in groups['elastic_nodes'] %}
  {{- nodes.append('CN=%s,OU=Systems/DevOps,O=x x x,L=x,C=x' % hostvars[host]['elk_node_name']) }}
  {%- endfor %}
  {{- nodes -}}"

This works great, however the problem I have is when I format it into a j2 template. 这很好用,但是我遇到的问题是将其格式化为j2模板时。

Here is the var inside my template: 这是模板中的var:

searchguard.nodes_dn:
    {{ sg_nodes_dn | to_nice_yaml }}

The problem with this is, it will print the first line fine, however the second line is not formatted in yaml and the service will fail to load, the outcome of this is. 这样做的问题是,它将很好地打印第一行,但是第二行未使用yaml格式化,因此该服务将无法加载,其结果是。

searchguard.nodes_dn:
    - CN=x.x-x.x,OU=Systems/DevOps,O=x x x,L=x,C=x
- CN=x.x-x.x,OU=Systems/DevOps,O=x x x,L=x,C=x

How can I ensure that the second line is properly formatted? 如何确保第二行格式正确? I did some brief reading and added: 我做了一些简短的阅读,并补充说:

#jinja2:trim_blocks: False

To the top of the file, but it didn't resolve the issue, can anyone other any input here? 在文件的顶部,但仍不能解决问题,这里是否可以输入其他任何内容?

You could use Jinja2 indent filter : 您可以使用Jinja2 indent过滤器:

searchguard.nodes_dn:
    {{ sg_nodes_dn | to_nice_yaml | indent(4, false) }}

Or simply use default parameters ( width=4 and indentfirst=False ), which are exactly what you need: 或者只是使用默认参数( width=4indentfirst=False ),这些正是您所需要的:

searchguard.nodes_dn:
    {{ sg_nodes_dn | to_nice_yaml | indent }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM