在 Jinja2 中连接变量

concatenate variables in Jinja2

在 Ansible 中使用 Jinja2 进行模板化时,我正在努力处理来自 Satellite 服务器的以下示例数据:

"results": {
    "test.example.com": {
        "interfaces": "eth0,lo",
        "ipaddress_eth0": "10.251.0.45",
        "ipaddress_lo": "127.0.0.1",
        "netmask_eth0": "255.255.255.0",
        "netmask_lo": "255.0.0.0",
        "network_eth0": "10.251.0.0",
        "network_lo": "127.0.0.0",

这段代码会return:127.0.0.1,但是我想把'ipaddress_lo'换成可变接口。 我已经尝试了一百万种组合......没有任何运气......请指教。

{% for items in my_host_facts.json.results %}
{% for interface in my_host_facts.json.results[items].interfaces.split(",") %}
   {{ interface }}:
        address: "{{ my_host_facts.json.results[items].ipaddress_lo }}"
        netmask: "{{ my_subnet.json.mask }}"
        network: "{{ my_subnet.json.network }}"
        gateway: "{{ my_subnet.json.gateway }}"
{% endfor %}
    dns: [ "{{ my_subnet.json.dns_primary }}", "{{ my_subnet.json.dns_secondary }}" ]
{% endfor %}

亲切的问候,

尝试{{ my_host_facts.json.results[items]['ipaddress_'+interface] }}