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在块的开头和结尾处插入一行,从而将{mark}
相应地替换为BEGIN
和END
:
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屋!