How can I make multiple replacements on the same file using one saltstack state?

Nathan Basanese picture Nathan Basanese · Jan 24, 2015 · Viewed 7.2k times · Source

Here's my target file:

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

I know there's an easy way to do this with regex or a simple sed script:

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

However, I would, ideally, prefer the saltstack way.

I would like it to look something like this:

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 

I haven't yet made sense of the saltstack documentation on this.

Saltstack's documentation for salt.states.file.replace seems fairly straightforward:

http://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#salt.states.file.replace

Here's what I tried:

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

I could maybe try multiple state IDs, but that seems inelegant.

If there's anything else I'm fuffing up, please advise!

I'd shore love to find a solution to this.

Also, if there's any demand for people improving the salt documentation, I think my team could be convinced to pitch in some.

Here's the closest thing I've found to someone else asking this question:

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

Answer

skazi picture skazi · Feb 23, 2017

For such a small file I would probably go with a template as ahus1 suggested.

If the file was bigger and/or we didn't want to control other lines just ensure that those two are correct, I think multiple state IDs (as mentioned by OP) is a good way to go. Something like:

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

I have a similar setup in my configuration but I use salt.states.file.line to replace some lines with my values. In addition I used salt.states.file.managed with a template and replace: False to initialize the file if it's missing but once it exists, only the line states are doing changes.