簡體   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