gpt4 book ai didi

salt-stack - salt : text file to variable and use the same variable in state file to find&replace

转载 作者:行者123 更新时间:2023-12-02 09:26:08 25 4
gpt4 key购买 nike

我遇到了一个无法解决的问题:我有一个文件(/etc/osci),我在所有服务器上使用它作为我们的监控(zabbix)的名称我创建了一个状态文件,它将模板配置文件推送到服务器并将/etc/osci 的内容读取到变量中。下一步是使用同一变量与“file.replace”函数来搜索字符串并将其替换为该变量。

uusnimi=$(cat /etc/osci):
cmd.run

/etc/zabbix_agentd.conf:
file.managed:
- source: salt://base/streamingconf/zabbix/zabbix_agentd.conf
- mode: 644


change_hostname_zabbix:
file.replace:
- name: /etc/zabbix_agentd.conf
- pattern: 'Hostname='
- repl: 'Hostname=$uusnimi'

现在,当执行回显目标服务器中的变量的状态文件时,它为我提供了正确的输出:

echo $uusnimi
Server1

但对于我来说,我无法弄清楚如何转义上述代码的最后一行,因此它会插入值而不是“$uusnimi”字符串

最佳答案

使用 uusnimi 作为 jinja 变量。

{% set uusnimi = salt['cmd.shell']('cat /etc/osci') %}

/etc/zabbix_agentd.conf:
file.managed:
- source: salt://base/streamingconf/zabbix/zabbix_agentd.conf
- mode: 644

change_hostname_zabbix:
file.replace:
- name: /etc/zabbix_agentd.conf
- pattern: 'Hostname='
- repl: 'Hostname={{ uusnimi }}'

关于salt-stack - salt : text file to variable and use the same variable in state file to find&replace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37842020/

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