ansibledelete-filemolecule

How to delete files inside a directory without deleting the directory with Ansible?


I am trying 2 methods to delete all files inside a directory my_dir. This is a parameter managed from another file. My workflow is creating the directory first, then copy files in that directory, then delete all files in there. So here is my first solution using loop_control:

---
# tasks file for deploy_files

- name: Create model directory
  file:
    path: "{{ my_dir }}"
    state: directory
    owner: "{{ my_daemon_name }}"
    group: "{{ my_daemon_name }}"
    mode: u=rwx,g=rX,o=rX

- name: Copy models
  copy:
    src: "{{ files_src_dir }}"
    dest: "{{ my_dir }}"
    owner: "{{ my_daemon_name }}"
    group: "{{ my_daemon_name }}"
    mode: u=rw,g=r,o=r

- name: Get directory listing
  find:
    path: "{{ my_dir }}"
    file_type: any
    hidden: yes
  register: directory_content_result

- name: Remove directory content
  file:
    path: "{{ item.path }}"
    state: absent
    owner: "{{ my_daemon_name }}"
    group: "{{ my_daemon_name }}"
    mode: u=rw,g=r,o=r
  with_items: "{{ directory_content_result.files }}"
  loop_control:
    label: "{{ item.path }}"

And, here is the 2nd solution without loop_control:

    ---
    # tasks file for deploy_files
    
    - name: Create model directory
      file:
        path: "{{ my_dir }}"
        state: directory
        owner: "{{ my_daemon_name }}"
        group: "{{ my_daemon_name }}"
        mode: u=rwx,g=rX,o=rX
    
    - name: Copy models
      copy:
        src: "{{ files_src_dir }}"
        dest: "{{ my_dir }}"
        owner: "{{ my_daemon_name }}"
        group: "{{ my_daemon_name }}"
        mode: u=rw,g=r,o=r

- name: Find files in directory
  find:
    path: "{{ my_dir }}"
  register: files_to_delete

- name: Remove files in directory
  file:
    path: "{{ item.path }}"
    state: absent
    owner: "{{ my_daemon_name }}"
    group: "{{ my_daemon_name }}"
    mode: u=rwx,g=rX,o=rX
  with_items: "{{ files_to_delete.files }}"

None of them work as when I ran my script, I got:

ERROR: Idempotence test failed because of the following tasks.

AnsibleERR1

Please, note that my tasks Create model directory and Copy models works perfectly fine as I already tested them. My script failed only when I added the last 2 tasks to delete the files. Could someone help me what I did wrong here please? Thank you.


Solution

  • There is nothing technically wrong with your playbook and it is actually doing exactly what you asked.

    I have to take a guess since you did not mention how you run your playbook at all, but I'm pretty sure you are using molecule to test your playbook/role (which is a very good idea by the way). And what is shown in your question is molecule failing its idempotence check.

    What happens is that molecule is running your playbook a second time and expects that nothing changes on the remote target since it is supposed to be already aligned to the desired state (by running the earlier converge step)

    Since your playbook unconditionally creates then deletes files, it will always change something on the target and that check will never pass. You either have to modify the way you are doing all this so that it becomes idempotent or ignore the changes on those tasks if you know they are not relevant (i.e. changed_when: false).

    From experience, the best option is usually to make things idempotent as far as possible.