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

33 views Asked by At

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

There are 1 answers

0
Vladimir Botka On

Given the variables for testing

    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.


See: