file.line.state mode: ensure is replacing ANY lines instead of adding/inserting
Description of Issue/Question
I am trying to add a single line to a nginx configuration file stored on the salt-master under the precondition that it does not yet exist. The idea is to add the line if it does not exist using the option: 'after', to ensure it is placed in a 'salt-managed' location inside the configuration file. Other lines may already be present in this 'salt-managed' location, inserted via the same process.
Note that the already existing lines inside the 'salt-managed' location cannot be matched beforehand to try and exclude them from being replaced since they are named dynamically coming out of a Jenkins build, and these naming parameters are not available upon subsequent state runs.
Salt seems to replace ANY line directly after the location specified in option 'after', instead of what I expected, which is to check IF the line EXIST after the location specified in option 'after', and IF it DOES NOT EXIST to insert the line directly after the location specified in option 'after'.
Setup
Note that in the SLS I am using a statically configured line instead of the dynamically generated line for purposes of testing. Also note that tests have been performed with and without " "/' ' in the Content and After options fields.
/srv/salt/states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls
file-line-post-jcis-build-reconf-nginx-conf: file.line: - name: /srv/salt/configs/nginx/uxg/nginx.conf - mode: ensure - content: "location /after-state-run { }" - after: "Start Saltstack managed location zone"
/srv/salt/configs/nginx/uxg/nginx.conf (BEFORE STATE RUN)
# Start Saltstack managed location zone
location /before-state-run { }
# End Saltstack managed location zone
/srv/salt/configs/nginx/uxg/nginx.conf (AFTER STATE RUN)
# Start Saltstack managed location zone
location /after-state-run { }
# End Saltstack managed location zone
Steps to Reproduce Issue
Salt Minion debug
[DEBUG ] LazyLoaded jinja.render
[DEBUG ] LazyLoaded yaml.render
[DEBUG ] In saltenv 'base', looking at rel_path 'states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls' to resolve 'salt://states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls'
[DEBUG ] In saltenv 'base', ** considering ** path '/var/cache/salt/minion/files/base/states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls' to resolve 'salt://states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls'
[DEBUG ] Fetching file from saltenv 'base', ** attempting ** 'salt://states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls'
[DEBUG ] No dest file found
[INFO ] Fetching file from saltenv 'base', ** done ** 'states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls'
[DEBUG ] compile template: /var/cache/salt/minion/files/base/states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls
[DEBUG ] Jinja search path: ['/var/cache/salt/minion/files/base']
[PROFILE ] Time (in seconds) to render '/var/cache/salt/minion/files/base/states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls' using 'jinja' renderer: 0.0044801235199
[DEBUG ] Rendered data from file: /var/cache/salt/minion/files/base/states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls:
# Reconfigure managed file
file-line-post-jcis-build-reconf-nginx-conf:
file.line:
- name: /srv/salt/configs/nginx/uxg/nginx.conf
- mode: ensure
- content: "location /after-state-run { }"
- after: "Start Saltstack managed location zone"
[DEBUG ] LazyLoaded config.get
[DEBUG ] Results of YAML rendering:
OrderedDict([('file-line-post-jcis-build-reconf-nginx-conf', OrderedDict([('file.line', [OrderedDict([('name', '/srv/salt/configs/nginx/uxg/nginx.conf')]), OrderedDict([('mode', 'ensure')]), OrderedDict([('content', 'location /after-state-run { }')]), OrderedDict([('after', 'Start Saltstack managed location zone')])])]))])
[PROFILE ] Time (in seconds) to render '/var/cache/salt/minion/files/base/states/nginx/uxg/post-jcis-build-reconf-nginx-conf.sls' using 'yaml' renderer: 0.00277709960938
[DEBUG ] LazyLoaded file.line
[INFO ] Running state [/srv/salt/configs/nginx/uxg/nginx.conf] at time 11:30:36.037241
[INFO ] Executing state file.line for /srv/salt/configs/nginx/uxg/nginx.conf
[DEBUG ] LazyLoaded file.line
[INFO ] File changed:
---
+++
@@ -466,7 +466,7 @@
} # Start Saltstack managed location zone - location /before-state-run { }+ location /after-state-run { } # End Saltstack managed location zone }
[INFO ] Completed state [/srv/salt/configs/nginx/uxg/nginx.conf] at time 11:30:36.052275 duration_in_ms=15.035
[DEBUG ] File /var/cache/salt/minion/accumulator/140686033847952 does not exist, no need to cleanup.
[DEBUG ] Minion return retry timer set to 10 seconds (randomized)
[INFO ] Returning information for job: 20170715113035699766
[DEBUG ] Initializing new AsyncZeroMQReqChannel for ('/etc/salt/pki/minion', 'ip-10-0-150-80.eu-central-1.compute.internal', 'tcp://10.0.150.80:4506', 'aes')
[DEBUG ] Initializing new AsyncAuth for ('/etc/salt/pki/minion', 'ip-10-0-150-80.eu-central-1.compute.internal', 'tcp://10.0.150.80:4506')
[DEBUG ] grains target: role:uxg
[DEBUG ] Attempting to match 'uxg' in 'role' using delimiter ':'
Versions Report
Salt Version: Salt: 2016.11.3
Dependency Versions: cffi: Not Installed cherrypy: 3.5.0 dateutil: 2.6.0 gitdb: 0.6.4 gitpython: 1.0.1 ioflo: Not Installed Jinja2: 2.8 libgit2: Not Installed libnacl: Not Installed M2Crypto: Not Installed Mako: 1.0.3 msgpack-pure: Not Installed msgpack-python: 0.4.6 mysql-python: Not Installed pycparser: Not Installed pycrypto: 2.6.1 pygit2: Not Installed Python: 2.7.12 (default, Nov 19 2016, 06:48:10) python-gnupg: Not Installed PyYAML: 3.12 PyZMQ: 15.2.0 RAET: Not Installed smmap: 0.9.0 timelib: Not Installed Tornado: 4.2.1 ZMQ: 4.1.4
System Versions: dist: Ubuntu 16.04 xenial machine: x86_64 release: 4.4.0-66-generic system: Linux version: Ubuntu 16.04 xenial
I am able to reproduce this both on v2016.11.3, the 2016.11 branch and develop. This issue appears to be symptom of a couple problems I can see.
The first and most trivial issue to fix is the documentation for the before and after options leave a big question in the users head of what they actually do. To me after means "Only touch lines after this line" and before means "Only touch lines before this line" but in the implementation (at least for ensure mode) after clearly means "Immediately following this line" and before is similar. In fact you even get an error if there is more than one line between your before and after matches. Regardless of which meaning the project chooses, the documentation should make it clear.
The second issue is that the implementation (at least for ensure mode) uses world level matching to determine if it should prepend the contents or if it should replace the first line with the contents. For example to help make what I am saying clear:
If you were using the following nginx.conf and ran your example state /srv/salt/configs/nginx/uxg/nginx.conf (BEFORE STATE RUN)
# Start Saltstack managed location zone
locatioX /before-state-run { }
# End Saltstack managed location zone
It would result in the following /srv/salt/configs/nginx/uxg/nginx.conf (AFTER STATE RUN)
# Start Saltstack managed location zone
location /after-state-run { }
locatioX /before-state-run { }
# End Saltstack managed location zone
It is not clear to me why file.line uses world level matching at all, although there are undoubtedly salt users unknowingly relying on this bug. For example a user using the following teststate would be relying on this bug to prevent duplicate AllowUsers options in their sshd_config
/etc/ssh/sshd_config
...
#SSH users
AllowUsers Alpha Bravo Charley
/srv/salt/teststate.sls
{% set users = ['Josh', 'Alice', 'Bob', 'Charley'] %}
ssh-conf-users:
file.line:
- name: /etc/ssh/sshd_config
- mode: ensure
- content: "AllowUsers {{ ' '.join(users) }}"
- after: "SSH users"
Although you could easily get the same behavior using match without relying on the bug.
I would love some advice on how to proceed with fixing the latter issue. The best approach I can think of at time of writing:
- Implement a "fix/workaround" for 2016.11 in the form of an added option to disable the word matching behavior (that defaults to being the same behavior that is has been)
- Merge forward the "fix/workaround" into 2017.7 and develop
- Remove the word matching behavior from develop and depreciate the added option (would be a breaking change for users relying on the bug)
Could you take a look at all the bugs mentioned here, https://github.com/saltstack/salt/issues/42075
I believe that this may be a duplicate of one of those.
Thanks, Daniel
I think the software issue experienced here is the same as the one experienced in #37360
#37360 seems to fit the bill on what I am seeing.
I think conceptually file.line with the given current options is very valid and it is my guess that were it given some additional love it would certainly be used more effectively by many people who are trying to accurately and reliably manage full service chains. I have not yet dug into file.replace which was mildly suggested as possible 'replacement' to file.line so I will spend some time getting into that function to see what it can do for my use case.
The Saltstack team probably has a much better view on the possible functional requirements generally out there in the community so I will humbly forego on attempting to formulate anything here so broad in scope. Having said that the 'ensure' option should to my mind do what is stated: 'IF line DOES NOT exist, it will be ADDED' and BEFORE or AFTER the provided positional marker, instead of replacing lines using what currently seems to be non-exact matching. Personally I think just matching the line directly before or after the positional marker would be too tight a match, since some use cases have 'saltstack dynamically managed blocks' inside otherwise static files, so the content of those locations is not always known beforehand.
Let me just finish up for the night by saying I really do love this software. Great job by the team and all the people providing feedback here.
That was the same issue i was thinking of.
Thanks for the feedback, if you wouldn't mind providing it in the overall issue that I linked that mentions all the issues with file.line, I would appreciate it.
I am going to close this issue as a duplicate of #37360
Thanks! Daniel