Home > other >  Ansible - Find files via a list with their name and delete them
Ansible - Find files via a list with their name and delete them

Time:10-21

im currently running into an issue with deleting certain files from our thumbor cache with ansible. After alot of snipping I receive a list with the file names and im running following ansible task to find and delete them:

      shell: find . -name {{ item }} -exec rm "{}" \;
      args:
        chdir: "{{ thumbor_data_path }}"
      ignore_errors: true
      loop:
        - deletion_hash
      when: file_url is defined and deletion_hash | length > 0

the list is definitly filled with the correct names of files I know exist and the task itself marks himself as changed, but the files are not getting deleted. The names of the files are sha1 hashes, and are two directories deep. Is there something wrong with the shell script?

Example of the deletion_hash list:

    "msg": [
        "115b744b9f6b23bbad3b6181c858cb953136", 
        "f52f17b2cca937e5586751ff2e938979890b", 
        "1c39661a0925b3cdb3b524983aaf6cccd6ee", 
        "1afc79a9e0e3c07ff0e95e1af3b5cb7ae54c", 
        "424e9159fe652f47c8e01d0aa85a86fbefed", 
        "11e4994789f24537d6feea085d2bf39c355b", 
        "a1d2fe0e122d37555df4062d4c0a5d10b651", 
        "aef976fc897a87091be5a8d5a11698e19591", 
        "e79f3ee1e6ccb3caff288b0028e031d75d77", 
        "9448e5e49679c908263922debdffff68eecb", 
        "a3933be52277a341906751c3da2dfb07ccd8", 
        "bef3370862a7504f7857be396d5a3139f5c0", 
        "8cc0cbe847234af96c0463d49c258c85d50f", 
        "1e7bf6110dcf994d1270682939e14416fc6e", 
        "d21dae2c047895129e7c462f6ddc4e512a58", 
        "c107b29b3185171ec46b479352fab6c97ad2"
    ]

CodePudding user response:

You can try using the file module; this comes with an assumption that the thumbor_data_path variable does not end with a /; if it does, you need to modify this a bit.

- name: Remove file (delete file)
  ansible.builtin.file:
    path: "{{ thumbor_data_path }}/{{ item }}"
    state: absent
  loop: deletion_hash
  when: file_url is defined and deletion_hash | length > 0
  • Related