[英]Ansible Nested variables and Jinja2 templates
I'm trying to figure out why my jinja2 template (and ansible for that matter) cannot find my variables in my inventory file.我试图弄清楚为什么我的 jinja2 模板(以及 ansible 就此而言)无法在我的清单文件中找到我的变量。
Here is my inventory file:这是我的库存文件:
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
Then attempting to get the first title.然后试图获得第一个冠军。 I'm doing the following:
我正在做以下事情:
- name: debugging
debug:
var: filebeat.applog.title
when I run this I end up getting filebeat.applog.title: VARIABLE IS NOT DEFINED!
当我运行它时,我最终得到
filebeat.applog.title: VARIABLE IS NOT DEFINED!
which I think is good since it doesn't know what title I want.我认为这很好,因为它不知道我想要什么标题。 So changing this to
所以把这个改成
- name: debugging
debug:
var: filebeat.applog.0.title
I end up getting what I want filebeat.applog.0.title: Separate Application Log Path with Tags
.我最终得到了我想要
filebeat.applog.0.title: Separate Application Log Path with Tags
。 However, how do I use this in a jinja2 template?但是,如何在 jinja2 模板中使用它?
I have the following for a template, I know I need to update this to loop through the different items in my inventory.我有以下模板,我知道我需要更新它以遍历我库存中的不同项目。 That's a different problem on how to loop through this.
关于如何遍历它,这是一个不同的问题。
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 %}
each time I get the following, even when I do use {{ filebeat.applog.0.logtitle }}
in the template:每次我得到以下信息,即使我在模板中使用
{{ 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 %}
I'm not sure what I'm missing or doing wrong.我不确定我遗漏了什么或做错了什么。 I'm thinking I'm doing something wrong since this the first time doing something like this.
我在想我做错了什么,因为这是第一次做这样的事情。
So the template you have should either:因此,您拥有的模板应该:
for
loop to iterate over filebeat.applog
for
循环来遍历filebeat.applog
OR或者
filebeat.applog
filebeat.applog
的第n 个元素Aside from that, there are some errors like below:除此之外,还有一些错误如下:
1. 1.
target: {{ filebeat.applog.target | default "" }}
This is the main one, and this is what the error message is complaining about, ie got 'string'
.这是主要的,这就是错误消息所抱怨的,即
got 'string'
。 The correct usage for default
filter is {{ some_variable | default("") }}
default
过滤器的正确用法是{{ some_variable | default("") }}
{{ some_variable | default("") }}
. {{ some_variable | default("") }}
。
2. 2.
{% if filebeat.applog.multiline_pattern is defined %}
In the inventory this variable is mis-spelled, ie multiline_patern
(missing one "t").在清单中,此变量拼写错误,即
multiline_patern
(缺少一个“t”)。 Fix this in your inventory.在您的库存中修复此问题。
3. 3.
when I do use
{{ filebeat.applog.0.logtitle }}
in the template当我在模板中使用
{{ filebeat.applog.0.logtitle }}
This should be {{ filebeat.applog.0.title }}
to work.这应该是
{{ filebeat.applog.0.title }}
才能工作。
Considering the above fixes, a template that loops over filebeat.applog
such as below should work:考虑到上述修复,一个循环遍历
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.