【问题标题】:How can I make multiple replacements on the same file using one saltstack state?如何使用一种 saltstack 状态对同一个文件进行多次替换?
【发布时间】:2019-05-05 13:33:57
【问题描述】:

这是我的目标文件:

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

我知道使用 regex 或简单的 sed 脚本可以轻松做到这一点:

sed -i 's/${bundleBasedir}\/..\/my\/second\/path\/002\/\/nexus/\/myfirstdir001\/g'

但是,理想情况下,我更喜欢 saltstack 方式。

我希望它看起来像这样:

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 文档看起来相当简单:

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/'

我也许可以尝试多个状态 ID,但这似乎不优雅。

如果还有什么我生气的,请指教!

我很乐意找到解决此问题的方法。

另外,如果有人需要改进 salt 文档,我认为我的团队可能会被说服提供一些帮助。

这是我发现的最接近别人问这个问题的东西:

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

【问题讨论】:

    标签: replace nexus configuration-management salt-stack


    【解决方案1】:

    对于这么小的文件,我可能会使用 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.managedreplace: False 来初始化文件,如果它丢失但一旦存在,只有line 状态正在更改。

    【讨论】:

    • 感谢您的回答。请注意,我认为您需要在每个块的第一行末尾使用冒号。
    【解决方案2】:

    据我了解的 salt 方法:将 nexus.properties 的模板文件放在 salt 中并使用 file.managed ,如文档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: "..."
    

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

    # Jetty section
    application-port=8081
    application-host=0.0.0.0
    nexus-webapp={{ bundleBasedir }}/nexus
    nexus-webapp-context-path=/nexus
    

    查看 Jinja 模板:http://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.jinja.html

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2019-06-10
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多