带有 jinja 模板的 cloud-init 脚本未通过 iinja 处理

带有 jinja 模板的 cloud-init 脚本未通过 iinja 处理

我有一个使用 jinja 模板的 cloud-init 脚本。它看起来像这样:

## template: jinja
#cloud-config

merge_how:
 - name: list
   settings: [append]
 - name: dict
   settings: [no_replace, recurse_list]

runcmd:
- mkdir -p /opt/collectd

write_files:
- path: /opt/collectd/collectd.conf
  content: |

    Hostname    "connect-${connect_group_id}"
    FQDNLookup   false
    Interval     60

    <LoadPlugin write_graphite>
    Globals false
    </LoadPlugin>

    LoadPlugin cpu
    LoadPlugin disk
    LoadPlugin df
    LoadPlugin memory
    LoadPlugin swap
    LoadPlugin interface
    LoadPlugin tcpconns

    <Plugin df>
        MountPoint "/hostfs"
        MountPoint "/hostfs/var/lib"
        MountPoint "/hostfs/var/log"
        IgnoreSelected false
        ReportInodes false
    </Plugin>

    <Plugin interface>
        Interface "eth0"
        IgnoreSelected false
    </Plugin>

    <Plugin swap>
        ReportByDevice false
        ReportBytes true
    </Plugin>

    <Plugin tcpconns>
        ListeningPorts false
        LocalPort "${jmx_port}"
        RemotePort "${jmx_port}"
    </Plugin>

    LoadPlugin processes
    <Plugin processes>
        CollectFileDescriptor true
        CollectContextSwitch false
        CollectMemoryMaps false
        ProcessMatch "zookeeper" ".*QuorumPeerMain.*"
        ProcessMatch "kafka" ".*SupportedKafka.*"
    </Plugin>
    # JMX Plugin Config

    LoadPlugin java

    {% macro mbean(objectName, name, type, instancePrefix, table, attribute) -%} 
    <MBean "{{ name }}">
        ObjectName "{{ objectName }}"
        <Value>
            Type "{{ type }}"
            InstancePrefix "{{ instancePrefix }}"
            Table {{ table }}
            Attribute "{{ attribute }}"
        </Value>
    </MBean>
    {% endmacro %}


    <Plugin java>
        JVMARG "-Djava.class.path=/usr/share/collectd/java/collectd-api.jar:/usr/share/collectd/java/generic-jmx.jar"
        LoadPlugin "org.collectd.java.GenericJMX"
        <Plugin "GenericJMX">
            {% set metrics=[
            "connector-count",
            "connector-startup-attempts-total",
            "connector-startup-failure-total",
            "connector-startup-failure-percentage",
            "connector-startup-success-total",
            "connector-startup-success-percentage",
            "task-count",
            "task-startup-attempts-total",
            "task-startup-failure-total",
            "task-startup-failure-percentage",
            "task-startup-success-total",
            "task-startup-success-percentage",
            ] -%}
            {% for name in metrics -%}
            {{ mbean(
                "kafka.connect:type=connect-worker-metrics",
                name,
                "counter",
                name,
                "false",
                "Value")
            }}
            {%- endfor %}

当此脚本被专门化并写入机器时,Jinja 标记不会被处理。

我自己拿来 collectd.conf 文件,用 jinja 运行,结果输出正确。所以 jinja 模板本身没有问题。

我还查看了 /var/log/cloud-init.log 和 /var/log/cloud-init-output.log 的输出,没有看到任何错误。

任何帮助表示感谢。

答案1

我找到了问题所在。我获取的 EC2 实例的 cloud-init 版本较旧(18.2),不支持 jinja 模板。

相关内容