繁体   English   中英

Ansible 嵌套变量和 Jinja2 模板

[英]Ansible Nested variables and Jinja2 templates

我试图弄清楚为什么我的 jinja2 模板(以及 ansible 就此而言)无法在我的清单文件中找到我的变量。

这是我的库存文件:

all:
  hosts:
    test05:
      ansible_host: 192.168.x.x
      filebeat:
        version: 7.15.2
        applog:
          - title: Separate Application Log Path with Tags
            type: log
            paths: 
              - /var/log/something/moresomething/current
            tags: '["something", "application"]'
          - title: Separate Application Log Path, with Tags, and "decode_json_fields" processor.
            type: log
            paths:
              - /var/log/something/moresomething/blah-shell.log
            tags: ["application", "something"]
            fields: ["message"]
            depth: 2
          - title: Separate Application Log Path, with Tags, and Multiline fields
            type: log
            paths:
              - /var/log/something/moresomething/production.log
            tags: ["application", "something"]
            multiline_type: pattern
            multiline_patern: 'Started'
            multiline_negate: true
            multiline_match: after

然后试图获得第一个冠军。 我正在做以下事情:

- name: debugging
  debug:
    var: filebeat.applog.title

当我运行它时,我最终得到filebeat.applog.title: VARIABLE IS NOT DEFINED! 我认为这很好,因为它不知道我想要什么标题。 所以把这个改成

- name: debugging
  debug:
    var: filebeat.applog.0.title

我最终得到了我想要filebeat.applog.0.title: Separate Application Log Path with Tags 但是,如何在 jinja2 模板中使用它?

我有以下模板,我知道我需要更新它以遍历我库存中的不同项目。 关于如何遍历它,这是一个不同的问题。

title: {{ filebeat.applog.title }}
  - type: {{ filebeat.applog.type }}
    enabled: true
    paths:
      - {{ filebeat.applog.path }}
   tags: {{ filebeat.applog.tags }}
{% if filebeat.applog.fields is defined %}
  processors:
    - decode_json_fields:
        fields: {{ filebeat.applog.fields }}
        max_depth: {{ filebeat.applog.depth }}
        target: {{ filebeat.applog.target | default "" }}
{% endif %}
{% if filebeat.applog.multiline_pattern  is defined %}
  multiline.type: {{ filebeat.applog.multiline_type }}
  multiline.pattern: {{ filebeat.applog.multiline_pattern }}
  multiline.negate: {{ filebeat.applog.multiline_negate }}
  multiline.match: {{ filebeat.applog.multiline_match }}
{% endif %}

每次我得到以下信息,即使我在模板中使用{{ filebeat.applog.0.logtitle }}也是如此:

fatal: [test05]: FAILED! => changed=false
  msg: |-
    AnsibleError: template error while templating string: expected token 'end of print statement', got 'string'. String: title: {{ filebeat.applog.title }}
      - type: {{ filebeat.applog.type }}
        enabled: true
        paths:
          - {{ filebeat.applog.path }}
       tags: {{ filebeat.applog.tags }}
    {% if filebeat.applog.fields is defined %}
      processors:
        - decode_json_fields:
            fields: {{ filebeat.applog.fields }}
            max_depth: {{ filebeat.applog.depth }}
            target: {{ filebeat.applog.target | default "" }}
    {% endif %}
    {% if filebeat.applog.multiline_pattern  is defined %}
      multiline.type: {{ filebeat.applog.multiline_type }}
      multiline.pattern: {{ filebeat.applog.multiline_pattern }}
      multiline.negate: {{ filebeat.applog.multiline_negate }}
      multiline.match: {{ filebeat.applog.multiline_match }}
    {% endif %}

我不确定我遗漏了什么或做错了什么。 我在想我做错了什么,因为这是第一次做这样的事情。

因此,您拥有的模板应该:

  • 有一个for循环来遍历filebeat.applog

或者

  • 引用filebeat.applog的第n 个元素

除此之外,还有一些错误如下:

1.

target: {{ filebeat.applog.target | default "" }}

这是主要的,这就是错误消息所抱怨的,即got 'string' default过滤器的正确用法是{{ some_variable | default("") }} {{ some_variable | default("") }}

2.

{% if filebeat.applog.multiline_pattern is defined %}

在清单中,此变量拼写错误,即multiline_patern (缺少一个“t”)。 在您的库存中修复此问题。

3.

当我在模板中使用{{ filebeat.applog.0.logtitle }}

这应该是{{ filebeat.applog.0.title }}才能工作。


考虑到上述修复,一个循环遍历filebeat.applog的模板应该可以工作,如下所示:

{% for applog in filebeat.applog %}
title: {{ applog.title }}
  - type: {{ applog.type }}
    enabled: true
    paths: {{ applog.paths }}
    tags: {{ applog.tags }}
{% if applog.fields is defined %}
  processors:
    - decode_json_fields:
        fields: {{ applog.fields }}
        max_depth: {{ applog.depth }}
        target: {{ applog.target | default("") }}
{% endif %}
{% if applog.multiline_pattern is defined %}
  multiline.type: {{ applog.multiline_type }}
  multiline.pattern: {{ applog.multiline_pattern }}
  multiline.negate: {{ applog.multiline_negate }}
  multiline.match: {{ applog.multiline_match }}
{% endif %}
{% endfor %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM