Ansible Playbook Task to Delete all Contents of a Directory

Today, I came across a situation where I had to write a task in an ansible playbook to delete the contents of /var/cache/yum / directory. Now, if we check the ansible document for the file module, there is no ‘delete’ parameter mentioned. However, we can do a trick that can delete the complete directory and recreate the same using the file module.

How to check ansible manual for file module?

$ ansible-doc file

Below is the task which can be inserted into playbook which can delete any directory

- name: remove files and directories
    state: "{{ item }}"
    path: "/var/cache/yum/"
    owner: root
    group: root
    mode: '0755'
    - absent
    - directory