"Failed to commit change, permission error" when using file.managed with check_cmd
Hi,
I'm using 2015.2.0rc1-1063-gac4264d and I'm having trouble using the file.managed state with check_cmd. As I understand it from the docs, check_cmd for file.managed will execute the specified command with the file as an argument to see if it can get a non-zero return status before replacing the file in question - great! However I'm being told there is a permissions issue and I can't for the life of me track down why.
The salt minion is running as root on CentOS7 and selinux is disabled.
[root@slave ~]# grep user /etc/salt/minion
# The user to run salt.
#user: root
[root@slave ~]# ps -ef | grep [s]alt-minion
root 3556 1 0 17:37 ? 00:00:01 /usr/bin/python /usr/bin/salt-minion
[root@slave ~]# sestatus
SELinux status: disabled
[root@slave ~]# getenforce
Disabled
I've got this state:
/etc/haproxy/haproxy.cfg:
file.managed:
- source: salt://l7_loadbalancer/files/haproxy.cfg.j2
- user: root
- group: root
- mode: 644
- template: jinja
- backup: minion
- check_cmd: /usr/sbin/haproxy -c -f
- require:
- pkg: haproxy
- context:
l7config: {{ pillar['l7config'] }}
Running this state gives the following error:
ID: /etc/haproxy/haproxy.cfg
Function: file.managed
Result: False
Comment: Failed to commit change, permission error
Started: 17:29:20.385135
Duration: 186.915 ms
Changes:
----------
diff:
Replace text file with binary file
I don't know why it's saying replace text with binary file as both the temporary file and the haproxy.cfg file it wants to replace are text:
[root@slave ~]# file /tmp/tmp5OY1Y8
/tmp/tmp5OY1Y8: ASCII text, with very long lines
[root@slave ~]# file /etc/haproxy/haproxy.cfg
/etc/haproxy/haproxy.cfg: ASCII text, with very long lines
Relevant debug logs from the minion:
2015-03-18 17:38:26,516 [salt.state ][INFO ] Running state [/etc/haproxy/haproxy.cfg] at time 17:38:26.515771
2015-03-18 17:38:26,516 [salt.state ][INFO ] Executing state file.managed for /etc/haproxy/haproxy.cfg
2015-03-18 17:38:26,521 [salt.fileclient ][INFO ] Fetching file from saltenv 'base', ** skipped ** latest already in cache 'salt://l7_loadbalancer/files/haproxy.cfg.j2'
2015-03-18 17:38:26,522 [salt.utils.jinja ][DEBUG ] Jinja search path: ['/var/cache/salt/minion/files/base']
2015-03-18 17:38:26,607 [salt.loaded.int.states.file][DEBUG ] running our check_cmd
2015-03-18 17:38:26,607 [salt.loaded.int.module.cmdmod][INFO ] Executing command '/usr/sbin/haproxy -c -f /tmp/tmp5OY1Y8' in directory '/root'
2015-03-18 17:38:26,629 [salt.loaded.int.module.cmdmod][DEBUG ] output: Configuration file is valid
2015-03-18 17:38:26,631 [salt.state ][ERROR ] File changed:
Replace text file with binary file
2015-03-18 17:38:26,631 [salt.state ][INFO ] Completed state [/etc/haproxy/haproxy.cfg] at time 17:38:26.631079
You can see that the message "Configuration file is valid" appears which means the check_cmd will have received a zero exit status.
Permissions on that temp file and the haproxy.cfg file both look to be correct - certainly no reason it shouldn't be able to overwrite it:
[root@slave ~]# ll /tmp/tmp5OY1Y8
-rw-r--r-- 1 root root 3654 Mar 18 17:38 /tmp/tmp5OY1Y8
[root@slave ~]# lsattr /tmp/tmp5OY1Y8
---------------- /tmp/tmp5OY1Y8
[root@slave ~]# ll /etc/haproxy/haproxy.cfg
-rw-r--r-- 1 root root 3653 Mar 18 17:32 /etc/haproxy/haproxy.cfg
[root@slave ~]# lsattr /etc/haproxy/haproxy.cfg
---------------- /etc/haproxy/haproxy.cfg
So I'm at a complete loss! I can't find anything on the Google about this error, but I'm still 50/50 as to whether this is a bug or going to turn into a support request...!
Thanks
I've just remembered that the minions are 2014.7.1. I'll try updating these to the same 2015.2 version and trying again (unfortunately I need to leave the office now and can't try this till the morning!)
I can confirm that I can duplicate this issue with the minion running 2015.2.0rc1-1074-g8a301bc.
Same exact thing is happening with 2014.7.2 (both minions and masters) running on rhel6 with selinux disabled trying to manage the /etc/sudoers file using a check_cmd of '/usr/sbin/visudo -c -f '
- Does anything interesting happen when you run
/usr/sbin/haproxy -c -f ; echo $?or/usr/sbin/visudo -c -f ; echo $? - Does the state run successfully if you remove the
- check-cmd: <cmd>? - There are three places in
salt.modules.manage_file(which thefile.managedstate function uses underneath that provide this error. None of them say anything about what the original message that came. I have a patch that can propagate that error through to the state return if you want to try it and see if it will give you enough information to identify and resolve the problem you're having.
Performing the check manually on the generated file returns a 0 exit status - the config is fine! (Unless you mean for me to run it without specifying the file at the end in which case an error occurs and I get a nonzero exit status)
Without check_cmd it does work fine.
I'll try the patch in the morning and report back!
Does the minion have a funny default ${SHELL}? check_cmd explicitly sets this shell when it runs the check command. I can't think of anything else that would be causing this, so hopefully the new error message will be more useful.
It's a default install of the Salt minion on a default install of CentOS 7 - there shouldn't be any funny shells in use.
Using the patch I'm seeing the error:
ID: /etc/haproxy/haproxy.cfg
Function: file.managed
Result: False
Comment: Failed to commit change: [Errno 2] No such file or directory: /tmp/tmpuXXBkn
Started: 09:08:28.244326
Duration: 231.845 ms
Changes:
----------
diff:
Replace text file with binary file
Logs from the salt minion at the time:
2015-03-20 09:08:28,245 [salt.state ][INFO ][10768] Executing state file.managed for /etc/haproxy/haproxy.cfg
2015-03-20 09:08:28,269 [salt.fileclient ][INFO ][10768] Fetching file from saltenv 'base', ** skipped ** latest already in cache 'salt://l7_loadbalancer/files/haproxy.cfg.j2'
2015-03-20 09:08:28,269 [salt.utils.jinja ][DEBUG ][10768] Jinja search path: ['/var/cache/salt/minion/files/base']
2015-03-20 09:08:28,391 [salt.loaded.int.states.file][DEBUG ][10768] running our check_cmd
2015-03-20 09:08:28,392 [salt.loaded.int.module.cmdmod][INFO ][10768] Executing command '/usr/sbin/haproxy -c -f /tmp/tmp2EWimo' in directory '/root'
2015-03-20 09:08:28,475 [salt.loaded.int.module.cmdmod][DEBUG ][10768] output: Configuration file is valid
2015-03-20 09:08:28,476 [salt.state ][ERROR ][10768] File changed:
Replace text file with binary file
2015-03-20 09:08:28,476 [salt.state ][INFO ][10768] Completed state [/etc/haproxy/haproxy.cfg] at time 09:08:28.476171
The "Configuration file is valid" message is the output from haproxy when the haproxy -c -f command is run, so the file must have existed at some point in the past. The haproxy command is non-destructive so will not have removed the file itself.
The filename from the IOError and the filename from the Salt logs are different, so I'm guessing that's the cause of the issue?
The file referenced in the minion logs still exists after the salt run:
[root@slave ~]# ll /tmp/tmp2EWimo
-rw-r--r-- 1 root root 3654 Mar 20 09:08 /tmp/tmp2EWimo
The file mentioned in the state logs does not exist.
@Xiol, thank you for your work on this issue.
Is there any update on this issue? This is still happening on 2017.7.0. file.manage check_cmd is completely useless at this point.
We just updated to 2017.7.0 and are seeing this new error as well
salt-call state.apply [ERROR ] File changed: Replace text file with binary file local:
ID: /etc/sudoers
Function: file.managed
Result: False
Comment: Failed to commit change: [Errno 2] No such file or directory: /tmp/__salt.tmp.c764V3
Started: 17:19:15.435003
Duration: 52.913 ms
Changes:
----------
diff:
Replace text file with binary file
Pertinent segment, different run, so tmp file is different:
DEBUG ] running our check_cmd [INFO ] Executing command '/usr/sbin/visudo -c -f /tmp/__salt.tmp.1HOMzz' in directory '/root' [DEBUG ] stdout: /tmp/__salt.tmp.1HOMzz: parsed OK [ERROR ] File changed: Replace text file with binary file [INFO ] Completed state [/etc/sudoers] at time 17:00:45.599713 duration_in_ms=53.56
also seeing the error now in 2017.7.0 with check_cmd: /usr/sbin/visudo -c -f where it worked previously
This might be fixed in the 2017.7.1 release. I had a very similar problem, and found issue #42404, which exactly matched my problem (and seems to be what @ssplatt talks about as well). That issue has been fixed with #42411.
I can confirm this issue is now gone for me in 2017.7.2-1.el7
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.