英文:
Ansible - handle the fluentbit config update
问题
我有以下的 fluentbit 输入配置文件:
```conf
[INPUT]
name tail
tag tail_fluent_bit
path /var/log/fluent-bit.log
[INPUT]
name tail
tag tail_messages
path /var/log/messages
multiline on
我想要在每个 INPUT
块中添加另一个键值对。我考虑使用 Ansible 通过 with_lines
模块逐行读取此文件,将其转换为键值对的字典,并生成输入列表,然后添加所需的键。
例如,如果我想要添加 multiline
键,其值为 on
,我应该能够将其添加到每个 INPUT
块中(如果不存在的话)。
根据我的上述方法,我认为我在过度工程,因为我必须修剪每行中的空格并再次按空格拆分以生成键值对,但我的空格长度不是恒定的。
所以这就是我的输出应该是什么样子的:
[INPUT]
name tail
tag tail_fluent_bit
path /var/log/fluent-bit.log
multiline on
[INPUT]
name tail
tag tail_messages
path /var/log/messages
multiline on
在上面的输出中,请注意在第一个 INPUT
块中添加了 multiline on
。
我需要一些指导。
<details>
<summary>英文:</summary>
I have following fluentbit input config file:
```conf
[INPUT]
name tail
tag tail_fluent_bit
path /var/log/fluent-bit.log
[INPUT]
name tail
tag tail_messages
path /var/log/messages
multiline on
I wanted to add another key value to each INPUT
block. I was thinking to read this file line by line using Ansible through with_lines
module and convert to dictionary of key-value pair and generate a list of inputs and then add the required key.
For example, if I wanted to add multiline
key with value as on
, I should be able to add that to each and every INPUT
block if doesn't exist.
With my above approach, I believe I'm doing over-engineering because I have to trim spaces in each line and split by spaces again to make key-value pair, but my space length is not constant.
So this is what my output should be:
[INPUT]
name tail
tag tail_fluent_bit
path /var/log/fluent-bit.log
multiline on
[INPUT]
name tail
tag tail_messages
path /var/log/messages
multiline on
In above output, please note that multiline on
is added in first INPUT
block.
I need some direction here.
答案1
得分: 1
给定文件
```ini
shell> cat /tmp/fluentbit.conf
[INPUT]
name tail
tag tail_fluent_bit
path /var/log/fluent-bit.log
[INPUT]
name tail
tag tail_messages
path /var/log/messages
multiline on
请自行解析文件。读取文件
- command: cat /tmp/fluentbit.conf
register: fluentbit_conf_out
并声明结构
fluentbit_conf: |
{% filter from_yaml %}
{% for line in fluentbit_conf_out.stdout_lines|select %}
{% if line is regex('\[.*\]') %}
- {{ line[1:-1] }}:
{% else %}
{{ line|split|first }}: '{{ line|split|last }}'
{% endif %}
{% endfor %}
{% endfilter %}
得到
fluentbit_conf:
- INPUT:
name: tail
path: /var/log/fluent-bit.log
tag: tail_fluent_bit
- INPUT:
multiline: 'on'
name: tail
path: /var/log/messages
tag: tail_messages
声明默认值并更新配置
fluentbit_conf_defaults:
- INPUT:
multiline: 'on'
fluentbit_conf_update: "{{ fluentbit_conf_defaults|
product(fluentbit_conf)|
map('combine', recursive=true) }}"
得到
fluentbit_conf_update:
- INPUT:
multiline: 'on'
name: tail
path: /var/log/fluent-bit.log
tag: tail_fluent_bit
- INPUT:
multiline: 'on'
name: tail
path: /var/log/messages
tag: tail_messages
写入文件
- copy:
dest: /tmp/fluentbit.conf
content: |
{% for i in fluentbit_conf_update %}
{% for section,conf in i.items() %}
[{{ section }}]
{% for k,v in conf.items() %}
{{ '%-11s%-11s' % (k, v) }}
{% endfor %}
{% endfor %}
{% endfor %}
测试差异,使用选项 --check --diff
shell> ansible-playbook pb.yml --check --diff
...
任务 点击复制 ***********************************************************************************
--- 之前: /tmp/fluentbit.conf
+++ 之后: /home/admin/.ansible/tmp/ansible-local-3967647klph_pe0/tmpd8vimcjo
@@ -1,10 +1,12 @@
[INPUT]
- name tail
- tag tail_fluent_bit
- path /var/log/fluent-bit.log
+ multiline on
+ name tail
+ tag tail_fluent_bit
+ path /var/log/fluent-bit.log
[INPUT]
- name tail
+ multiline on
+ name tail
tag tail_messages
path /var/log/messages
- multiline on
+
并写入文件
shell> cat /tmp/fluentbit.conf
[INPUT]
multiline on
name tail
tag tail_fluent_bit
path /var/log/fluent-bit.log
[INPUT]
multiline on
name tail
tag tail_messages
path /var/log/messages
<hr>
<sup>
用于测试的完整播放书例子
shell> cat pb.yml
- hosts: localhost
vars:
fluentbit_conf: |
{% filter from_yaml %}
{% for line in fluentbit_conf_out.stdout_lines|select %}
{% if line is regex('\[.*\]') %}
- {{ line[1:-1] }}:
{% else %}
{{ line|split|first }}: '{{ line|split|last }}'
{% endif %}
{% endfor %}
{% endfilter %}
fluentbit_conf_defaults:
- INPUT:
multiline: 'on'
fluentbit_conf_update: "{{ fluentbit_conf_defaults|
product(fluentbit_conf)|
map('combine', recursive=true) }}"
tasks:
- command: cat /tmp/fluentbit.conf
register: fluentbit_conf_out
check_mode: false
- debug:
var: fluentbit_conf
- debug:
var: fluentbit_conf_update
- copy:
dest: /tmp/fluentbit.conf
content: |
{% for i in fluentbit_conf_update %}
{% for section,conf in i.items() %}
[{{ section }}]
{% for k,v in conf.items() %}
{{ '%-11s%-11s' % (k, v) }}
{% endfor %}
{% endfor %}
{% endfor %}
英文:
Given the file
shell> cat /tmp/fluentbit.conf
[INPUT]
name tail
tag tail_fluent_bit
path /var/log/fluent-bit.log
[INPUT]
name tail
tag tail_messages
path /var/log/messages
multiline on
Parse the file on your own. Read the file
- command: cat /tmp/fluentbit.conf
register: fluentbit_conf_out
and declare the structure
fluentbit_conf: |
{% filter from_yaml %}
{% for line in fluentbit_conf_out.stdout_lines|select %}
{% if line is regex('\[.*\]') %}
- {{ line[1:-1] }}:
{% else %}
{{ line|split|first }}: '{{ line|split|last }}'
{% endif %}
{% endfor %}
{% endfilter %}
gives
fluentbit_conf:
- INPUT:
name: tail
path: /var/log/fluent-bit.log
tag: tail_fluent_bit
- INPUT:
multiline: 'on'
name: tail
path: /var/log/messages
tag: tail_messages
Declare the defaults and update the configuration
fluentbit_conf_defaults:
- INPUT:
multiline: 'on'
fluentbit_conf_update: "{{ fluentbit_conf_defaults|
product(fluentbit_conf)|
map('combine', recursive=true) }}"
gives
fluentbit_conf_update:
- INPUT:
multiline: 'on'
name: tail
path: /var/log/fluent-bit.log
tag: tail_fluent_bit
- INPUT:
multiline: 'on'
name: tail
path: /var/log/messages
tag: tail_messages
Write the file
- copy:
dest: /tmp/fluentbit.conf
content: |
{% for i in fluentbit_conf_update %}
{% for section,conf in i.items() %}
[{{ section }}]
{% for k,v in conf.items() %}
{{ '%-11s%-11s' % (k, v) }}
{% endfor %}
{% endfor %}
{% endfor %}
Test the difference running with the options --check --diff
shell> ansible-playbook pb.yml --check --diff
...
TASK 点击复制 ***********************************************************************************
--- before: /tmp/fluentbit.conf
+++ after: /home/admin/.ansible/tmp/ansible-local-3967647klph_pe0/tmpd8vimcjo
@@ -1,10 +1,12 @@
[INPUT]
- name tail
- tag tail_fluent_bit
- path /var/log/fluent-bit.log
+ multiline on
+ name tail
+ tag tail_fluent_bit
+ path /var/log/fluent-bit.log
[INPUT]
- name tail
+ multiline on
+ name tail
tag tail_messages
path /var/log/messages
- multiline on
+
and write the file
shell> cat /tmp/fluentbit.conf
[INPUT]
multiline on
name tail
tag tail_fluent_bit
path /var/log/fluent-bit.log
[INPUT]
multiline on
name tail
tag tail_messages
path /var/log/messages
<hr>
<sup>
Example of a complete playbook for testing
shell> cat pb.yml
- hosts: localhost
vars:
fluentbit_conf: |
{% filter from_yaml %}
{% for line in fluentbit_conf_out.stdout_lines|select %}
{% if line is regex('\[.*\]') %}
- {{ line[1:-1] }}:
{% else %}
{{ line|split|first }}: '{{ line|split|last }}'
{% endif %}
{% endfor %}
{% endfilter %}
fluentbit_conf_defaults:
- INPUT:
multiline: 'on'
fluentbit_conf_update: "{{ fluentbit_conf_defaults|
product(fluentbit_conf)|
map('combine', recursive=true) }}"
tasks:
- command: cat /tmp/fluentbit.conf
register: fluentbit_conf_out
check_mode: false
- debug:
var: fluentbit_conf
- debug:
var: fluentbit_conf_update
- copy:
dest: /tmp/fluentbit.conf
content: |
{% for i in fluentbit_conf_update %}
{% for section,conf in i.items() %}
[{{ section }}]
{% for k,v in conf.items() %}
{{ '%-11s%-11s' % (k, v) }}
{% endfor %}
{% endfor %}
{% endfor %}
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论