salt icon indicating copy to clipboard operation
salt copied to clipboard

file.line mode=replace breaks on empty file.

Open jeffreyctang opened this issue 10 years ago • 4 comments

Platform: Ubuntu 14.04.3 LTS Salt version: salt 2015.8.3 (Beryllium)

if file.line mode=replace is confronted with an empty file, it causes an exception:

     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1624, in call
                  **cdata['kwargs'])
                File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1491, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python2.7/dist-packages/salt/states/file.py", line 2560, in line
                  backup=backup, quiet=quiet, indent=indent)
                File "/usr/lib/python2.7/dist-packages/salt/modules/file.py", line 1499, in line
                  for line in body.split(os.linesep)])
              TypeError: expected a character buffer object

If the file has one blank line, then this doesn't fail.

jeffreyctang avatar Feb 11 '16 17:02 jeffreyctang

@jeffreyctang, thanks for reporting.

jfindlay avatar Feb 11 '16 17:02 jfindlay

HI @jeffreyctang

I cannot reproduce this one. Can you please show me how do you get that trace?


root@ubuntu:~# touch /tmp/foo
root@ubuntu:~# cat /tmp/foo
root@ubuntu:~# salt '*' file.line /tmp/foo content="CREATEMAIL_SPOOL=no" match="CREATE_MAIL_SPOOL=yes" mode="replace"
ubuntu:
    Passed invalid arguments to file.line: expected a character buffer object

        .. versionadded:: 2015.8.0

        Edit a line in the configuration file.

        :param path:
            Filesystem path to the file to be edited.

        :param content:
            Content of the line.

        :param match:
            Match the target line for an action by
            a fragment of a string or regular expression.

        :param mode:
            Ensure
                If line does not exist, it will be added.

            Replace
                If line already exist, it will be replaced.

            Delete
                Delete the line, once found.

            Insert
                Insert a line.

        :param location:
            start
                Place the content at the beginning of the file.

            end
                Place the content at the end of the file.

        :param before:
            Regular expression or an exact case-sensitive fragment of the string.

        :param after:
            Regular expression or an exact case-sensitive fragment of the string.

        :param show_changes
            Output a unified diff of the old file and the new file.
            If ``False`` return a boolean if any changes were made.
            Default is ``True``

            .. note::

                Using this option will store two copies of the file in-memory
                (the original version and the edited version) in order to generate the diff.

        :param backup
            Create a backup of the original file with the extension:
            "Year-Month-Day-Hour-Minutes-Seconds".

        :param quiet
            Do not raise any exceptions. E.g. ignore the fact that the file that is
            tried to be edited does not exist and nothing really happened.

        :param indent
            Keep indentation with the previous line.

        If an equal sign (``=``) appears in an argument to a Salt command, it is
        interpreted as a keyword argument in the format of ``key=val``. That
        processing can be bypassed in order to pass an equal sign through to the
        remote shell command by manually specifying the kwarg:

        .. code-block:: bash

            salt '*' file.line /path/to/file content="CREATEMAIL_SPOOL=no" match="CREATE_MAIL_SPOOL=yes" mode="replace"

        CLI Examples:

        .. code-block:: bash

            salt '*' file.line /etc/nsswitch.conf "networks:    files dns", after="hosts:.*?", mode='ensure'

abednarik avatar Feb 21 '16 01:02 abednarik

I can reproduce this. The stack trace comes through when you run it with salt-call and -l debug. A fix is incoming.

rallytime avatar Nov 03 '16 20:11 rallytime

@jeffreyctang This is fixed by #37452. Thanks again for the good report here.

rallytime avatar Nov 03 '16 21:11 rallytime