blockinfile 不断添加块 [英] blockinfile keeps adding the block
问题描述
我想使用 blockinfile 向我的 ElasticSearch 配置添加一个文本块,但是每次我运行我的剧本时,该块都会被添加到文件中——即使它已经存在.这是一个问题,因为 ElasticSearch 不只是采用最后一个值,它在启动时窒息说你有这个值的多个条目"(或类似的东西).
I'd like to add a block of text to my ElasticSearch configuration using blockinfile, but every time I run my playbook, the block gets added to the file -- even when it already exists. This is a problem because ElasticSearch doesn't just take the last value, it chokes on startup saying "you have multiple entries for this value" (or something similar).
我的剧本是这样的:
- name: configure elasticsearch
blockinfile:
dest: /etc/elasticsearch/elasticsearch.yml
marker: "## added by ansible configuration"
block: |
network.host: 0.0.0.0
path.data: /var/lib
path.logs: /var/log/elasticsearch
path.repo: /home/chris/elastic-backups
state: present
但是在第二次运行我的剧本后,我的 elasticsearch.yml
文件看起来像:
But after running my playbook a second time, my elasticsearch.yml
file looks like:
## added by ansible configuration
network.host: 0.0.0.0
path.data: /var/lib
path.logs: /var/log/elasticsearch
path.repo: /home/chris/elastic-backups
## added by ansible configuration
network.host: 0.0.0.0
path.data: /var/lib
path.logs: /var/log/elasticsearch
path.repo: /home/chris/elastic-backups
## added by ansible configuration
有没有办法只添加不存在的块?
Is there a way to only add the block if it does not exist yet?
推荐答案
您应该在 marker
参数中指定 {mark}
关键字:
You should specify {mark}
keyword in the marker
parameter:
marker: "## {mark} added by ansible (configuration elasticsearch)"
这将导致 Ansible 在块的开头和结尾插入一行,用 BEGIN
和 END
相应地替换 {mark}
>:
This will cause Ansible to insert a line at the beginning and at the end of the block replacing {mark}
accordingly with BEGIN
and END
:
## BEGIN added by ansible (configuration elasticsearch)
network.host: 0.0.0.0
path.data: /var/lib
path.logs: /var/log/elasticsearch
path.repo: /home/chris/elastic-backups
## END added by ansible (configuration elasticsearch)
否则 Ansible 不知道块的开始和结束位置,因此每次运行时它都会认为块不存在并插入一个新块.
Otherwise Ansible has no clue, where the block starts and where it ends, so on every run it considers the block is not present and inserts a new one.
这篇关于blockinfile 不断添加块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!