salt icon indicating copy to clipboard operation
salt copied to clipboard

file.line mode=ensure state issues

Open haeac opened this issue 9 years ago • 7 comments

Description of Issue/Question

Hello,

The file.line with mode=ensure doesn't seem to be working as it should. I'm trying to ensure that a line is present in a file after another line. However, the regex for after or before don't seem to be working and also using location=end doesn't work as documented.

Thanks in advance.

Setup

Configuration file:

key1=value1
key2=value2
.
key4=value4,key=value, paramx
.
key=value    # This is the line after which I want my new line added
.
.
.
keyn=valuen

Steps to Reproduce Issue

Running the following on the master:

salt 'minion' file.line /path/to/configfile mode="ensure" content="mykey=myval" after="^key=value$"

Gives the following result:

ERROR: Found less than expected occurrences in "after" expression

On removing the end of line character $, I can get matches but salt complains about having more than expected number of matches. I can work around the $ by using key=value(\n|\r|\\r\n) but it will still match lines which don't start with key.

Also, as per the documentation, location takes precedence over before and after. So, if I use the following:

salt 'minion' file.line /path/to/configfile mode="ensure" content="mykey=myval" location="end"

I get the following error:

ERROR: Wrong conditions? Unable to ensure line without knowing where to put it before and/or after.

I can't use file.manged because files like the grub menu usually get updated when new kernels are installed.

Versions Report

Master version report:

Salt Version:
           Salt: 2016.11.0

Dependency Versions:
           cffi: 0.8.6
       cherrypy: Not Installed
       dateutil: Not Installed
          gitdb: 0.6.4
      gitpython: 1.0.1
          ioflo: 1.3.8
         Jinja2: 2.7.2
        libgit2: 0.21.0
        libnacl: 1.4.3
       M2Crypto: 0.21.1
           Mako: 0.8.1
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: Not Installed
      pycparser: 2.14
       pycrypto: 2.6.1
         pygit2: 0.21.4
         Python: 2.7.5 (default, Sep 15 2016, 22:37:39)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.3.0
           RAET: 0.6.3
          smmap: 0.9.0
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

System Versions:
           dist: centos 7.2.1511 Core
        machine: x86_64
        release: 3.10.0-327.36.3.el7.x86_64
         system: Linux
        version: CentOS Linux 7.2.1511 Core

Minion version report:

Salt Version:
           Salt: 2016.11.0

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 1.4.1
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.7.3
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: 0.20.2
           Mako: 0.3.4
   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.6.6 (r266:84292, Aug 18 2016, 15:13:37)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 14.5.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5

System Versions:
           dist: centos 6.8 Final
        machine: x86_64
        release: 2.6.32-642.6.2.el6.x86_64
         system: Linux
        version: CentOS 6.8 Final

haeac avatar Dec 08 '16 22:12 haeac

Thanks for reporting this, I have confirmed this behavior.

Daniel

gtmanfred avatar Dec 13 '16 16:12 gtmanfred

Thanks @gtmanfred , for now the work around is as follows:

(\r|\n|\r\n)key=value(\r|\n|\r\n)

haeac avatar Dec 14 '16 15:12 haeac

I see the same problem with file.replace and - flags: 0 running 2016.11.1

disaster123 avatar Dec 20 '16 20:12 disaster123

@disaster123 be careful using the flags param with file.replace. I believe the behavior you are seeing is expected when setting flags=0. file.replace treats the entire file as a string (using mmap to do this efficiently), so if you set - flags: 0 in the state you are telling file.replace to treat anchors like ^ and $ as belonging to the beginning and end of the file, not the line. Either remove flags from the state, or set it to flags: 8 (the default) and the anchors will refer to the line instead. This behavior is documented in the note for the flag param.

lorengordon avatar Dec 20 '16 23:12 lorengordon

Another workaround to use inline flags

(?m)^key=value$

doitian avatar Jan 17 '17 00:01 doitian

ERROR: Wrong conditions? Unable to ensure line without knowing where to put it before and/or after.

Problem also exists in 2016.3.6 when just trying to add a new line

hemebond avatar Jun 26 '17 23:06 hemebond

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 08 '18 23:11 stale[bot]