Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 314
Nathan Basanese Avatar asked Jan 24 '15 07:01

Nathan Basanese


2 Answers

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.

like image 174
skazi Avatar answered Sep 17 '22 15:09

skazi


The salt way to do this as I understand it: Place a template file for nexus.properties inside salt and use file.managed like shown in the docs http://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html

You will end up with something like:

/opt/nexus-2.8.0/conf/nexus.properties:
  file.managed:
    - source: salt://nexus/nexus.properties.jinja
    - template: jinja
    - defaults:
        bundleBasedir: "..."

You'll then use Jinja templating in your file:

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

See here for Jinja templating: http://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.jinja.html

I hope it helps.

like image 25
ahus1 Avatar answered Sep 17 '22 15:09

ahus1