gpt4 book ai didi

ansible - blockinfile 不断添加 block

转载 作者:行者123 更新时间:2023-12-04 10:03:13 24 4
gpt4 key购买 nike

我想使用 blockinfile 在我的 ElasticSearch 配置中添加一个文本块,但是每次我运行我的剧本时,该块都会被添加到文件中——即使它已经存在。这是一个问题,因为 ElasticSearch 不只是采用最后一个值,它在启动时窒息说“你有这个值的多个条目”(或类似的东西)。

我的剧本是这样的:

  - 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文件看起来像:
## 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

有没有办法只添加不存在的块?

最佳答案

您应该指定 {mark} marker 中的关键字范围:

marker: "## {mark} added by ansible (configuration elasticsearch)"

这将导致 Ansible 在块的开头和结尾插入一行替换 {mark}相应地与 BEGINEND :
## 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 不知道块从哪里开始和在哪里结束,所以每次运行它都会认为块不存在并插入一个新块。

关于ansible - blockinfile 不断添加 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41284602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com