salt icon indicating copy to clipboard operation
salt copied to clipboard

Feature request: addition of source for the blockreplace

Open joshFive opened this issue 11 years ago • 6 comments

I would like to use one of my previously created config files to append to a different config file. Normally, file.append should work, however each time the state is run, if there is any change, the whole block is added to the end, which is where the markers of blockreplace come in handy.

Here is an example:

kestrel_properties:
  file.blockreplace:
    - name: /etc/kestrel/kestrel.properties
    - template: jinja
    - marker_start: "# START Redis properties added via salt -DO-NOT-EDIT-"
    - marker_end: "# END Redis properties added via salt"
    - source: salt://qa/etc/cache.properties
    - append_if_not_found: True
    - backup: '.bak'
    - show_changes: True

Contents for cache.properties:

# START Redis properties added via salt -DO-NOT-EDIT-
redis.server1=cacheqa1
{% if (grains['host'] == 'sqa01a') -%}
redis.port1=6379
{% elif (grains['host'] == 'sqa02a') %}
redis.port1=6380
{% elif (grains['host'] == 'sqa03a') %}
redis.port1=6381
{% elif (grains['host'] == 'sqa04a') %}
redis.port1=6382
{% elif (grains['host'] == 'sqa05a') %}
redis.port1=6383
{% elif (grains['host'] == 'sqa06a') %}
redis.port1=6384
{% endif -%}
redis.password1=########

cache.region1=######
cache.memberinfo.evictSeconds=900

jedis.pool.maxActive = 64
jedis.pool.maxIdle = 8
jedis.pool.minIdle = 2
jedis.pool.maxWait = 100

jedis.pool.testWhileIdle = true
jedis.pool.testOnBorrow = true
jedis.pool.testOnReturn = true

jedis.pool.minEvictableIdleTimeMillis = 600000
jedis.pool.timeBetweenEvictionRunsMillis = 5000
jedis.pool.numTestsPerEvictionRun = 8

# END Redis properties added via salt

joshFive avatar Mar 10 '15 15:03 joshFive

@kuyote Thanks for this request. Have you tried using file.blockreplace?

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

rallytime avatar Mar 10 '15 17:03 rallytime

I've used block replace. The only way it will work, that I've found is to have all the entries in the content field inside the state. I couldn't find anything that reads the content from a file for the blockreplace.

joshFive avatar Mar 10 '15 19:03 joshFive

Ah, I wasn't looking closely. You've already got file.blockreplace in your state...

rallytime avatar Mar 10 '15 19:03 rallytime

+1 this would be really great, love blockreplace but having to hardcode strings in the .sls files is kind of ugly especially for multi-line "blockreplaces"

NoUsername avatar Mar 13 '15 21:03 NoUsername

I think that I've got a patch for this in https://github.com/saltstack/salt/compare/develop...rmcleay:templated_file.blockreplace

I need to make my local unit tests and lint work again prior to submitting the pull request, but the idea is to add support for doing the following:

foo:
  file.blockreplace:
    - source: salt://files/foo.tpl
    - template: 'jinja'
    - source_hash: 5357642471cfab4740d1d0a20c51af6a 
    - marker_start: '###start###'
    - marker_end: '###end###'
    - append_if_not_found: True
    - show_changes: False

@kuyote: Is this what you were after?

rmcleay avatar May 12 '15 07:05 rmcleay

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Nov 06 '17 15:11 stale[bot]