salt icon indicating copy to clipboard operation
salt copied to clipboard

`file.line` state doesn't take `show_changes` into account for new files

Open JensRantil opened this issue 10 years ago • 10 comments

file.line state doesn't take show_changes into account for new files. The parameter show_changes is not used here: https://github.com/saltstack/salt/blob/eb6773823bd15188fc2592e7afaaa045572026f2/salt/states/file.py#L2721

I haven't tested this, but think this is not-so-serious bug.

JensRantil avatar Apr 07 '16 13:04 JensRantil

@JensRantil, thanks for the report.

jfindlay avatar Apr 07 '16 17:04 jfindlay

HI @JensRantil / @jfindlay

After digging arounf this, I think there are a few issues here. This is just an example

cat /srv/salt/source.sls
test:
  file.line:
    - name: /tmp/foo
    - mode: ensure
    - content: 'whatever'
    - create: True
    - show_changes: True
    - file_mode: 0644
root@ubuntu:~# salt ubuntu state.apply source
ubuntu:
----------
          ID: test
    Function: file.line
        Name: /tmp/foo
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/root/salt/salt/state.py", line 1703, in call
                  **cdata['kwargs'])
                File "/root/salt/salt/loader.py", line 1661, in wrapper
                  return f(*args, **kwargs)
                File "/root/salt/salt/states/file.py", line 2730, in line
                  backup=backup, quiet=quiet, indent=indent)
                File "/root/salt/salt/modules/file.py", line 1626, in line
                  raise CommandExecutionError("Wrong conditions? "
              CommandExecutionError: Wrong conditions? Unable to ensure line without knowing where to put it before and/or after.
     Started: 12:30:50.381331
    Duration: 25.477 ms
     Changes:

Summary for ubuntu
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
ERROR: Minions returned with non-zero exit code

Since file is new, hot do you setup something valid for after/before? This also happen when you don't set same value for mode or file_mode.

abednarik avatar Apr 08 '16 12:04 abednarik

Since file is new, hot do you setup something valid for after/before?

I don't know. I just stumbled across this when reading code so I actually never set up a testcase.

JensRantil avatar Apr 08 '16 12:04 JensRantil

@abednarik, I haven't investigated this issue very much, but from my experience, the file.managed function will show something like new file for changes when the file is new. I suppose something similar could be done here as well.

jfindlay avatar Apr 08 '16 17:04 jfindlay

Ok, that for that. Let's see if someone find something that I'm missing here. I will try to dig a little more during the weekend.

abednarik avatar Apr 08 '16 17:04 abednarik

HI

After digging a little more, this report is invalid I think. Here salt will take care of creating the file and set proper atributes but is not the place where salt handle the content, while this is where file.line is called and as you can see show_changes is present.

I do think someone needs to review this, since this call will fail here because both after and before defaults are none

abednarik avatar Apr 16 '16 19:04 abednarik

@abednarik, thanks for your research on this.

jfindlay avatar Apr 18 '16 18:04 jfindlay

Still seeing this issue with the following code;

State

{% for k, v in salt['pillar.get']('environment:present', {}).iteritems() %}
environment_{{ k }}:
  file.line:
    - name: /etc/environment
    - mode: ensure
    - content: "{{ k }}={{ v }}"

{% endfor %}

Pillar;

environment:
  present:
    FOO: foo
    BAR: bar

Resulting output;

----------
          ID: environment_FOO
    Function: file.line
        Name: /etc/environment
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python2.7/site-packages/salt/state.py", line 1723, in call
                  **cdata['kwargs'])
                File "/usr/lib/python2.7/site-packages/salt/loader.py", line 1650, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python2.7/site-packages/salt/states/file.py", line 2711, in line
                  backup=backup, quiet=quiet, indent=indent)
                File "/usr/lib/python2.7/site-packages/salt/modules/file.py", line 1626, in line
                  raise CommandExecutionError("Wrong conditions? "
              CommandExecutionError: Wrong conditions? Unable to ensure line without knowing where to put it before and/or after.
     Started: 17:40:19.535163
    Duration: 11.277 ms
     Changes:
----------
          ID: environment_BAR
    Function: file.line
        Name: /etc/environment
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python2.7/site-packages/salt/state.py", line 1723, in call
                  **cdata['kwargs'])
                File "/usr/lib/python2.7/site-packages/salt/loader.py", line 1650, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python2.7/site-packages/salt/states/file.py", line 2711, in line
                  backup=backup, quiet=quiet, indent=indent)
                File "/usr/lib/python2.7/site-packages/salt/modules/file.py", line 1626, in line
                  raise CommandExecutionError("Wrong conditions? "
              CommandExecutionError: Wrong conditions? Unable to ensure line without knowing where to put it before and/or after.
     Started: 17:40:19.546681
    Duration: 0.793 ms
     Changes:

Is this because /etc/environment is an already-existing file, with no line return?

hartloosli avatar Jan 20 '17 17:01 hartloosli

@hardloosli Sounds like you also might be interested in #33751... ;)

JensRantil avatar Jan 22 '17 07:01 JensRantil

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