I have a folder in base rorot:
/srv/salt/folder_name/folder_name_1/file_name.binNow i want to remove what ever is present inside
/srv/salt/folder_name/ after this path:
tried with python script and run it through the salt-run its through error:
Python Script:
def delete_file(file_path):
try:
# Remove the initial '/etc' from the file path
if file_path.startswith('/etc'):
file_path = file_path[4:]
# Add '/srv/salt' at the beginning of the file path
new_file_path = '/srv/salt' + file_path
# Check if the file exists
if not os.path.exists(new_file_path):
return f"The file {new_file_path} does not exist."
# Attempt to change permissions
chmod_command = ['chmod', 'u+w', new_file_path]
subprocess.run(chmod_command, check=True, text=True, capture_output=True)
# Construct the delete command
command = ['rm', '-rf', new_file_path]
# Run the delete command
result = subprocess.run(command, check=True, text=True, capture_output=True)
# Check the result
if result.returncode == 0:
return f"Successfully deleted: {new_file_path}"
else:
return f"Failed to delete: {new_file_path}\nError: {result.stderr}"
except subprocess.CalledProcessError as e:
return f"An error occurred: {e}\nError output: {e.stderr}"
except PermissionError as pe:
return f"Permission error: {pe}"
An error occurred: Command '['rm', '-rf', '/srv/salt/folder_name/f08790538d63ed11aa731bf35720eef3/prod_bin_ch12.bin']' returned non-zero exit status 1.
Error output: rm: cannot remove '/
srv/salt/ folder_name /f08790538d63ed11aa731bf35720eef3/prod_bin_ch12.bin ': Permission denied
tried via state.file:
delete_vetb_bin_files:
file.absent:
- name: /srv/salt/folder_name/f08790538d63ed11aa731bf35720eef3/prod_bin_ch12.bin
- include_empty: True
- force: True
----------
ID: delete_vetb_bin_files
Function: file.absent
Name:
/srv/salt/folder_name/f08790538d63ed11aa731bf35720eef3/prod_bin_ch12.bin
Result: False
Comment: Could not remove '
/srv/salt/folder_name/f08790538d63ed11aa731bf35720eef3/prod_bin_ch12.bin ': [Errno 13] Permission denied: '/srv/salt/vetb-bin/f08790538d63ed11aa731bf35720eef3/prod_bin_ch12.bin'
Started: 06:49:12.540831
Duration: 6.963 ms
Changes:
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 6.963 ms
file has permission:
total 864
-rwxrwxrwx 1 ubuntu ubuntu 881732 May 15 06:49 prod_bin_ch12.bin