Replace 如何使用一个saltstack状态对同一文件进行多个替换?
这是我的目标文件:Replace 如何使用一个saltstack状态对同一文件进行多个替换?,replace,nexus,configuration-management,salt-stack,Replace,Nexus,Configuration Management,Salt Stack,这是我的目标文件: Sonatype Nexus # ============== # This is the most basic configuration of Nexus. # Jetty section application-port=8081 application-host=0.0.0.0 nexus-webapp=${bundleBasedir}/nexus nexus-webapp-context-path=/nexus # Nexus section nexus-wor
Sonatype Nexus
# ==============
# This is the most basic configuration of Nexus.
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
# Nexus section
nexus-work=/opt/nexuswork
runtime=${bundleBasedir}/nexus/WEB-INF
我知道有一种简单的方法可以使用sed脚本或简单的sed脚本实现这一点:
sed -i 's/${bundleBasedir}\/..\/my\/second\/path\/002\/\/nexus/\/myfirstdir001\/g'
然而,理想情况下,我更喜欢盐堆方式
我希望它看起来像这样:
Sonatype Nexus
# ==============
# This is the most basic configuration of Nexus.
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-webapp=/my/second/path/002/nexus # changed
nexus-webapp-context-path=/nexus
# Nexus section
nexus-work=/opt/nexuswork
runtime=/myfirstdir001/nexus/WEB-INF # changed
我还没有弄清楚关于这个的saltstack文档
Saltstack针对salt.states.file.replace的文档似乎相当简单:
以下是我尝试过的:
/opt/nexus-2.8.0/conf/nexus.properties
file: # state
- replace
- pattern: '\$\{bundleBasedir\}' # without escapes: '${bundleBasedir}/nexus'
- repl: '/my/second/path/002/nexus'
# - name: /opt/nexus-2.8.0/conf/nexus.properties
# - count=0
# - append_if_not_found=False
# - prepend_if_not_found=False
# - not_found_content=None
# - backup='.bak'
# - show_changes=True
- pattern: '\$\{bundleBasedir\}\/WEB-INF' # without escapes: ${bundleBasedir}/WEB-INF
- repl: '/myfirstdir001/'
我也许可以尝试多个州ID,但这似乎不雅观
如果我还有别的事要做,请告诉我
我很想找到解决这个问题的办法
此外,如果有人需要改进salt文档,我想我的团队可以被说服参与其中
这是我发现的最接近其他人的问题:
据我所知,salt实现这一点的方法是:在salt中放置nexus.properties的模板文件,并使用file.managed,如文档中所示 您将得到以下结果:
/opt/nexus-2.8.0/conf/nexus.properties:
file.managed:
- source: salt://nexus/nexus.properties.jinja
- template: jinja
- defaults:
bundleBasedir: "..."
然后在文件中使用Jinja模板:
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-webapp={{ bundleBasedir }}/nexus
nexus-webapp-context-path=/nexus
有关Jinja模板,请参见此处:
我希望它能有所帮助。对于这样一个小文件,我可能会按照ahus1的建议使用一个模板 如果文件较大,并且/或者我们不想控制其他行,只需确保这两行是正确的,我认为多状态ID(正如OP所提到的)是一个好方法。比如:
/opt/nexus-2.8.0/conf/nexus.properties-jetty:
file:
- replace
- name: /opt/nexus-2.8.0/conf/nexus.properties
- pattern: '\$\{bundleBasedir\}' # without escapes: '${bundleBasedir}/nexus'
- repl: '/my/second/path/002/nexus'
/opt/nexus-2.8.0/conf/nexus.properties-nexus:
file:
- replace:
- name: /opt/nexus-2.8.0/conf/nexus.properties
- pattern: '\$\{bundleBasedir\}\/WEB-INF' # without escapes: ${bundleBasedir}/WEB-INF
- repl: '/myfirstdir001/'
我的配置中有类似的设置,但我使用
salt.states.file.line
用我的值替换一些行。此外,我使用了带有模板的salt.states.file.managed
和replace:False
来初始化文件,如果它丢失了,但一旦它存在,只有行
状态正在做更改。感谢您的回答。请注意,我相信您需要在每个块的第一行末尾使用冒号。