file.copy force ignored during highstate, but not with 'salt-call state.sls_id'
During highstate, force is ignored:
Comment: The target file "/home/deploy/.env" exists and will not be overwritten
State below
bash_app_env:
file.managed:
- name: /home/deploy/.bash_app_env
- source: salt://application-base/autoscale-base/deployment-user/bash_app_env
- template: jinja
- user: deploy
- group: deploy
- mode: 0644
formatappenv:
file.replace:
- name: /home/deploy/.bash_app_env
- pattern: '^\s*$'
- repl: ''
- backup: False
- require:
- file: bash_app_env
removeblanklinesappenv:
cmd.run:
- name: "sed -i.bak '/^$/d' /home/deploy/.bash_app_env"
- user: deploy
- shell: /bin/bash
- require:
- file: formatappenv
createENV:
file.copy:
- name: /home/deploy/.env
- source: /home/deploy/.bash_app_env
- user: deploy
- group: deploy
- force: True
- require:
- cmd: removeblanklinesappenv
But run with 'salt-call state.sls_id', it functions as expected:
local:
----------
ID: bash_app_env
Function: file.managed
Name: /home/deploy/.bash_app_env
Result: True
Comment: File /home/deploy/.bash_app_env updated
Started: 14:32:44.099434
Duration: 28.466 ms
Changes:
ID: formatappenv
Function: file.replace
Name: /home/deploy/.bash_app_env
Result: True
Comment: Changes were made
Started: 14:32:44.132742
Duration: 4.311 ms
ID: removeblanklinesappenv
Function: cmd.run
Name: sed -i.bak '/^$/d' /home/deploy/.bash_app_env
Result: True
Comment: Command "sed -i.bak '/^$/d' /home/deploy/.bash_app_env" run
Started: 14:32:44.140639
Duration: 126.497 ms
Changes:
----------
pid:
16113
retcode:
0
stderr:
stdout:
----------
ID: createENV
Function: file.copy
Name: /home/deploy/.env
Result: True
Comment: Copied "/home/deploy/.bash_app_env" to "/home/deploy/.env"
Started: 14:32:44.269203
Duration: 4.918 ms
Changes:
----------
/home/deploy/.env:
/home/deploy/.bash_app_env
group:
deploy
user:
deploy
Summary
------------
Succeeded: 4 (changed=4)
Failed: 0
------------
Total states run: 4
Versions
deploy@prd-app-050521:~$ salt-call --versions-report
Salt: 2015.5.0
Python: 2.7.6 (default, Mar 22 2014, 22:59:56)
Jinja2: 2.7.2
M2Crypto: 0.21.1
msgpack-python: 0.3.0
msgpack-pure: Not Installed
pycrypto: 2.6.1
libnacl: Not Installed
PyYAML: 3.10
ioflo: Not Installed
PyZMQ: 14.0.1
RAET: Not Installed
ZMQ: 4.0.4
Mako: 0.9.1
Debian source package: 2015.5.0+ds-1trusty1
Nevermind... it's totally inconsistent...
Now the sls_id is showing this after running a highstate:
ID: createENV
Function: file.copy
Name: /home/deploy/.env
Result: True
Comment: The target file "/home/deploy/.env" exists and will not be overwritten
Started: 14:47:19.610031
Duration: 2.774 ms
Changes:
What gives? Is this a regression?
Hrm...I'm not sure. Can you elaborate of what you mean by "it's totally inconsistent"? Is the salt-call now failing where it wasn't before? Can you reproduce this consistently? For example, does it fail on the first attempt, but succeed on the second or anything like that?
I did not verify the second part of the report, however for me in 2015.5.0 on CentOS 6.6 x64 "-force: True" does not have any effect on copying file. File is not copied - both under higshtate and state.sls
Salt: 2015.5.0
Python: 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
Jinja2: 2.2.1
M2Crypto: 0.20.2
msgpack-python: 0.4.6
msgpack-pure: Not Installed
pycrypto: 2.0.1
libnacl: Not Installed
PyYAML: 3.10
ioflo: Not Installed
PyZMQ: 14.3.1
RAET: Not Installed
ZMQ: 3.2.4
Mako: Not Installed
For me it fails on all attempts, rerunning state does not help.
I think the point here is that the force: True simply isn't working on file.copy.
I get this:
Comment: The target file "/home/deploy/.env" exists and will not be overwritten
I think I may have muddied the waters with the inconsistently comment. Let's just say that highstates running file.copy with force, and not overwriting the files...consistently not overwriting the files.
Ok, that makes more sense. Thanks for the extra details!
Actually, in 2016.3 and 2015.8 it looks like no matter what (state.sls, state.single, state.highstate), force=True is ignored.
@cro check out #32732
I think this appeared inconsistent because, even when force=True we were not overwriting the file if the source and target hashes were identical. The PR by @lomeroe above will overwrite the file even if the hashes match.
local testing seems 2015.5.2 worked like the above PR, 2015.5.8 does not
I can back port the fix if it is good in develop....
@lomeroe That would be great. Thanks!
@rallytime backport PRs inbound, I don't believe 2014.7 suffers from the issue after review...
Thanks for doing these backports @lomeroe!
@naemono This bug should be fixed in the next releases of salt, on all current release branches. Can you confirm that this bug is fixed for you so we can close this issue?
I have just updated to Salt 2015.8.9. I now always have an unexpected state change in my salt runs when I use file.copy with force: True. Isn’t the previous behaviour described by @cro above exactly what (most) users would want?
With this change in place, it seems that using force: True will always copy the file, regardless of whether the source and destination are already identical. This means that the file.copy state always results in a change when force: True is used.
If force: True is not present, my understanding is that an existing destination file will never be replaced, even if it differs from the source.
Assuming the above is correct, how does one now use file.copy to copy a file only if the destination does not exist or differs from the source?
What I’m trying to achieve is functionality equivalent to this:
set timezone:
cmd.run:
- name: cp -p /usr/share/zoneinfo/UTC /etc/localtime
- unless: diff /usr/share/zoneinfo/UTC /etc/localtime
Based on the documentation, I was previously using the following (which now always results in a state change):
/etc/localtime:
file.copy:
- source: /usr/share/zoneinfo/UTC
- force: True
- preserve: True
If 'force: True' is not present, the file will be copied only if it doesn't exist. This should match the documentation here (though "moved" should probably be replaced with "copied" in the doc string, otherwise it is accurate)
The force flag will cause file.copy to always copy the file, if the destination exists, doesn't exist, or even if it hash matches the source.
Other than having the preserve option for user/group/mode settings, file.managed could possibly be used to do what you want. This would only change /etc/localtime if it didn't match /usr/share/zoneinfo/UTC. You'd have to explicitly set the user/group/mode you want...
/etc/localtime:
file.managed:
- source: /usr/share/zoneinfo/UTC
I suppose an "update" option could be added that would copy the file if the source and destination didn't match, but then you're very close to just having another "file.managed" (in my opinion). Or maybe file.managed needs a preserve option for locally sourced files, if that is your reason for not using it...
@lomeroe, thank you for the detailed response. I should be happy to use file.managed, and indeed that is what I was going to use first. However, the documentation for the file.managed source argument says:
The source file to download to the minion, this source file can be hosted on either the salt master server, or on an HTTP or FTP server.
That led me to believe that a local source is not possible (only a file hosted on the salt master, or an HTTP or FTP server), which is why I went hunting for an alternative (and found file.copy) in the first place.
Since I now know that local-to-the-minion files are indeed possible with file.managed, that is a perfectly acceptable solution for me. May I suggest that the opening sentence of the documentation for the file.managed source argument be updated expressly to mention local-to-the-minion files? (I see now that the very last sentence of the source documentation does mention local files for the source-list functionality. That was insufficient to clue-me-in in the face of the opening sentence, and the rest of the long opening paragraph, which does not mention local files at all.)
Thanks again!