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}
Given the variables for testing
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
gives
It's not possible to get rid of the new lines created by the function to_nice_yaml, I think.
See: