Bug #17703
closedJinja2 vars.sys.ipv4 issue
Description
Hi,
it seems like via Jinja2 the 'sys.ipv4[eth0]' is not properly resolved.
# cf-promises --show-vars |grep sys.ipv4
default:sys.ipv4 192.168.1.22 inventory,source=agent,attribute_name=none
default:sys.ipv4[eth0] 192.168.1.22 source=agent
default:sys.ipv4[eth1] 10.0.1.22 source=agent
default:sys.ipv4[lo] 127.0.0.1 source=agent
[...]
# rudder agent run -qu
[...]
E| repaired fileTemplate Load Template from a fil| /root/ips.txt | The copy of the file /var/rudder/configuration-repository/shared-files/ips.tpl from the policy server to /var/rudder/tmp/templates/ips.tpl was repaired
E| repaired fileTemplate Expand template /root/ips.txt | The expansion of the template /var/rudder/tmp/templates/ips.tpl in /root/ips.txt was repaired
[...]
# cat /var/rudder/configuration-repository/shared-files/ips.tpl
eth0: {{ vars.sys.ipv4[eth0] }}
eth1: {{ vars.sys.ipv4[eth1] }}
# cat /root/ips.txt
#
Tried so far:
{{ vars.sys.ipv4[eth0] }}
{{ vars.sys.ipv4.eth0 }}
{{ vars.sys.ipv4'['eth0']' }}
{{ vars.sys.ipv4'[eth0]' }}
Best regards,
Alex
[1] https://jinja.palletsprojects.com/en/2.11.x/templates/#variables
Rudder agent: 6.0.5.releaseJinja2:
- python36-jinja2-2.11.1-1.el7.noarch (CentOS 7)
- python3-Jinja2-2.8-19.17.1.noarch (SLES12 SP4)
Updated by François ARMAND over 4 years ago
In jinja, access to variable is all doted: {{vars.sys.ipv4.eth0}}
We clarified all this difference in new variable documentation here: https://docs.rudder.io/reference/6.1/usage/variables.html (it's for 6.1, but that part is the same in previous version).
Hope it helps
Updated by Alexander Brunhirl over 4 years ago
In jinja, access to variable is all doted:
{{vars.sys.ipv4.eth0}}
I tried it but didn't work as expected. The output file is still rendered empty
# rudder agent run -qu
[...]
E| repaired fileTemplate Load Template from a fil| /root/ips.txt | The copy of the file /var/rudder/configuration-repository/shared-files/ips.tpl from the policy server to /var/rudder/tmp/templates/ips.tpl was repaired
E| repaired fileTemplate Expand template /root/ips.txt | The expansion of the template /var/rudder/tmp/templates/ips.tpl in /root/ips.txt was repaired
[...]
# cat /var/rudder/configuration-repository/shared-files/ips.tpl
eth0: {{ vars.sys.ipv4.eth0 }}
eth1: {{ vars.sys.ipv4.eth1 }}
# cat /root/ips.txt
#
Updated by Alexis Mousset over 4 years ago
The problem is that the system variable is not an array:
"ipv4": "10.0.2.15", "ipv4[enp0s3]": "10.0.2.15", "ipv4[enp0s8]": "192.168.41.2", "ipv4[lo]": "127.0.0.1", "ipv4_1[enp0s3]": "10", "ipv4_1[enp0s8]": "192", "ipv4_1[lo]": "127", "ipv4_2[enp0s3]": "10.0", "ipv4_2[enp0s8]": "192.168", "ipv4_2[lo]": "127.0", "ipv4_3[enp0s3]": "10.0.2", "ipv4_3[enp0s8]": "192.168.41", "ipv4_3[lo]": "127.0.0",
so we need to access ipv4_3[lo]
as key name.
jinja2 has actually two syntaxes for key access : . or [''] (with quotes), so as the variable name contains [] we need to use the second one
{{ vars.sys['ipv4[enp0s3]'] }}
should work.
Updated by Alexander Brunhirl over 4 years ago
jinja2 has actually two syntaxes for key access : . or [''] (with quotes), so as the variable name contains [] we need to use the second one
{{ vars.sys['ipv4[enp0s3]'] }}
should work.
perfect, now it works like a charm. I thank you :)
# rudder agent run -qu
[...]
E| repaired fileTemplate Load Template from a fil| /root/ips.txt | The copy of the file /var/rudder/configuration-repository/shared-files/ips.tpl from the policy server to /var/rudder/tmp/templates/ips.tpl was repaired
E| repaired fileTemplate Expand template /root/ips.txt | The expansion of the template /var/rudder/tmp/templates/ips.tpl in /root/ips.txt was repaired
[...]
# cat /var/rudder/configuration-repository/shared-files/ips.tpl
eth0: {{ vars.sys['ipv4[eth0]'] }}
eth1: {{ vars.sys['ipv4[eth1]'] }}
# cat /root/ips.txt
192.168.1.22
10.0.1.22
#