【问题标题】:concatenate variables in Jinja2在 Jinja2 中连接变量
【发布时间】:2017-04-05 09:25:01
【问题描述】:

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

"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",

这段代码会返回: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 %}

亲切的问候,

【问题讨论】:

    标签: ansible jinja2 satellite


    【解决方案1】:

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

    【讨论】:

    • ...如果我尝试这个结果是:" "。有什么想法吗?我正在尝试匹配字符串变量,这些变量有时只有一个空格甚至 NULL(空字段)的值。
    猜你喜欢
    • 2011-03-08
    • 2019-07-08
    • 2015-10-21
    • 2015-07-27
    • 1970-01-01
    • 2017-04-13
    • 2022-08-07
    • 2018-03-24
    • 2015-10-02
    相关资源
    最近更新 更多