如何使用一个saltstack状态在同一个文件上进行多次替换?

Nat*_*ese 6 replace configuration-management nexus 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-work=/opt/nexuswork
runtime=${bundleBasedir}/nexus/WEB-INF
Run Code Online (Sandbox Code Playgroud)

我知道使用正则表达式或简单的sed脚本有一种简单的方法:

sed -i 's/${bundleBasedir}\/..\/my\/second\/path\/002\/\/nexus/\/myfirstdir001\/g'
Run Code Online (Sandbox Code Playgroud)

但是,理想情况下,我更喜欢盐栈方式.

我希望它看起来像这样:

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 
Run Code Online (Sandbox Code Playgroud)

我还没有弄清楚这方面的saltstack文档.

Saltstack的salt.states.file.replace文档似乎相当简单:

http://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#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/'
Run Code Online (Sandbox Code Playgroud)

我可以尝试多个状态ID,但这看起来不够优雅.

如果还有其他事我搞砸了,请指教!

我愿意寻找解决方案.

此外,如果有人要求人们改进盐文件,我认为我的团队可以说服一些人.

这是我发现给别人问这个问题的最接近的事情:

http://comments.gmane.org/gmane.comp.sysutils.salt.user/15138

ska*_*azi 5

对于这么小的文件,我可能会使用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/'
Run Code Online (Sandbox Code Playgroud)

我的配置中有类似的设置,但我常使用salt.states.file.line自己的值替换一些行。另外,我还使用salt.states.file.managed了模板,并replace: False在缺少文件时对文件进行初始化,但是一旦文件存在,只有line状态在进行更改。


ahu*_*us1 4

据我了解,执行此操作的盐方法:将 nexus.properties 的模板文件放入 salt 中并使用 file.management,如文档http://docs.saltstack.com/en/latest/ref/states/all中所示/salt.states.file.html

你最终会得到类似的结果:

/opt/nexus-2.8.0/conf/nexus.properties:
  file.managed:
    - source: salt://nexus/nexus.properties.jinja
    - template: jinja
    - defaults:
        bundleBasedir: "..."
Run Code Online (Sandbox Code Playgroud)

然后,您将在文件中使用 Jinja 模板:

# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-webapp={{ bundleBasedir }}/nexus
nexus-webapp-context-path=/nexus
Run Code Online (Sandbox Code Playgroud)

请参阅此处的 Jinja 模板:http://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.jinja.html

我希望它有帮助。