如何应用正确的过滤器以获得具有正确缩进的Jinja变量列表。

huangapple go评论77阅读模式
英文:

How to apply right filter to get list of variables to an jinga variable with correct indentation

问题

以下是翻译好的部分:

我有一个在Ansible角色中使用变量的模板,这些变量在vars.yaml中定义。

模板如下所示:

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        {{ vm_routes }}

vm_routes的变量在vars.yaml中传递如下:

vm_routes: 
  - to: 0.0.0.0/0
    via: 192.168.0.1
  - to: 192.168.0.0/16
    via: 192.168.0.101

但是在运行Playbook后,我得到的变量值没有保持缩进。输出如下所示:

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        [{'to': '0.0.0.0/0', 'via': '192.168.0.1'}, {'to': '192.168.0.0/16', 'via': '192.168.0.101'}}

但是我需要输出正确缩进的结果,期望的输出如下:

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        - to: 0.0.0.0/0
          via: 192.168.0.1
        - to: 192.168.0.0/16
          via: 192.168.0.101

有人可以帮助我如何以正确的缩进格式获取Jinja变量的值吗?提前感谢。

我尝试使用Jinja模板的过滤器,如"to_yaml"和"to_nice_yaml",但仍然存在相同的缩进问题。

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        {{ vm_routes | to_yaml }}

输出如下所示:

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        - {to: 0.0.0.0/0, via: 192.168.0.1}
        - {to: 192.168.0.0/16, via: 192.168.0.101}
英文:

I have an template in ansible role that uses variable and the variables are defined in vars.yaml

the template looks like:

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        {{ vm_routes }}

the variable for vm_routes are passed as below in vars.yaml

vm_routes: 
  - to: 0.0.0.0/0
    via: 192.168.0.1
  - to: 192.168.0.0/16
    via: 192.168.0.101

but after running the playbook i am getting the values for variable without maintaining indentation. the output looks like below

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        [{'to': '0.0.0.0/0', 'via': '192.168.0.1'}, {'to': '192.168.0.0/16', 'via': '192.168.0.101'}}

But i need the output to be correctly indented, the expected output is

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        - to: 0.0.0.0/0
          via: 192.168.0.1
        - to: 192.168.0.0/16
          via: 192.168.0.101

Can anyone help me how to get the values for jinga variables in the correct format with proper indentation. Thanks in advance

I tried using jinga filters for templates such as "to_yaml" and "to_nice_yaml" , but left out with same indentation problem

network:
  version: 2
  ethernets:
    {{ vm_guest_interface }}:
      dhcp4: no
      addresses: [{{ vm_static_ip }}]
      nameservers:
           addresses: [{{ vm_dns_servers }}]
      routes: 
        {{ vm_routes | to_yaml }}

output looks like

network:
version: 2
ethernets:
{{ vm_guest_interface }}:
dhcp4: no
addresses: [{{ vm_static_ip }}]
nameservers:
addresses: [{{ vm_dns_servers }}]
routes:
- {to: 0.0.0.0/0, via: 192.168.0.1}

  • {to: 192.168.0.0/16, via: 192.168.0.101}

答案1

得分: 1

给定用于测试的变量

```yaml
    vm_guest_interface: eth0
    vm_static_ip:
      - 10.2.0.11
    vm_dns_servers:
      - 10.2.0.10
    vm_routes: 
      - to: 0.0.0.0/0
        via: 192.168.0.1
      - to: 192.168.0.0/16
        via: 192.168.0.101

使用 Jinja 过滤器 indent 对整个结构进行缩进。在需要时还可以使用函数 to_nice_yaml 设置结构内部的缩进。例如,模板

    - debug:
        msg: |
          network:
            version: 2
            ethernets:
              {{ vm_guest_interface }}:
                dhcp4: no
                addresses:
                  {{ vm_static_ip|to_nice_yaml|indent(8) }}
                nameservers:
                  addresses:
                    {{ vm_dns_servers|to_nice_yaml|indent(10) }}
                routes: 
                  {{ vm_routes|to_nice_yaml(indent=2)|indent(8) }}          

得到

    network:
      version: 2
      ethernets:
        eth0:
          dhcp4: no
          addresses:
            - 10.2.0.11
  
          nameservers:
            addresses:
              - 10.2.0.10
  
          routes:
            - to: 0.0.0.0/0
              via: 192.168.0.1
            - to: 192.168.0.0/16
              via: 192.168.0.101

无法消除由函数 to_nice_yaml 创建的新行,我认为。

<hr>

<sup>

参见:

</sup>


<details>
<summary>英文:</summary>

Given the variables for testing

```yaml
    vm_guest_interface: eth0
    vm_static_ip:
      - 10.2.0.11
    vm_dns_servers:
      - 10.2.0.10
    vm_routes: 
      - to: 0.0.0.0/0
        via: 192.168.0.1
      - to: 192.168.0.0/16
        via: 192.168.0.101

Use Jinja filter indent to indent the whole structure. Set also the indentation inside the structure by the function to_nice_yaml when needed. For example, the template

    - debug:
        msg: |
          network:
            version: 2
            ethernets:
              {{ vm_guest_interface }}:
                dhcp4: no
                addresses:
                  {{ vm_static_ip|to_nice_yaml|indent(8) }}
                nameservers:
                  addresses:
                    {{ vm_dns_servers|to_nice_yaml|indent(10) }}
                routes: 
                  {{ vm_routes|to_nice_yaml(indent=2)|indent(8) }}          

gives

    network:
      version: 2
      ethernets:
        eth0:
          dhcp4: no
          addresses:
            - 10.2.0.11
  
          nameservers:
            addresses:
              - 10.2.0.10
  
          routes:
            - to: 0.0.0.0/0
              via: 192.168.0.1
            - to: 192.168.0.0/16
              via: 192.168.0.101

It's not possible to get rid of the new lines created by the function to_nice_yaml, I think.

<hr>

<sup>

See:

</sup>

huangapple
  • 本文由 发表于 2023年7月13日 21:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定